JDBC的编程步骤

  1. 加载数据库驱动类
  2. Java 连接数据库
  3. 执行SQL语句
  4. 获得结果
  5. 处理结果(封装成Java对象)
  6. 关闭数据库连接

一. 下载数据库驱动

Java 连接MySQL 8数据库 (网址: https://dev.mysql.com/downloads/connector/j/

  1. maven:https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.16

Java对数据库的操作的源代码,dao包(DAO: Data Access Object 数据访问对象)

二、JDBC编程步骤

1.加载驱动类

  1. Class.forName()|Class.forName().new Instance()|new DriverName()
  2. 实例化自动向DriverManager注册

2. Connect to the Database

  1. String url ="jdbc:mysql://localhost:3306/studentSystem?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimeZone=Asia/Shanghai"; //Mysql 数据库连接 的URL 格式
  2. String username ="root";
  3. String password = "root"
  4. DriverManager.getConnection(url,username,password);
  5. //Oracle 数据库的URL 格式 : "jdbc:oracle:thin:@localhost:1521:oracle"
  6. //SqlServer 数据库的URL 格式 :"jdbc:microsoft:sqlserver://localhost:1433:DatabaseName=mydb";

3. 执行SQL语句

  1. Connection.createStatement()
  2. Statement.executeQuery()
  3. Statement.executeUpdate()

4. 获得执行结果

  1. ResultSet rs
  2. 循环取得结果while(rs.next())

5. 显示数据

  1. 将数据库的各种类型转换成为Java中的类型(getxxx)方法

6. 关闭连接

  1. Close the resultset | close the Statement | close the Connection

JDBC核心接口与类

Driver接口:

说明:每一个数据库驱动程序如果要实现与JDBC API的对接,就需要实现Driver接口,也就是说每个驱动程序都应该提供一个实现Driver接口的类,DriverManager 会试着加载尽可能多的它可以找到的驱动程序,然后,对于任何给定连接请求,它会让每个驱动程序依次试着连接到目标 URL 。在加载某一 Driver 类时,它应该创建自己的实例并向 DriverManager 注册该实例。这意味着用户可以通过调用以下程序加载和注册一个驱动程序 。

DriverManager类作用:

通常用来注册了数据库驱动程序后获得一个数据库连接对象。主要使用它的getConnection()方法。DriverManager 会试着从初始化时加载的那些驱动程序以及使用与当前 applet 或应用程序相同的类加载器显式加载的那些驱动程序中查找合适的驱动程序。

Connection接口

一个Connection对象表示与特定的数据库的连接,主要用于执行SQL语句并得到执行的结果。默认情况下,Connection对象处于自动提交模式下,也就是说在执行每个SQL语句后都会自动提交.

Statement接口

用于执行静态SQL语句并返回它所生成结果的对象。在默认情况下,同一时刻每个Statement对象只能打开一个ResultSet对象。

PreparedStatement接口

表示预编译的 SQL 语句的对象, SQL 语句被预编译并且存储在 PreparedStatement 对象中。然后可以使用此对象高效地多次执行该语句。

注:用来设置 IN 参数值的 setter 方法(setShort、setString 等等)必须指定与输入参数的已定义 SQL 类型兼容的类型。例如,如果 IN 参数具有SQL 类型 int,那么应该使用 setInt 方法

ResultSet接口

表示数据库结果集的数据表,通常通过执行查询数据库的语句生成 。

ResultSet 对象具有指向其当前数据行的指针。最初,指针被置于第一行之前。next 方法将指针移动到下一行;因为该方法在 ResultSet 对象中没有下一行时返回 false,所以可以在 while 循环中使用它来迭代结果集 。

默认的 ResultSet 对象不可更新,仅有一个向前移动的指针。因此,只能迭代它一次,并且只能按从第一行到最后一行的顺序进行。可以生成可滚动和/或可更新的 ResultSet 对象。以下代码片段演示了如何生成可滚动且不受其他更新影响的、可更新的结果集。

示例:

  1. public class DBConnTest {
  2. public static void main(String[] args) {
  3. //1. 加载驱动类
  4. try {
  5. Class.forName("com.mysql.jdbc.Driver");
  6. } catch (ClassNotFoundException e) {
  7. e.printStackTrace();
  8. }
  9. //2. 连接数据库
  10. Connection conn = null;
  11. try {
  12. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tripdb","root","root");
  13. } catch (SQLException e) {
  14. e.printStackTrace();
  15. }
  16. System.out.println(conn);
  17. // 3. 执行SQL语句
  18. String sql ="select cust_id,cust_name,cust_pwd, cust_gender,cust_telno, cust_email,cust_contury,cust_province,cust_city,cust_score,cust_level" +
  19. " from customer where cust_id = 101 and cust_status =0";
  20. Statement stmt = null; // Statement 接口:用于执行静态SQL语句
  21. try {
  22. stmt = conn.createStatement();
  23. } catch (SQLException e) {
  24. e.printStackTrace();
  25. }
  26. ResultSet rs = null;
  27. try {
  28. rs = stmt.executeQuery(sql);
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }
  32. //4. 获得执行结果
  33. //5. 处理结果
  34. try {
  35. while (rs.next()) {
  36. int custId = rs.getInt(1);
  37. String custName = rs.getString(2);
  38. String custPwd = rs.getString(3);
  39. String custGender = rs.getString(4);
  40. long custTelno = rs.getLong(5);
  41. String custEmail = rs.getString(6);
  42. String custCountry = rs.getString(7);
  43. String custProvince = rs.getString(8);
  44. String custCity = rs.getString(9);
  45. int custScore = rs.getInt(10);
  46. int custLevel = rs.getInt(11);
  47. Customer customer = new Customer();
  48. customer.setCustId(custId);
  49. customer.setCustName(custName);
  50. customer.setCustPwd(custPwd);
  51. customer.setCustGender(custGender);
  52. customer.setCustTelno(custTelno);
  53. customer.setCustEmail(custEmail);
  54. customer.setCustCountry(custCountry);
  55. customer.setCustProvince(custProvince);
  56. customer.setCustCity(custCity);
  57. customer.setCustScore(custScore);
  58. customer.setCustLevel(custLevel);
  59. System.out.println(customer);
  60. }
  61. }catch (SQLException e){
  62. }finally {
  63. //6. 关闭 连接
  64. if(rs!= null) {
  65. try {
  66. rs.close();
  67. } catch (SQLException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. if(stmt != null) {
  72. try {
  73. stmt.close();
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. if(conn != null) {
  79. try {
  80. conn.close();
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86. }
  87. }