Java连接MySQL(SQL注入攻击以及解决方法)

Java中简单的MySQL连接操作
基本步骤:

1.加载驱动类
  Class.forName(“com.mysql.jdbc.Driver”);
2.建立连接
  Connection conn = DriverManager.getConnection(url,user,password);
  三个参数的解读:
    url:"jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8"
    localhost是地址,由于连接的是主机的MySQL,所以是localhost,
    3306是MySQL默认的断端口号
    ishopn是你要连接的数据库名称
    ?characterEncoding是编码格式,防止出现乱码
    user:“root” 登录MySQL的用户名
    password:“1234” 登录MySQL的密码
3.编写相应的SQL语句
  String sql = “insert into commoditytype (ct_id,ct_name) values (7,‘电子产品’)”;
4.获取执行SQL语句的对象
  Statement state = conn.createStatement();
5.执行SQL语句
  除了查询语句之外,其他的修改,删除,添加语句返回值都为int类型,表示数据库中执行此命令后几行受到影响
  int s = state.executeUpdate(sql); //sql为你的SQL语句
6.关闭连接
  conn.close();

实例操作

public static void main(String[]args){
	Connection conn = null;
	try{
		Class.forName("com.mysql.jdbc.Driver"); //加载驱动类
		String url = "jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";
		String user = "root";
		String password = "1234";
		conn = DriverManager.getConnection(url,user,password); //建立连接
		Statement state = conn.createStatement();//获取执行对象
		String sql = "insert into commoditytype (ct_id,ct_name) values (7,'电子产品')";
		int s = state.executeUpdate(sql); //执行sql语句,并返回s行受到影响
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		if(conn!=null)
		try{
			conn.close(); //关闭连接
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}

SQL控制台模仿登录

public static void main(String[] args) {
		Connection coon = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");  //获取所需的驱动
			String url = "jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";
			String user = "root";
			String password = "1234";
			coon = DriverManager.getConnection(url, user, password);// 建立连接
			
			/**
			 * SQL语句操作
			   *   登录功能
			 */
			Scanner input = new Scanner(System.in);
			System.out.println("请输入你的账号:");
			String cu_name = input.next();
			System.out.println("请输入你的密码:");
			String cu_phone = input.next();
			/*
			*登录功能就是去数据库中查找你所输入的账号免所对应的数据
			*假如数据条数大于一条,说明存在该账号,反正不存在
			*/
			String sql = "select count(*) from customer where cu_name='"+cu_name+"' and cu_phone='"+cu_phone+"'";
			Statement state = coon.createStatement();  //获取执行对象
			ResultSet rs = state.executeQuery(sql);  //执行SQL语句
			rs.next(); //由于查询语句返回的是一个结果集,需要迭代方法,next()就是获取下一个
			if(rs.getInt(1)>0) {
				System.out.println("登录成功");
			}else {
				System.out.println("登录失败");
			}	
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(coon!=null) {
				try {
					coon.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

在上述代码中,由于SQL语句是通过拼接完成的,那么就有可能存在SQL注入攻击
例如:

String sql = "select count(*) from customer where cu_name='"+cu_name+"' and cu_phone='"+cu_phone+"'";
//该条SQL语句中的cu_name为 ' or 1=1 #
//那么拼接之后的SQL语句就是
select count(*) from customer where cu_name='' or 1=1 # and cu_phone='cu_phone'
//仔细观察不难发现 密码部分被#注释了,前面的账号部分用了or1=1,恒真,所以在不知道账号密码时也能通过验证了

解决SQL注入攻击

Java为了解决SQL注入攻击,有了一个新的方法,一下是改进的登录

public static void main(String[] args) {
		Connection coon = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");  //获取所需的驱动
			String url = "jdbc:mysql://localhost:3306/ishopn?characterEncoding=utf8";
			String user = "root";
			String password = "1234";
			coon = DriverManager.getConnection(url, user, password);// 建立连接
			
			/**
			 * SQL语句操作
			   *   登录功能
			 */
			Scanner input = new Scanner(System.in);
			System.out.println("请输入你的姓名:");
			String cu_name = input.next();
			System.out.println("请输入你的手机号:");
			String cu_phone = input.next();
			
			String sql = "select count(*) from customer where cu_name=? and cu_phone=?";
			//这里的SQL语句没有使用拼接,而是使用了?来代替字段
			PreparedStatement ps = coon.prepareStatement(sql);//这里使用了prepareStatement()方法   
			ps.setString(1, cu_name);//这里设置?的值  1对应第一个?
			ps.setString(2, cu_phone);
			ResultSet rs = ps.executeQuery();
			rs.next();
			if(rs.getInt(1)>0) {
				System.out.println("登录成功");
			}else {
				System.out.println("登录失败");
			}	
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(coon!=null) {
				try {
					coon.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

你可能感兴趣的:(MySQL)