当前位置:   article > 正文

JDBC (基础)

JDBC (基础)

我们之前操作数据库--都是通过图形化界面来对mysql进行操作,我们实际开发是我们应该通过java代码来完成对数据库的操作

1 为什么学 jdbc

2. jdbc 如何连接数据库的

Java :OOP 面向对象的编程对象

SQL: 结构化查询语言

思考: java和sql是两种不同的编程语言。连接必须请翻译。翻译程序---每个数据库厂商都提高了翻译软件-- -打包jar。---我们的java代码引入jar就可完成与数据库的沟通。网上可以下载。

  1. 创建 Java 工程并把 Java 放入 lib 目录下

  1. 添加到库

代码(增删改)

  1. // 1、加载驱动
  2. Class.forName("com.mysql.cj.jdbc.Driver");
  3. // 2、获取连接数据库
  4. String url = "jdbc:mysql://localhost:3306/数据库名"
  5. String user = "root";
  6. String psw = "root";
  7. Connection con = DriverManager.getConnection(url,user,psw);
  8. //3. 获取执行sql语句的对象
  9. Statement st = con.createStatement();
  10. //4. 执行sql语句: 执行增删改的sql
  11. String sql = "sql语句";
  12. st.executeUpdate(sql);
  13. st.close();
  14. con.close();

代码(查)

  1. public static void q(){
  2. Connection conn = null;
  3. Statement stmt = null;
  4. ResultSet rs = null;
  5. try {
  6. Class.forName("com.mysql.cj.jdbc.Driver");
  7. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",
  8. "root", "root");
  9. stmt = conn.createStatement();
  10. String sql = "select * from dept";
  11. rs = stmt.executeQuery(sql);
  12. System.out.println("编号\t部门\t\t地址");
  13. while (rs.next()){
  14. int deptNo = rs.getInt(1);
  15. String dName = rs.getString(2);
  16. String loc = rs.getString(3);
  17. System.out.println(deptNo +"\t"+ dName +"\t"+loc);
  18. }
  19. } catch (Exception e) {
  20. System.out.println(e.getMessage());;
  21. }finally {
  22. try {
  23. if (rs != null) {
  24. rs.close();
  25. }
  26. if (stmt != null) {
  27. stmt.close();
  28. }
  29. if (conn != null) {
  30. conn.close();
  31. }
  32. } catch (SQLException e) {
  33. System.out.println(e.getMessage());;
  34. }
  35. }

3. 异常

  1. public static void selectAll() {
  2. ResultSet rs=null;
  3. Statement st=null;
  4. Connection conn=null;
  5. try {
  6. Class.forName("com.mysql.cj.jdbc.Driver");
  7. String url = "jdbc:mysql://localhost:3306/qy174";
  8. String user = "root";
  9. String password = "root";
  10. conn = DriverManager.getConnection(url, user, password);
  11. st = conn.createStatement();
  12. String sql = "select * from dept";
  13. //执行查询的sql语句 ResultSet:把数据库表返回的记录都封装到ResultSet结果集对象中
  14. rs = st.executeQuery(sql);
  15. //遍历结果集 next():判断下一条是否有记录,如果有记录返回true并且指针下移。
  16. while (rs.next()) {
  17. //获取当前行的指定列的值
  18. int deptno = rs.getInt("deptno");
  19. String dname = rs.getString("dname");
  20. String loc = rs.getString("loc");
  21. System.out.println("部门编号:" + deptno +
  22. ";部门名称:" + dname + ";部门地址:" + loc);
  23. }
  24. } catch (ClassNotFoundException e) {
  25. e.printStackTrace();
  26. } catch (SQLException throwables) {
  27. throwables.printStackTrace();
  28. } finally {
  29. if(rs!=null){
  30. try {
  31. rs.close();
  32. } catch (SQLException throwables) {
  33. throwables.printStackTrace();
  34. }
  35. }
  36. if(st!=null){
  37. try {
  38. st.close();
  39. } catch (SQLException throwables) {
  40. throwables.printStackTrace();
  41. }
  42. }
  43. if(conn!=null){
  44. try {
  45. conn.close();
  46. } catch (SQLException throwables) {
  47. throwables.printStackTrace();
  48. }
  49. }
  50. }
  51. }

4. SQL 注入安全问题

我们输入的账号和密码再数据库中压根不存在,但是显示登录成功。出现了sql注入安全隐患问题。 由于Statement类的问题。找到PreparedStatement它是Statement的子类

出现这类问题的主要原因是系统没有对用户输入的内容进行充分的检查,而用户输入的数据中存在非法的 sql 语句导致绕过了验证,从而去恶意的去攻击数据库。

为了避免这类问题:在获取执行sql语句的对象的时候:使用 PreparedStatement

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号