JDBC的Statement

一、Statement

1.Statement对象,用于执行静态sql语句并返回其生成的结果的对象。

2.在建立连接后,需要对数据库进行访问,执行命令或是sql语句,可以通过

(1).Statement【存在sql注入】

(2).PreparedStatement[预处理]

(3).CallableStatement[存储过程]

3.Statement对象执行sql语句,存在sql注入风险。

package com.jun.jdbc.Statement;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import java.util.Scanner;

/**
 *statement sql注入
 */
public class Statement01 {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(System.in);
        //用户输入管理员的姓名和密码
        System.out.println("请输入姓名");
        String admin_name = scanner.nextLine();//不能使用next,遇到空格或’会结束
        System.out.println("请输入密码");
        String admin_pwd = scanner.nextLine();
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //获取到相关值
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        //得到Statement
        Statement statement = connection.createStatement();
        //sql语句
        String sql ="select name,pwd from admin where name='"+admin_name+"' and pwd='"+admin_pwd+"'";
        ResultSet resultSet = statement.executeQuery(sql);
        if (resultSet.next()){//假如查询有记录,即管理员存在
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        //关闭流
        resultSet.close();
        statement.close();
        connection.close();
    }
}

 

你可能感兴趣的:(JDBC,jdbc)