国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java使用PreparedStatement接口及ResultSet結果集的方法示例

2024-07-14 08:41:28
字體:
來源:轉載
供稿:網友

本文實例講述了Java使用PreparedStatement接口及ResultSet結果集的方法。分享給大家供大家參考,具體如下:

說明:

1.PreparedStatement接口繼承Statement,它的實例包含已編譯的SQL語句,執行速度要快于Statement。

2.PreparedStatement繼承了Statement的所有功能,三種方法executeUpdateexecuteQueryexecute不再需要參數。

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程序設計有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 舒兰市| 巫溪县| 扬中市| 大理市| 宣威市| 广昌县| 阜城县| 浪卡子县| 通化县| 乌鲁木齐县| 六安市| 巴青县| 堆龙德庆县| 商都县| 汨罗市| 博爱县| 睢宁县| 玛多县| 巨鹿县| 九寨沟县| 旬邑县| 喀喇沁旗| 东方市| 磐石市| 高密市| 峨边| 崇义县| 荔浦县| 德钦县| 任丘市| 丰城市| 白山市| 抚远县| 革吉县| 繁峙县| 宽甸| 穆棱市| 沁阳市| 遂宁市| 中西区| 阿合奇县|