数据库驱动

image.png

程序会通过数据库驱动,与数据库交互

JDBC
为了简化开发人员的操作(对数据库的统一操作),提供了一个(java操作数据库)规范 jdbc
这些规范的实现 由具体的厂商实现

对于开发人员,只需要掌握JDBC接口的操作
image.png

java.sql
javax.sql
还需要解压一个数据库驱动包

jdbc实例

  1. public class JdbcDemo {
  2. public static void main(String[] args) throws SQLException {
  3. //1.加载驱动
  4. try {
  5. Class.forName("com.mysql.jdbc.Driver");
  6. } catch (ClassNotFoundException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. //2.创建连接
  11. //useUnicode=true&characterEncoding=utf8 设置编码格式
  12. String url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8";
  13. String user="root";
  14. String password="123321";
  15. Connection connection = (Connection) DriverManager.getConnection(url, user, password);
  16. //3,创建Statement对象 用于执行sql语句
  17. Statement statement = (Statement) connection.createStatement();
  18. //4.执行sql语句 接收返回值
  19. //executeUpdate 增删改都属于update方法 返回值为数据库中被影响数据的行数
  20. //executeQuery查询 返回值是数据库中的数据集 保存在ResultSet对象中
  21. int count = statement.executeUpdate("insert into `student`(`StudentNo`,`LoginPwd`,`StudentName`,`Sex`,`GradeID`,`Phone`,`Address`,`BornDate`,`Emile`,`IdentityCard`)\r\n" +
  22. "values('1705100217','111','测试名',0,1,'111','彭山','2020-8-3','@qq.com','111222333')");
  23. ResultSet resultSet = statement.executeQuery("select * from student");
  24. //
  25. // if(count>0) {
  26. // System.out.println("修改成功");
  27. // }else {
  28. // System.out.println("修改失败");
  29. // }
  30. // while(resultSet.next()) {
  31. // System.out.println(resultSet.getObject("StudentNo"));
  32. // System.out.println(resultSet.getObject("StudentName"));
  33. // System.out.println(resultSet.getObject("BornDate"));
  34. // System.out.println(resultSet.getObject("IdentityCard"));
  35. // }
  36. //5释放连接
  37. resultSet.close();
  38. statement.close();
  39. connection.close();
  40. }
  41. }

Statement对象

jdbc中的statement对象用于向数据库发送sql语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送对应的SQL语句即可
statemrnt对象主要的两个方法

  • executeUpdate(String sql)
    • 用于向数据库中发送增删改语句
    • 返回类型 int 数据库中被影响的行数
  • executeQuery(String sql)
    • 用于向数据库中发送查语句
    • 返回类型 ResultSet 保存sql语句查询返回的数据集

      SQL注入

SQL注入即是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息

  1. public class JdbcDemo {
  2. public static void main(String[] args) throws SQLException {
  3. //1.加载驱动
  4. try {
  5. Class.forName("com.mysql.jdbc.Driver");
  6. } catch (ClassNotFoundException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. //2.创建连接
  11. //useUnicode=true&characterEncoding=utf8 设置编码格式
  12. String url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8";
  13. String user="root";
  14. String password="123321";
  15. Connection connection = (Connection) DriverManager.getConnection(url, user, password);
  16. //3,创建Statement对象 用于执行sql语句
  17. Statement statement = (Statement) connection.createStatement();
  18. //4.执行sql语句 接收返回值
  19. //select * from student where name='' or '1=1' and password='' or '1=1'
  20. ResultSet resultSet = statement.executeQuery("select * from student where name='' or '1=1' and password='' or '1=1'");
  21. //
  22. // if(count>0) {
  23. // System.out.println("修改成功");
  24. // }else {
  25. // System.out.println("修改失败");
  26. // }
  27. // while(resultSet.next()) {
  28. // System.out.println(resultSet.getObject("StudentNo"));
  29. // System.out.println(resultSet.getObject("StudentName"));
  30. // System.out.println(resultSet.getObject("BornDate"));
  31. // System.out.println(resultSet.getObject("IdentityCard"));
  32. // }
  33. //5释放连接
  34. resultSet.close();
  35. statement.close();
  36. connection.close();
  37. }
  38. }

select * from student where name=’’ or ‘1=1’ and password=’’ or ‘1=1’
name=’’ or ‘1=1’ 返回true
password=’’ or ‘1=1’ 返回true
name 和password总是返回true 可以查询表中的所以信息

PrepareStatement(防止SQL注入)

防止SQL 注入的本质:将参入的参数当做字符,如果存在转义字符,会被直接转义;
**

  1. public class JdbcDemo {
  2. public static void main(String[] args) throws SQLException {
  3. //1.加载驱动
  4. try {
  5. Class.forName("com.mysql.jdbc.Driver");
  6. } catch (ClassNotFoundException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. //2.创建连接
  11. //useUnicode=true&characterEncoding=utf8 设置编码格式
  12. //useSSL=true"
  13. String url="jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8";
  14. String user="root";
  15. String password="123321";
  16. Connection connection = (Connection) DriverManager.getConnection(url, user, password);
  17. //4.执行sql语句 接收返回值
  18. // ? 占位符
  19. //prepareStatement(sql) 预编译sql 不执行
  20. PreparedStatement statement =connection.prepareStatement("INSERT INTO `student`(`StudentNo`,`LoginPwd`,`StudentName`,`Sex`,`GradeID`,`Phone`,`Address`,`BornDate`,`Emile`,`IdentityCard`) \r\n" +
  21. "VALUES(?,?,?,?,?,?,?,?,?,?)");
  22. //对相应位置的占位符赋值 根据对应位置数据类型 使用setString() setint() setObject() 等方法
  23. statement.setObject(1,"1705100217");
  24. statement.setObject(2,"111");
  25. statement.setObject(3,"测试");
  26. statement.setObject(4,0);
  27. statement.setObject(5,1);
  28. statement.setObject(6,"111");
  29. statement.setObject(7,"彭山");
  30. statement.setObject(8,"2020-8-3");
  31. statement.setObject(9,"@qq.com");
  32. statement.setObject(10,"111222333");
  33. int count = statement.executeUpdate();
  34. System.out.println(count);
  35. //5释放连接
  36. statement.close();
  37. connection.close();
  38. }
  39. }