標籤:語句 資料庫操作 一個 cep host 需要 api getc user
前言:
想必大家在實際編碼中都遇到過JDBC的操作, 這裡僅做自己的一個總結, 有錯誤和不完整之處還請大家提出來.
1,JDBC其實一套規範(介面)
資料庫廠商需要實現此介面(實作類別)--資料庫驅動
2,jdbc的作用:
可以和資料庫建立連結
發送sql語句
接收傳回值,處理結果
3,api詳解(java.sql或者javaX.sql)
DriverManager 類:
管理一組 JDBC 驅動程式的基本服務。
常用方法:
registerDriver(Driver):註冊驅動
查看 mysql的Driver的時候有下面一段代碼:
發現在類載入的時候已經註冊過驅動,我們以後只需要把Driver載入到記憶體即可
類.Class
對象.getClass()
Class.forName("全限定名(包名+類名)")
以後開發中我們通過Class.forName("com.mysql.jdbc.Driver")把驅動註冊進去即可.
static Connection getConnection(String url, String user, String password) :擷取連結
參數說明:
url:告訴jdbc去串連那個資料庫
固定格式: 協議:子協議:子協議名稱 參數
mysql: jdbc:mysql ://localhost:3306/databaseName
oracle: jdbc:oracle:thin :@localhost:1521:執行個體名
user:資料庫的登入名稱
password:資料庫的登入密碼
Connection 介面:
建立語句執行者:
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "root", "123456");
常用方法:
Statement createStatement():建立Statement -語句執行者
PreparedStatement prepareStatement(String sql) :建立一個先行編譯的語句執行對象
CallableStatement prepareCall(String sql) :(瞭解) 建立一個 CallableStatement 對象來調用資料庫預存程序。
Statement 接口(容易產生sql注入, 後期使用PreparedStatement. 後面會有blog說明這個問題)
sql語句執行者:
Statement st=conn.createStatement();
常用方法:
ResultSet executeQuery(String sql) :執行查詢語句,返回一個集合 ☆
int executeUpdate(String sql) :執行更新 插入 刪除語句,返回影響行數.☆
boolean execute(sql):執行給定的 SQL 陳述式,該語句可能返回多個結果。
若返回true ,執行是的查詢語句
調用 getResultSet 擷取查詢結果
若返回false,執行的是更新 插入 刪除語句
調用 getUpdateCount 擷取影響的行數
ResultSet 介面
返回的查詢結果:
String sql = "...";
ResultSet rs=st.executeQuery(sql);
常用方法:
boolean next() :判斷是否有下一條記錄,並且移動到下一行
擷取內容:
getXXX(參數)
參數的寫法:
1.欄位名稱 字串
2.第幾列 從1開始
getInt()
getString()
getObject()
4, 執行個體JDBCUtil類的書寫
(1)設定檔 jdbc.properties
drivername=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/ddatabaseNameuser=rootpassword=1234
(2)JDBCUtil.java
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.sql.Statement; 6 import java.util.ResourceBundle; 7 8 public class JDBCUtil { 9 //ctrl + shift + x 轉成大寫10 //ctrl + shift + y 轉成小寫11 static final String DRIVERNAME;12 static final String URL;13 static final String USERNAME;14 static final String PASSWORD;15 16 static{17 18 /*通過ResourceBundle 專門用來載入properties檔案19 ResourceBundle bundle=ResourceBundle.getBundle("檔案名稱");20 21 通過 bundle.getString(鍵的名字)22 String value=bundle.getString("url");23 */24 25 ResourceBundle bundle=ResourceBundle.getBundle("jdbc");26 DRIVERNAME=bundle.getString("drivername");27 URL=bundle.getString("url");28 USERNAME=bundle.getString("user");29 PASSWORD=bundle.getString("password");30 }31 32 static{33 try {34 Class.forName(DRIVERNAME);35 } catch (ClassNotFoundException e) {36 e.printStackTrace();37 }38 }39 40 //擷取連結41 public static Connection getConnection() throws SQLException{42 return DriverManager.getConnection(URL,USERNAME,PASSWORD);43 }44 45 //釋放資源46 public static void closeResource(Connection conn,Statement st,ResultSet rs){47 if (rs!=null) {48 try {49 rs.close();50 } catch (SQLException e) {51 e.printStackTrace();52 }53 }54 55 if (st!=null) {56 try {57 st.close();58 } catch (SQLException e) {59 e.printStackTrace();60 }61 }62 63 if (conn!=null) {64 try {65 conn.close();66 } catch (SQLException e) {67 e.printStackTrace();68 }69 }70 71 }72 }
(3)CRUDDemo, 使用PreparedStatement方式
1 public class PPCRUDDemo { 2 public static void main(String[] args) { 3 //插入 4 //insert("趙四","123","[email protected]"); 5 //更新 6 //updateByName("趙四","尼古拉斯.趙四"); 7 //擷取 8 //getByName("尼古拉斯.趙四"); 9 //刪除 10 deleteByName("尼古拉斯.趙四"); 11 } 12 13 private static void deleteByName(String string) { 14 //模版 15 Connection conn=null; 16 PreparedStatement st=null; 17 ResultSet rs=null; 18 19 try { 20 //擷取連結 21 conn=JDBCUtil.getConnection(); 22 //編寫sql 23 String sql="delete from user where username =?"; 24 //擷取先行編譯執行者 25 st=conn.prepareStatement(sql); 26 //設定參數 27 st.setString(1, string); 28 //執行sql 29 int i = st.executeUpdate(); 30 //處理結果 31 if (i>0) { 32 System.out.println("成功"); 33 }else{ 34 System.out.println("失敗"); 35 } 36 } catch (Exception e) { 37 e.printStackTrace(); 38 }finally{ 39 //釋放資源 40 JDBCUtil.closeResource(conn, st, rs); 41 } 42 43 } 44 45 private static void getByName(String string) { 46 47 Connection conn=null; 48 PreparedStatement st=null; 49 ResultSet rs=null; 50 51 try { 52 conn=JDBCUtil.getConnection(); 53 String sql="select * from user where username=? limit 1"; 54 st=conn.prepareStatement(sql); 55 56 st.setString(1, string); 57 rs=st.executeQuery(); 58 if (rs.next()) { 59 System.out.println(rs.getInt(1)+":"+rs.getString(2)+":"+rs.getObject(3)+":"+rs.getObject(4)); 60 } 61 } catch (Exception e) { 62 // TODO: handle exception 63 e.printStackTrace(); 64 }finally{ 65 66 JDBCUtil.closeResource(conn, st, rs); 67 } 68 69 } 70 71 private static void updateByName(String oldName, String newName) { 72 Connection conn=null; 73 PreparedStatement st=null; 74 ResultSet rs=null; 75 76 try { 77 conn=JDBCUtil.getConnection(); 78 String sql="update user set username = ? where username = ?"; 79 st=conn.prepareStatement(sql); 80 st.setString(1, newName); 81 st.setString(2, oldName); 82 83 int i=st.executeUpdate(); 84 //處理結果 85 if (i>0) { 86 System.out.println("成功"); 87 }else{ 88 System.out.println("失敗"); 89 } 90 91 } catch (Exception e) { 92 e.printStackTrace(); 93 }finally{ 94 JDBCUtil.closeResource(conn, st, rs); 95 } 96 97 98 } 99 100 private static void insert(String username, String password, String email) {101 Connection conn=null;102 PreparedStatement st=null;103 ResultSet rs=null;104 105 try {106 //擷取連結107 conn=JDBCUtil.getConnection();108 //編寫sql109 String sql="insert into user values(null,?,?,?)";110 //擷取先行編譯語句執行者111 st=conn.prepareStatement(sql);112 //設定參數113 st.setString(1, username);114 st.setString(2, password);115 st.setString(3, email);116 //執行sql117 int i=st.executeUpdate();118 //處理結果119 if (i>0) {120 System.out.println("成功");121 }else{122 System.out.println("失敗");123 }124 } catch (Exception e) {125 e.printStackTrace();126 }finally{127 JDBCUtil.closeResource(conn, st, rs);128 }129 }130 131 }
這裡沒有多麼高深的東西, 熟能生巧, 暫時總結的就這麼多.
[資料庫操作]Java中的JDBC的使用方法.