您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)PreparedStatement接口怎么在Java中使用,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
具體如下:
說明:
1.PreparedStatement接口繼承Statement,它的實例包含已編譯的SQL語句,執(zhí)行速度要快于Statement。
2.PreparedStatement繼承了Statement的所有功能,三種方法executeUpdate
、executeQuery
、execute
不再需要參數(shù)。
3.在JDBC應(yīng)用中,一般都用PreparedStatement,而不是Statement。
便于操作,先做一些封裝:
對連接數(shù)據(jù)庫,關(guān)閉連接封裝,在之前博客中已經(jīng)提到DbUtil.java;
對數(shù)據(jù)庫表進行封裝,這里是對我的數(shù)據(jù)庫中comp表進行操作,因此封裝如下:
package com.mysqltest.jdbc.modelComp; public class CompMember { private int id; private String name; private int age; private double salary; /** * 構(gòu)造函數(shù)1 * @param name * @param age * @param salary */ public CompMember(String name, int age, double salary) { super(); this.name = name; this.age = age; this.salary = salary; } /** * 重載構(gòu)造函數(shù) * @param id * @param name * @param age * @param salary */ public CompMember(int id, String name, int age, double salary) { super(); this.id = id; this.name = name; this.age = age; this.salary = salary; } /** * get,set方法 */ 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } @Override /** * 改寫toString,使得顯示更好 */ public String toString() { return "["+this.id+"]"+this.name+","+this.age+","+this.salary; } }
然后利用PreparedStatement接口實現(xiàn)增的操作:
package com.mysqltest.jdbc.xiao1; import java.sql.Connection; import java.sql.PreparedStatement; import com.mysqltest.jdbc.modelComp.CompMember; import com.mysqltest.jdbc.util.DbUtil; public class PstatementTest { private static DbUtil dbUtil = new DbUtil(); /** * 用PreparedStatement添加成員 * @param mem * @return * @throws Exception */ private static int addMember(CompMember mem) throws Exception{ Connection con = dbUtil.getCon(); String sql = "insert into comp values(null,?,?,?)"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setString(1, mem.getName()); pstmt.setInt(2, mem.getAge()); pstmt.setDouble(3, mem.getSalary()); int result = pstmt.executeUpdate();//中間不用傳入sql dbUtil.close(pstmt, con); //preparedStatement是子類,用父類關(guān)閉也行 return result; } public static void main(String[] args) throws Exception { CompMember mem = new CompMember("劉翔", 24, 8000.00); int result = addMember(mem); if (result==1) { System.out.println("添加成功"); } else { System.out.println("添加失敗"); } } }
再利用PreparedStatement接口實現(xiàn)查詢,并運用ResultSet結(jié)果集:
package com.mysqltest.jdbc.xiao2; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import com.mysqltest.jdbc.modelComp.CompMember; import com.mysqltest.jdbc.util.DbUtil; public class ResultsetTest { private static DbUtil dbUtil = new DbUtil(); /** * 遍歷查詢結(jié)果 * @throws Exception */ @SuppressWarnings("unused") private static void listMem1() throws Exception { Connection con = dbUtil.getCon();// 獲取連接 String sql = "select * from comp"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery();// 返回結(jié)果集 // next()將光標向后一行 while (rs.next()) { int id = rs.getInt(1);// 獲取第一列的值id String name = rs.getString(2);// int age = rs.getInt(3); double salary = rs.getDouble(4); System.out.println("編號:" + id + "姓名:" + name + "年齡:" + age + "工資:" + salary); System.out.println("+====================================+"); } } /** * 遍歷查詢結(jié)果方法2 * @throws Exception */ @SuppressWarnings("unused") private static void listMem2() throws Exception { Connection con = dbUtil.getCon();// 獲取連接 String sql = "select * from comp"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery();// 返回結(jié)果集 // next()將光標向后一行 while (rs.next()) { int id = rs.getInt("id");// 獲取第一列的值id String name = rs.getString("name");// int age = rs.getInt("age"); double salary = rs.getDouble("salary"); System.out.println("編號:" + id + "姓名:" + name + "年齡:" + age + "工資:" + salary); System.out.println("+====================================+"); } } private static List<CompMember> listMem3() throws Exception{ List<CompMember> memList = new ArrayList<CompMember>(); Connection con = dbUtil.getCon();// 獲取連接 String sql = "select * from comp"; PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery();// 返回結(jié)果集 // next()將光標向后一行 while (rs.next()) { int id = rs.getInt("id");// 獲取第一列的值id String name = rs.getString("name");// int age = rs.getInt("age"); double salary = rs.getDouble("salary"); CompMember mem = new CompMember(id, name, age, salary); memList.add(mem);//添加到List中 } return memList; } public static void main(String[] args) throws Exception { // listMem1(); // listMem2(); List<CompMember> memList = listMem3(); for (CompMember mem : memList) { //遍歷集合的每個元素 System.out.println(mem); } } }
以上就是PreparedStatement接口怎么在Java中使用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。