JDBC初体验(二)

发布于:2022-12-24 ⋅ 阅读:(338) ⋅ 点赞:(0)

目录

SQL注入

PreparedStatement接口

PreparedStatement的使用

PreparedStatement编程模板

使用JDBC完成数据添加的操作模板

使用JDBC完成数据修改的操作模板

使用JDBC完成数据删除的操作模板

 方法总结


SQL注入

注入原理:利用现有应用程序,将(恶意的)SQL命令注入到后台数据库引擎执行的能力,它可以通过在Web表单中输入(恶意)SQL语句得到一个存在安全漏洞的网站上的数据库,而不是按照设计者意图去执行SQL语句。


防止SQL注入的方法:
过滤用户输入的数据中是否包含非法字符;
分步校验!先使用用户名来查询用户,如果查找到了,再比较密码;
使用PreparedStatement接口。


语法:12'or'1'='1

PreparedStatement接口

PreparedStatement接口是Statement的子接口,你可以使用该接口来替换Statement接口。

 

PreparedStatement的使用

使用Connection对象的prepareStatement(String sql):即创建它时就让它与一条SQL语句绑定;
编写SQL语句时,如果存在参数,使用“?”作为数据占位符;
调用PreparedStatement的setXXX()系列方法为占位符设置值,索引从1开始;
调用executeUpdate()或executeQuery()方法,但要注意,调用没有参数的方法;

PreparedStatement编程模板

Connection con = null;
PreparedStatement  stmt = null;
ResultSet rs = null;
try {
      Class.forName(“JDBC驱动类”); //1.加载驱动
      con=DriverManager.getConnection(URL,数据库用户名,密码);//2.获取Connection 连接对象

      String sql = "SELECT username,userpass,nickname FROM users where username=? and userpass=?";
      stmt = con.prepareStatement(sql); //3.创建PrepareStatement对象,执行SQL语句

      smt.setString(1, “zhangsan”);  //4.给参数赋值
      smt.setString(2, "0");
      rs = stmt.executeQuery();//5.返回ResultSet并查询结果
      ……
}catch(Exception e){ 
      e.printStackTrace();
}finally{
          //6.释放资源
try {if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}

使用JDBC完成数据添加的操作模板

Connection con = null;
PreparedStatement  stmt = null;
ResultSet rs = null;
try {
      Class.forName(“JDBC驱动类”); //1.加载驱动
      con=DriverManager.getConnection(URL,数据库用户名,密码);//2.获取Connection 连接对象

      String sql = " INSERT INTO student(字段列表) VALUES(值列表) ";
      stmt = con.prepareStatement(sql); //3.创建PrepareStatement对象

      smt.setString(1, “zhangsan”);  //4.给参数赋值
      smt.setInt(2, 0);
      int row = stmt. executeUpdate();//5.执行SQL语句,返回受影响的行数
      ……
}catch(Exception e){ 
      e.printStackTrace();
}finally{
          //6.释放资源
try {if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}

使用JDBC完成数据修改的操作模板

Connection con = null;
PreparedStatement  stmt = null;
ResultSet rs = null;
try {
      Class.forName(“JDBC驱动类”); //1.加载驱动
      con=DriverManager.getConnection(URL,数据库用户名,密码);//2.获取Connection 连接对象

      String sql = " UPDATE student SET loginpwd=? WHERE studentno=? AND loginpwd=? ";
      stmt = con.prepareStatement(sql); //3.创建PrepareStatement对象

      smt.setString(1, “zhangsan”);  //4.给参数赋值
      smt.setInt(2, 0);
      int row = stmt. executeUpdate();//5.执行SQL语句,返回受影响的行数
      ……
}catch(Exception e){ 
      e.printStackTrace();
}finally{
          //6.释放资源
try {if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}

使用JDBC完成数据删除的操作模板

Connection con = null;
PreparedStatement  stmt = null;
ResultSet rs = null;
try {
      Class.forName(“JDBC驱动类”); //1.加载驱动
      con=DriverManager.getConnection(URL,数据库用户名,密码);//2.获取Connection 连接对象

      String sql = " DELETE FROM student WHERE studentno=? ";
      stmt = con.prepareStatement(sql); //3.创建PrepareStatement对象

      smt.setString(1, “zhangsan”);  //4.给参数赋值
      smt.setInt(2, 0);
      int row = stmt. executeUpdate();//5.执行SQL语句,返回受影响的行数
      ……
}catch(Exception e){ 
      e.printStackTrace();
}finally{
          //6.释放资源
try {if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}

 方法总结

    • 1、载驱动 DriverManager
    • 2、接数据库 Connection
    • 3、执行
    • 4、行,返回结果 ResultSet
    • 5、放资源 close()

    • 贾琏欲执事

网站公告

今日签到

点亮在社区的每一天
去签到