Java使用PrepareStatement實現(xiàn)數(shù)據(jù)的插入與查詢操作
一、使用PrepareStatement實現(xiàn)插入數(shù)據(jù)的操作
public class PreparedStatementUpdateTest { @Test public void testInsert() { Connection connection = null; PreparedStatement ps = null; try { //1.獲取鏈接 connection = JDBCUtils.getConnection(); //2.預編譯sql語句,返回PreparedStatement實例 String sql = "insert into customers(name,email,birth) value(?,?,?)"; ps = connection.prepareStatement(sql); //3.填充占位符 ps.setString(1,"哪吒"); ps.setString(2,"nezha@gmail.com"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date parse = sdf.parse("2001-03-21"); long time = parse.getTime(); //這個Date是sql中的Date ps.setDate(3,new Date(time)); //4.執(zhí)行操作 ps.execute(); } catch (Exception e) { e.printStackTrace(); } //5.資源釋放 JDBCUtils.closeConnection(connection,ps); } }
二、使用PrepareStatement實現(xiàn)查詢數(shù)據(jù)的操作
public class CustomerForQuery { public Customer commonQueryForCustomer(String sql,Object ...obj){ Connection conn = null; PreparedStatement ps = null; ResultSet resultSet = null; try { //1.獲取連接 conn = JDBCUtils.getConnection(); //2.預編譯sql語句,返回PreparedStatement實例 ps = conn.prepareStatement(sql); //3.填充占位符 for (int i = 0; i < obj.length; i++) { ps.setObject(i+1,obj[i]); } //4.執(zhí)行查詢操作 resultSet = ps.executeQuery(); //5.獲取描述結果集的對象 ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); //6.處理結果集 if(resultSet.next()){ Customer customer = new Customer(); for (int i = 0; i < columnCount; i++) { Object object = resultSet.getObject(i + 1); String columnName = metaData.getColumnName(i + 1); Field declaredField = Customer.class.getDeclaredField(columnName); declaredField.setAccessible(true); declaredField.set(customer,object); } return customer; } } catch (Exception e) { e.printStackTrace(); }finally { //7.關閉資源 JDBCUtils.closeConnection(conn,ps,resultSet); } return null; } @Test public void test(){ String sql = "select name,email from customers where id=?"; Customer customer = commonQueryForCustomer(sql, 2); System.out.println(customer); } }
用到的bean類:
public class Customer { private int id; private String name; private String email; private Date birth; public Customer() { } public Customer(int id, String name, String email, Date birth) { this.id = id; this.name = name; this.email = email; this.birth = birth; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Customer{" + "id=" + id + ", name='" + name + '\'' + ", email='" + email + '\'' + ", birth=" + birth + '}'; } }
三、ResultSet和ResultSetMetaData
ResultSet:
1.查詢需要調用PreparedStatement 的 executeQuery() 方法,查詢結果是一個ResultSet 對象
2.ResultSet 對象以邏輯表格的形式封裝了執(zhí)行數(shù)據(jù)庫操作的結果集,ResultSet 接口由數(shù)據(jù)庫廠商提供實現(xiàn)
3.ResultSet 返回的實際上就是一張數(shù)據(jù)表。有一個指針指向數(shù)據(jù)表的第一條記錄的前面ResultSet 對象維護了一個指向當前數(shù)據(jù)行的游標,初始的時候,游標在第一行之前,可以通過 ResultSet 對象 的 next() 方法移動到下一行。調用 next()方法檢測下一行是否有效。若有效,該方法返回 true,且指針下移。 相當于Iterator對象的 hasNext() 和 next() 方法的結合體。 當指針指向一行時, 可以通過調用 getXxx(int index) 或 getXxx(int columnName) 獲取每一列的值。 例如: getInt(1), getString("name")
注意:Java與數(shù)據(jù)庫交互涉及到的相關Java API中的索引都從1開始
ResultSetMetaData:
1.可用于獲取關于ResultSet對象中列的類型和屬性信息的對象
2.通過ResultSet的getMetaData()方法獲取
3.常用方法
- getColumnName(int column)方法,獲取指定列的名稱
- getColumnLabel(int column)方法,獲取指定列的別名,如果不指定別名則返回名稱
- getColumnCount()方法,返回當前 ResultSet 對象中的列數(shù)
四、資源釋放
釋放ResultSet, Statement,Connection。
數(shù)據(jù)庫連接(Connection)是非常稀有的資源,用完后必須馬上釋放,如果Connection不能及時正確的關閉將導致系統(tǒng)宕機
Connection的使用原則是盡量晚創(chuàng)建,盡量早的釋放。 可以在finally中關閉,保證及時其他代碼出現(xiàn)異常,資源也一定能被關閉。
到此這篇關于Java使用PrepareStatement實現(xiàn)數(shù)據(jù)的插入與查詢操作的文章就介紹到這了,更多相關Java PrepareStatement內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot jasypt2.x與jasypt3.x的使用方式
在軟件開發(fā)中,將配置文件中的敏感信息(如數(shù)據(jù)庫密碼)進行加密是保障安全的有效手段,jasypt框架提供了這一功能,支持通過加密工具類或命令行工具生成密文,并通過修改配置文件和啟動參數(shù)的方式使用密文和密鑰,這樣即便配置文件被泄露2024-09-09maven多profile 打包下 -P參和-D參數(shù)的實現(xiàn)
這篇文章主要介紹了maven多profile 打包下 -P參和-D參數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11SpringBoot中實現(xiàn)啟動任務的實現(xiàn)步驟
這篇文章主要介紹了SpringBoot中實現(xiàn)啟動任務的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09