當(dāng)前位置:
首頁(yè) >
jdbc java数据库连接 3)Statement接口之执行DDL和DML语句的简化
發(fā)布時(shí)間:2023/12/20
24
豆豆
生活随笔
收集整理的這篇文章主要介紹了
jdbc java数据库连接 3)Statement接口之执行DDL和DML语句的简化
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
上一章的代碼中,可以發(fā)現(xiàn),jdbc執(zhí)行DDL和DML有幾個(gè)步驟都是一樣的:
?
1)執(zhí)行語(yǔ)句開(kāi)始時(shí),創(chuàng)建驅(qū)動(dòng)注冊(cè)對(duì)象、獲取連接的數(shù)據(jù)庫(kù)對(duì)象、創(chuàng)建Statement對(duì)象
1 // 創(chuàng)建驅(qū)動(dòng)注冊(cè)對(duì)象 2 Class.forName("com.mysql.jdbc.Driver"); 3 4 // 獲取連接的數(shù)據(jù)庫(kù)對(duì)象 5 Connection conn = DriverManager.getConnection(url, user, 6 password); 7 8 // 創(chuàng)建Statement對(duì)象 9 Statement stsm = conn.createStatement();
2)所有操作執(zhí)行完后,關(guān)閉連接(后來(lái)的先關(guān))
1 if (stsm != null) { 2 try { 3 stsm.close(); 4 } catch (SQLException e) { 5 // TODO Auto-generated catch block 6 e.printStackTrace(); 7 throw new RuntimeException(e); 8 } 9 } 10 if (conn != null) { 11 try { 12 conn.close(); 13 } catch (SQLException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 throw new RuntimeException(e); 17 }?
所以,我們可以把這三大部分湊起來(lái),建立一個(gè)jdbc的工具類:
jdbcutil
1 /** 2 * 這是jdbc執(zhí)行DDL和DML的工具類 3 * 4 * @author LZl 5 * 6 */ 7 public class Jdbcutil { 8 9 // 創(chuàng)建數(shù)據(jù)庫(kù)的基本信息 10 // 創(chuàng)建url 11 private static String url = "jdbc:mysql://localhost:3306/day1029?useUnicode=true&characterEncoding = GB2312 "; 12 // 數(shù)據(jù)庫(kù)的用戶名和密碼 13 private static String user = "root"; 14 private static String password = "root"; 15 public static Connection conn = null; 16 static Statement stsm = null; 17 18 /** 19 * 一:注冊(cè)的驅(qū)動(dòng)程序 獲取連接對(duì)象的方法 靜態(tài)代碼塊(好處是只需要加載一次,且隨著類的加載而加載) 20 */ 21 22 static { 23 try { 24 Class.forName("com.mysql.jdbc.Driver"); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 System.out.println("獲取數(shù)據(jù)庫(kù)連接對(duì)象出錯(cuò)"); 28 } 29 } 30 31 /** 32 * 二:獲取連接對(duì)象 該方法返回一個(gè)連接 33 */ 34 35 public static Connection getConnection() { 36 37 // 創(chuàng)建連接對(duì)象 38 try { 39 conn = DriverManager.getConnection(url, user, password); 40 } catch (SQLException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 throw new RuntimeException(e); 44 } 45 return conn; 46 47 } 48 49 /** 50 * 三:釋放資源,斷開(kāi)連接 參數(shù)列表:conn。stsm 51 */ 52 53 public static void close(Connection conn, Statement stsm) { 54 55 if (stsm != null) { 56 try { 57 stsm.close(); 58 } catch (SQLException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 throw new RuntimeException(e); 62 } 63 } 64 65 if (conn != null) { 66 try { 67 conn.close(); 68 } catch (SQLException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 throw new RuntimeException(e); 72 } 73 } 74 } 75 76 }
?
工具類再重載一個(gè)帶有3個(gè)參數(shù)的關(guān)閉連接的方法:
1 public static void close(Connection conn,Statement stmt,ResultSet rs){2 if(rs!=null)3 try {4 rs.close();5 } catch (SQLException e1) {6 e1.printStackTrace(); 7 throw new RuntimeException(e1); 8 } 9 if(stmt!=null){ 10 try { 11 stmt.close(); 12 } catch (SQLException e) { 13 e.printStackTrace(); 14 throw new RuntimeException(e); 15 } 16 } 17 if(conn!=null){ 18 try { 19 conn.close(); 20 } catch (SQLException e) { 21 e.printStackTrace(); 22 throw new RuntimeException(e); 23 } 24 } 25 }?
然后,1)jdbc使用DDL的方法要這樣:
1 public class UtilTest { 2 3 private static Connection conn = null; 4 // 創(chuàng)建Statement對(duì)象 5 private static Statement stsm; 6 7 // 執(zhí)行DDL語(yǔ)句(創(chuàng)建) 8 private static void DDL() { 9 10 try { 11 // 使用jdbc工具類來(lái)獲取連接對(duì)象 12 conn = Jdbcutil.getConnection(); 13 14 // 準(zhǔn)備sql語(yǔ)句 15 String sql = "CREATE TABLE person(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(10),sex VARCHAR(5),age INT,psot VARCHAR(10),email VARCHAR(20),phone INT)"; 16 17 stsm = conn.createStatement(); 18 19 // 發(fā)送sql語(yǔ)句 20 int result = stsm.executeUpdate(sql); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 throw new RuntimeException(e); 24 } finally { 25 // 調(diào)用工具類的方法,關(guān)閉連接 26 Jdbcutil.close(conn, stsm); 27 } 28 } 29 public static void main(String[] args) { 30 DDL(); 31 } 32 }
?
?
?
?
2)執(zhí)行DML語(yǔ)句:
1 //創(chuàng)建驅(qū)動(dòng)注冊(cè)對(duì)象 2 private static Connection conn = null; 3 // 創(chuàng)建Statement對(duì)象 4 private static Statement stsm = null; 5 6 // 執(zhí)行DML語(yǔ)句(插入) 7 private static void DML() { 8 9 try { 10 // 使用工具類獲取連接對(duì)象 11 conn = Jdbcutil.getConnection(); 12 13 // 準(zhǔn)備sql語(yǔ)句 14 String sql = "INSERT INTO person (NAME,sex,age) VALUES ('張三','男',20);"; 15 16 // 創(chuàng)建statement對(duì)象 17 stsm = conn.createStatement(); 18 19 // 執(zhí)行sql語(yǔ)句 20 int result = stsm.executeUpdate(sql); 21 System.out.println("影響了" + result + "行"); 22 23 } catch (Exception e) { 24 e.printStackTrace(); 25 throw new RuntimeException(e); 26 } finally { 27 // 調(diào)用工具類關(guān)閉連接 28 Jdbcutil.close(conn, stsm); 29 } 30 }?
3)執(zhí)行DQL語(yǔ)句:
1 // 創(chuàng)建驅(qū)動(dòng)注冊(cè)對(duì)象 2 private static Connection conn = null; 3 // 創(chuàng)建Statement對(duì)象 4 private static Statement stsm = null; 5 6 // 執(zhí)行DQL語(yǔ)句 7 private static void DQL() { 8 9 try { 10 11 // 調(diào)用工具類連接對(duì)象 12 conn = Jdbcutil.getConnection(); 13 14 // 創(chuàng)建statement對(duì)象 15 stsm = conn.createStatement(); 16 17 // 準(zhǔn)備sql語(yǔ)句 18 String sql = "SELECT * FROM person;"; 19 20 // 執(zhí)行sql語(yǔ)句,返回的是RrsultSet對(duì)象 21 ResultSet rs = stsm.executeQuery(sql); 22 23 // 查看第二行數(shù)據(jù) 24 25 // 移動(dòng)光標(biāo) 26 rs.next(); 27 rs.next(); 28 // 使用列名來(lái)查看 29 int id = rs.getInt("id"); 30 String name = rs.getString("name"); 31 String sex = rs.getString("sex"); 32 System.out.println(id + "," + name + "," + sex); 33 34 } catch (Exception e) { 35 e.printStackTrace(); 36 throw new RuntimeException(e); 37 } finally { 38 // 調(diào)用工具類關(guān)閉連接,這里要多關(guān)閉一個(gè)連接:ResultSet,工具類的關(guān)閉方法要添加它 39 Jdbcutil.close(conn, stsm,rs); 40 } 41 42 }?
?
轉(zhuǎn)載于:https://www.cnblogs.com/LZL-student/p/6012715.html
總結(jié)
以上是生活随笔為你收集整理的jdbc java数据库连接 3)Statement接口之执行DDL和DML语句的简化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PHP base64
- 下一篇: MongoDB的查询操作