1.注冊驅動:通知Java程式我們要連接的是哪個品牌的資料庫
2.獲取資料庫連接:Java行程和Mysql行程之間的通道開啟了
3.獲取資料庫操作物件:這個物件是用來執行sql陳述句的
4.執行SQL陳述句:對資料庫進行CRUD(增刪改查)操作
5.處理查詢結果集:如果第四步有select陳述句才有這一步
6.釋放資源:關閉所有資源
例1:不帶處理查詢結果集的JDBC
1 package com.xiaoma.JDBC; 2 3 import java.sql.*; 4 5 public class TestJDBC { 6 public static void main(String[] args) throws SQLException { 7 //1.注冊驅動 8 com.mysql.jdbc.Driver driver=new com.mysql.jdbc.Driver(); 9 DriverManager.registerDriver(driver); 10 //2.獲取資料庫連接物件 11 String url="jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8"; 12 String username="root"; 13 String password="123456"; 14 Connection conn=DriverManager.getConnection(url,username,password); 15 System.out.println(conn); 16 //3.獲取資料庫操作物件(通過一個Connection連接物件可以拿到多個操作物件) 17 Statement statement = conn.createStatement(); 18 //4.執行sql陳述句(這里以插入和洗掉為例,其余同理,前提是要會寫sql陳述句) 19 //String InsertSql="insert into people(name,age,address) values('小馬',18,'山東省')"; 20 String DeleteSql="delete from people where name='小馬'"; 21 //這個executeUpdate方法專門用來執行sql的增刪改查陳述句的,其中回傳值為影響力資料庫中的資料條數,也就是影響力幾行 22 int i = statement.executeUpdate(DeleteSql); 23 System.out.println(i); 24 //5.釋放資源,先釋放statement再釋放connection 25 if (statement != null) { 26 statement.close(); 27 } 28 if (conn != null){ 29 conn.close(); 30 } 31 } 32 }
例2:帶處理查詢結果集的JDBC
1 package com.xiaoma.JDBC; 2 3 import java.sql.*; 4 5 public class TestJDBC02 { 6 public static void main(String[] args) throws SQLException { 7 //1.注冊驅動 8 com.mysql.jdbc.Driver driver=new com.mysql.jdbc.Driver(); 9 DriverManager.registerDriver(driver); 10 11 //2.獲取資料庫連接物件 12 String url="jdbc:mysql://127.0.0.1:3306/jdbc?useUnicode=true&characterEncoding=utf-8"; 13 String username="root"; 14 String password="123456"; 15 Connection conn=DriverManager.getConnection(url,username,password); 16 System.out.println(conn); 17 18 //3.獲取資料庫操作物件(通過一個Connection連接物件可以拿到多個操作物件) 19 Statement statement = conn.createStatement(); 20 21 //4.執行sql陳述句(這里以插入和洗掉為例,其余同理,前提是要會寫sql陳述句) 22 String SeleteSql="select * from people"; 23 //這個executeUpdate方法專門用來執行sql的增刪改查陳述句的,其中回傳值為影響力資料庫中的資料條數,也就是影響力幾行 24 ResultSet resultSet = statement.executeQuery(SeleteSql); 25 26 //5.處理查詢結果集 27 while(resultSet.next()){ 28 //這個getString()方法是不管資料庫中的值是什么型別的,他都會以String型別回傳 29 //getString()是通過下標取值的,默認都是以字串型別取出,可以通過相應型別的get方法取出特定型別的值 30 System.out.println(resultSet.getString(1)); 31 System.out.println(resultSet.getString(2)); 32 System.out.println(resultSet.getString(3)); 33 System.out.println(resultSet.getString(4)); 34 } 35 36 //6.釋放資源,先釋放statement再釋放connection 37 if (statement != null) { 38 statement.close(); 39 } 40 if (conn != null){ 41 conn.close(); 42 } 43 if(resultSet!=null){ 44 resultSet.close(); 45 } 46 } 47 }
例3:將資料庫連接資訊寫到properties組態檔中,通過讀取properties組態檔連接資料庫,進行查詢(做開發基本上都用這種方式)
1 package com.xiaoma.JDBC; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.sql.*; 6 import java.util.Properties; 7 import java.util.ResourceBundle; 8 9 //讀取組態檔 10 public class TestJDBC03 { 11 public static void main(String[] args) throws SQLException, IOException { 12 //獲取組態檔資料 13 InputStream is=TestJDBC03.class.getClassLoader().getResourceAsStream("db.properties"); 14 Properties pro=new Properties(); 15 pro.load(is); 16 String username= pro.getProperty("username"); 17 String password=pro.getProperty("password"); 18 String url=pro.getProperty("url"); 19 20 //1.注冊驅動 21 com.mysql.jdbc.Driver driver=new com.mysql.jdbc.Driver(); 22 23 //2.獲取資料庫連接物件 24 25 Connection conn=DriverManager.getConnection(url,username,password); 26 System.out.println(conn); 27 28 //3.獲取資料庫操作物件(通過一個Connection連接物件可以拿到多個操作物件) 29 Statement statement = conn.createStatement(); 30 31 //4.執行sql陳述句(這里以插入和洗掉為例,其余同理,前提是要會寫sql陳述句) 32 String SeleteSql="select * from people"; 33 //這個executeUpdate方法專門用來執行sql的增刪改查陳述句的,其中回傳值為影響力資料庫中的資料條數,也就是影響力幾行 34 ResultSet resultSet = statement.executeQuery(SeleteSql); 35 36 //5.處理查詢結果集 37 while(resultSet.next()){ 38 //這個getString()方法是不管資料庫中的值是什么型別的,他都會以String型別回傳 39 //getString()是通過下標取值的,默認都是以字串型別取出,可以通過相應型別的get方法取出特定型別的值 40 System.out.println(resultSet.getString(1)); 41 System.out.println(resultSet.getString(2)); 42 System.out.println(resultSet.getString(3)); 43 System.out.println(resultSet.getString(4)); 44 } 45 46 //6.釋放資源,先釋放statement再釋放connection 47 if (statement != null) { 48 statement.close(); 49 } 50 if (conn != null){ 51 conn.close(); 52 } 53 if(resultSet!=null){ 54 resultSet.close(); 55 } 56 } 57 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/308209.html
標籤:Java
上一篇:實作動態封禁 IP,干死爬蟲!!
下一篇:Java中Session的詳解
