本文實例講述了Java使用PreparedStatement接口及ResultSet結果集的方法。分享給大家供大家參考,具體如下:
說明:
1.PreparedStatement接口繼承Statement,它的實例包含已編譯的SQL語句,執行速度要快于Statement。
2.PreparedStatement繼承了Statement的所有功能,三種方法executeUpdate
、executeQuery
、execute
不再需要參數。
3.在JDBC應用中,一般都用PreparedStatement,而不是Statement。
便于操作,先做一些封裝:
對連接數據庫,關閉連接封裝,在之前博客中已經提到DbUtil.java;
對數據庫表進行封裝,這里是對我的數據庫中comp表進行操作,因此封裝如下:
package com.mysqltest.jdbc.modelComp;public class CompMember { private int id; private String name; private int age; private double salary; /** * 構造函數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; } /** * 重載構造函數 * @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接口實現增的操作:
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是子類,用父類關閉也行 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接口實現查詢,并運用ResultSet結果集:
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(); /** * 遍歷查詢結果 * @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();// 返回結果集 // 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("+====================================+"); } } /** * 遍歷查詢結果方法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();// 返回結果集 // 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();// 返回結果集 // 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); } }}
希望本文所述對大家java程序設計有所幫助。
新聞熱點
疑難解答
圖片精選