1. 程式人生 > 其它 >14-MySQL-資料庫驅動、JDBC 、 第一個JDBC程式

14-MySQL-資料庫驅動、JDBC 、 第一個JDBC程式

技術標籤:MySQL資料庫mysqljdbc

MySQL -> 資料庫驅動、JDBC 、 第一個JDBC程式

1. 資料庫驅動

  • 驅動:音效卡、顯示卡、資料庫
  • 程式會通過資料庫驅動,和資料庫打交道

2. JDBC

  • SUN公司為了簡化開發人員(對資料庫的統一)操作,提供了一個(Java操作資料庫的)規範,俗稱JDBC,這些規範的實現由具體的廠商做
  • 對於開發人員來說,只需要掌握JDBC介面的操作即可
  • java.sql
  • javax.sql
  • 還需匯入一個數據庫驅動包 mysql-connector-java-5.1.47.jar

3. 第一個JDBC程式

建立測試資料庫

CREATE DATABASE
jdbcStudy CHARACTER SET utf8 COLLATE utf8_general_ci; USE jdbcStudy; CREATE TABLE users( `id` INT PRIMARY KEY, `name` VARCHAR(40), `password` VARCHAR(40), `email` VARCHAR(60), `birthday` DATE ); INSERT INTO users(`id`,`name`,`password`,`email`,`birthday`) VALUES(1,'zhangsan','123456'
,'[email protected]','1999-12-4'), (2,'lisi','123456','[email protected]','1999-11-15'), (3,'wangwu','123456','[email protected]','1998-10-4');

編寫測試程式碼

import java.sql.*;

public class JdbcFirstDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1. 載入驅動
Class.forName("com.mysql.jdbc.Driver"); //固定寫法,載入驅動 //2.使用者資訊和url // 三個引數:useUnicode=true&characterEncoding=utf8&useSSL=true String url ="jdbc:mysql://localhost:3306/jdbcStudy?useUnicode=true&characterEncoding=utf8&useSSL=true"; String username = "root"; String password = "123456"; //3.連結成功,資料庫物件 connection代表資料庫 Connection connection = DriverManager.getConnection(url, username, password); //4.執行SQL的物件 Statement statement = connection.createStatement(); //5.執行SQL的物件 去 執行SQL,可能存在結果,檢視返回結果 String sql = "SELECT * FROM `users`"; ResultSet resultSet = statement.executeQuery(sql);//返回的結果集,封裝了全部查詢出來的結果 while (resultSet.next()){ System.out.println("id="+resultSet.getObject("id")); System.out.println("name="+resultSet.getObject("name")); System.out.println("password="+resultSet.getObject("password")); System.out.println("email="+resultSet.getObject("email")); System.out.println("birthday="+resultSet.getObject("birthday")); } //6.釋放連結 resultSet.close(); statement.close(); connection.close(); } }

步驟總結:

  • 1、載入驅動;
  • 2、使用者資訊和url;
  • 3、連線資料庫DriverManager;
  • 4、獲得執行sql的物件;
  • 5、獲得返回的結果集;
  • 6、釋放連線。

JDBC中的物件解釋

  • DriverManager
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName("com.mysql.jdbc.Driver"); //固定寫法,載入驅動
  • URL
String url ="jdbc:mysql://localhost:3306/jdbcStudy?useUnicode=true&characterEncoding=utf8&useSSL=true";

//mysql預設埠號:3306
//jdbc:mysql://主機地址:埠號/資料庫名?引數1&引數2&引數3

//oracle預設埠號:1521
//jdbc:oracle:thin:@localhost:1521:sid
  • Connection
Connection connection = DriverManager.getConnection(url, username, password);
//connection 代表資料庫

connection.rollback();
connection.commit();
connection.setAutoCommit();
  • Statement 執行SQL的物件; PrepareStatement也是執行SQL的物件
//執行SQL的物件
String sql = "SELECT * FROM `users`"; //編寫sql

Statement statement = connection.createStatement();

statement.executeQuery(); //查詢操作,返回ResultSet
statement.execute();  //執行任何sql
statement.executeUpdate(); //更新、插入、刪除,都是用這個,返回一個受影響的行數
  • ResultSet 查詢的結果集,封裝了所有的查詢結果

獲得指定的資料型別:

resultSet.getObject(); //在不知道列型別的情況下使用
//如果知道列的型別就使用指定的型別
resultSet.getString(); 
resultSet.getInt();
resultSet.getFloat();
resultSet.getDate();
//...

遍歷:指標

resultSet.beforeFirst();//移動到最前面
resultSet.afterLast();//移動到最後面
resultSet.next();//移動到下一個位置
resultSet.previous();//移動到前一行
resultSet.absolute(row);//移動到指定行
  • 釋放資源
resultSet.close();
statement.close();
connection.close();