JDBC簡介 JDBC全稱為:Java Data Base Connectivity (java資料庫連接),可以為多種資料庫提供填統一的訪問。JDBC是sun開發的一套資料庫訪問編程介面,是一種SQL級的API。它是由java語言編寫完成,所以具有很好的跨平臺特性,使用JDBC編寫的資料庫應用程式 ...
JDBC簡介
JDBC全稱為:Java Data Base Connectivity (java資料庫連接),可以為多種資料庫提供填統一的訪問。JDBC是sun開發的一套資料庫訪問編程介面,是一種SQL級的API。它是由java語言編寫完成,所以具有很好的跨平臺特性,使用JDBC編寫的資料庫應用程式可以在任何支持java的平臺上運行,而不必在不同的平臺上編寫不同的應用程式。
JDBC編程步驟
(1)載入驅動程式:
下載驅動包 : http://dev.mysql.com/downloads/connector/j/
解壓,得到 jar文件。將該文件複製到Java工程目錄Java Resources/Libraries/ 下,→ buildpath 。
(2)獲得資料庫連接
(3)創建Statement對象:
(4)向資料庫發送SQL命令
(5)處理資料庫的返回結果(ResultSet類)
1 package com.baidu.emp.jdbcTest; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 8 import com.mysql.jdbc.Driver; 9 /** 10 * 開始使用jdbc連接資料庫 11 * @author Admin 12 * 13 */ 14 public class Test001 { 15 16 public static void main(String[] args) throws Exception { 17 18 /** 19 * 載入驅動 20 */ 21 // 方法一: 22 /* 23 * import java.sql.DriverManager; import com.mysql.jdbc.Driver; 24 */ 25 // Driver driver = new Driver(); 26 // DriverManager.registerDriver(driver); 27 28 // 方法二:(推薦使用) 29 Class.forName("com.mysql.jdbc.Driver"); 30 31 /** 32 * 創建鏈接 33 */ 34 String url = "jdbc:mysql://localhost:3306/testjdbc"; 35 String user = "root"; 36 String password = "root"; 37 Connection connection = DriverManager.getConnection(url, user, password); 38 39 // 創建statement對象 40 Statement statement = connection.createStatement(); 41 42 /** 43 * 執行SQL,獲取結果集 44 */ 45 String sql = "select * from test01"; 46 ResultSet result = statement.executeQuery(sql); 47 48 // 遍歷結果集 49 while (result.next()) { 50 String name = result.getString("name"); 51 int id = result.getInt("id"); 52 System.out.println(name + "\t" + id); 53 } 54 55 /** 56 * 關閉鏈接,釋放資源 57 */ 58 result.close(); 59 statement.close(); 60 connection.close(); 61 } 62 }
防止SQL註入改用prepareStatement
1 package com.boya.emp.jdbcTest; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 /** 8 * SQL註入,使用prepareStatement對象進行預編譯 9 * @author Admin 10 * 11 */ 12 public class Test002 { 13 14 public static void main(String[] args) throws Exception { 15 16 /** 17 * 載入驅動 18 */ 19 Class.forName("com.mysql.jdbc.Driver"); 20 21 /** 22 * 創建鏈接 23 */ 24 String url = "jdbc:mysql://localhost:3306/testjdbc"; 25 String user = "root"; 26 String password = "root"; 27 Connection connection = DriverManager.getConnection(url, user, password); 28 29 // 寫SQL 30 String sql = "select * from test01 where id = ?"; 31 //創建statement對象,預編譯 32 PreparedStatement statement = connection.prepareStatement(sql); 33 //設置參數 34 statement.setInt(1, 2); 35 /** 36 * 執行SQL,獲取結果集 37 */ 38 ResultSet result = statement.executeQuery(); 39 40 // 遍歷結果集 41 while (result.next()) { 42 String name = result.getString("name"); 43 int id = result.getInt("id"); 44 System.out.println(name + "\t" + id); 45 } 46 47 /** 48 * 關閉鏈接,釋放資源 49 */ 50 result.close(); 51 statement.close(); 52 connection.close(); 53 } 54 }View Code
進行代碼優化,設置配置文件,工具類,實現增刪該查
增加配置文件方便修改資料庫,用戶登錄。。。
jdbc.properties(配置文件名)
1 driverName=com.mysql.jdbc.Driver 2 url=jdbc:mysql://localhost:3306/testjdbc 3 userName=root 4 password=root
註意寫配置文件時中間不可以有空格,引號之類的
工具類:增強了代碼的復用性
1 package com.baidu.emp.utils; 2 3 import java.io.InputStream; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.util.Properties; 10 11 import org.junit.Test; 12 13 14 15 public class JdbcUtils { 16 17 static String driverClassName; 18 static String url; 19 static String user; 20 static String password; 21 22 static { 23 // 創建配置文件對象 24 Properties properties = new Properties(); 25 // 載入配置文件輸入流 26 InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); 27 // 重新載入配置文件 28 try { 29 properties.load(inputStream); 30 // 獲取配置文件的值 31 driverClassName = properties.getProperty("driverName"); 32 url = properties.getProperty("url"); 33 user = properties.getProperty("userName"); 34 password = properties.getProperty("password"); 35 Class.forName(driverClassName); 36 37 } catch (Exception e) { 38 // 拋出異常 39 throw new RuntimeException(e); 40 } 41 } 42 43 /** 44 * 獲取連接 45 */ 46 @Test 47 public void testName() throws Exception { 48 49 System.out.println(driverClassName); 50 } 51 public static Connection getConnection() { 52 Connection connection = null; 53 try { 54 connection = DriverManager.getConnection(url, user, password); 55 } catch (SQLException e) { 56 // 拋出異常 57 throw new RuntimeException(e); 58 } 59 return connection; 60 } 61 62 /** 63 * 關閉鏈接,釋放資源 64 */ 65 public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) { 66 67 try { 68 if (resultSet != null) { 69 resultSet.close(); 70 } 71 resultSet = null; // 垃圾及時清除 72 //註意,不要弄成死迴圈 73 close(connection, statement); 74 } catch (SQLException e) { 75 throw new RuntimeException(e); 76 } 77 78 } 79 80 /** 81 * 增刪改釋放資源 82 */ 83 public static void close(Connection connection, PreparedStatement statement) { 84 85 try { 86 if (connection != null) { 87 connection.close(); 88 } 89 90 connection = null; 91 if (statement != null) { 92 statement.close(); 93 } 94 statement = null; 95 96 } catch (SQLException e) { 97 throw new RuntimeException(e); 98 } 99 100 } 101 102 }View Code
測試增刪改查:
1 package com.baidu.emp.jdbcTest; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 7 import org.junit.After; 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import com.baidu.emp.utils.JdbcUtils; 12 13 /** 14 * 使用jdbcUtils連接資料庫進行增刪改查 15 * 16 * @author Admin 17 * 18 */ 19 public class Test003 { 20 21 // 初始化值 22 Connection connection = null; 23 PreparedStatement statement = null; 24 ResultSet result = null; 25 26 @Before 27 public void start() throws Exception { 28 // 創建鏈接 29 connection = JdbcUtils.getConnection(); 30 System.out.println("創建鏈接"); 31 } 32 33 @After 34 public void end() throws Exception { 35 // 關閉鏈接 36 JdbcUtils.close(connection, statement, result); 37 System.out.println("關閉鏈接"); 38 } 39 40 /** 41 *插入數據 42 * @throws Exception 43 */ 44 @Test 45 public void add() throws Exception { 46 String sql = "insert into test01 values(null,?)"; 47 statement = connection.prepareStatement(sql); 48 statement.setString(1, "李四"); 49 int result = statement.executeUpdate(); 50 if (result!=0) { 51 System.out.println("添加成功"); 52 } 53 } 54 /** 55 * 刪除數據 56 * @throws Exception 57 */ 58 @Test 59 public void del() throws Exception { 60 String sql = "delete from test01 where id =?"; 61 statement = connection.prepareStatement(sql); 62 statement.setInt(1,3); 63 int result = statement.executeUpdate(); 64 if (result!=0) { 65 System.out.println("刪除成功"); 66 } 67 } 68 /** 69 * 修改數據 70 * @throws Exception 71 */ 72 @Test 73 public void change() throws Exception { 74 String sql = "update test01 set name = ? where id = ?"; 75 statement = connection.prepareStatement(sql); 76 statement.setString(1, "張飛"); 77 statement.setInt(2, 2); 78 int result = statement.executeUpdate(); 79 if (result!=0) { 80 System.out.println("修改成功"); 81 } 82 } 83 84 /** 85 * 查詢全部數據 86 * @throws Exception 87 */ 88 @Test 89 public void findAll() throws Exception { 90 String sql = "select id , name from test01"; 91 statement = connection.prepareStatement(sql); 92 result = statement.executeQuery(); 93 if (result.next()) { 94 System.out.println("查詢成功"); 95 } 96 } 97 98 /** 99 * 條件查詢數據 100 * @throws Exception 101 */ 102 @Test 103 public void findOne() throws Exception { 104 String sql = "select id , name from test01 where id = ?"; 105 statement = connection.prepareStatement(sql); 106 statement.setInt(1, 2); 107 result = statement.executeQuery(); 108 if (result.next()) { 109 System.out.println("查詢成功"); 110 } 111 } 112 113 }
存在錯誤望各位同仁指出,非常感謝