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

首頁 > 開發(fā) > Java > 正文

Java使用Statement接口執(zhí)行SQL語句操作實例分析

2024-07-14 08:41:28
字體:
供稿:網(wǎng)友

本文實例講述了Java使用Statement接口執(zhí)行SQL語句操作的方法。分享給大家供大家參考,具體如下:

Statement執(zhí)行SQL語句:

1. 對數(shù)據(jù)庫的曾刪改操作時,使用stmt.executeUpdate(sql)  執(zhí)行給定 SQL 語句,分別為 insertupdatedelete.

2. 對數(shù)據(jù)庫做查詢時,直接使用 stmt.executeQuery(sql),返回結果可以為一個resultSet結果集。

首先做一些準備工作:

①對要進行操作的數(shù)據(jù)庫表進行封裝,比如說我的數(shù)據(jù)mydata中的aistu表,用AiMember.java進行封裝,以便后面操作。具體如下:

package com.mysqltest.jdbc.model;/** * 定義一個model * 成員模型 * @author AI_STU * */public class AiMember {  private String name;  private int id;  private int age;  private String email;  private String tel;  private double salary;  private String riqi;  /**   * alt+shift+s 添加構造函數(shù)generating constructor using fields.   * @param name   * @param id   * @param age   * @param email   * @param tel   * @param salary   * @param riqi   */  public AiMember(String name, int id, int age, String email, String tel, double salary, String riqi) {    super();    this.name = name;    this.id = id;    this.age = age;    this.email = email;    this.tel = tel;    this.salary = salary;    this.riqi = riqi;  }  //重構  public AiMember(int id) {    super();    this.id = id;  }  public String getName() {    return name;  }  public void setName(String name) {    this.name = name;  }  public int getId() {    return id;  }  public void setId(int id) {    this.id = id;  }  public int getAge() {    return age;  }  public void setAge(int age) {    this.age = age;  }  public String getEmail() {    return email;  }  public void setEmail(String email) {    this.email = email;  }  public String getTel() {    return tel;  }  public void setTel(String tel) {    this.tel = tel;  }  public double getSalary() {    return salary;  }  public void setSalary(double salary) {    this.salary = salary;  }  public String getRiqi() {    return riqi;  }  public void setRiqi(String riqi) {    this.riqi = riqi;  }}

②對連接MySQL數(shù)據(jù)庫,和關閉連接方法進行封裝,這里用DbUtil.java進行封裝,具體如下:

package com.mysqltest.jdbc.modelComp;public class CompMember {  private int id;  private String name;  private int age;  private double salary;  /**   * 構造函數(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;  }  /**   * 重載構造函數(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;  }}

準備工作做好了,下面開始使用Statement接口執(zhí)行sql語句來實現(xiàn)增刪改:

①增:

package com.mysqltest.jdbc.two2;import java.sql.Connection;import java.sql.Statement;import com.mysqltest.jdbc.model.AiMember;import com.mysqltest.jdbc.util.DbUtil;public class Demo3 {  /**   * 添加成員到表中1   * @param name   * @param id   * @param age   * @param email   * @param tel   * @param salary   * @param riqi   * @return   * @throws Exception   */  @SuppressWarnings("unused")  private static int addMember(String name,int id,int age,String email,String tel,double salary,String riqi) throws Exception{    DbUtil dbUtil = new DbUtil();//之前封裝好的    Connection con = dbUtil.getCon(); //獲取數(shù)據(jù)庫連接    String sql = "insert into aistu values('"+name+"',"+id+",'"+age+"','"+email+"','"+tel+"','"+salary+"','"+riqi+"')";    Statement stmt = con.createStatement();//獲取statement    int result = stmt.executeUpdate(sql);    dbUtil.close(stmt, con);    return result;  }  /**   * 添加成員到表中2方法   * @param mem   * @return   * @throws Exception   */  private static int addMember2(AiMember mem) throws Exception{ //AiMember也是之前封裝好的//    mem.getName();    DbUtil dbUtil = new DbUtil();//之前封裝好的    Connection con = dbUtil.getCon(); //獲取數(shù)據(jù)庫連接    String sql = "insert into aistu values('"+mem.getName()+"',"+mem.getId()+",'"+mem.getAge()+"','"+mem.getEmail()+"','"+mem.getTel()+"','"+mem.getSalary()+"','"+mem.getRiqi()+"')";    Statement stmt = con.createStatement();//獲取statement    int result = stmt.executeUpdate(sql);    dbUtil.close(stmt, con);    return result;  }//  private static int addMenber2()  public static void main(String[] args) throws Exception {    /*int result = addMember("劉翔", 4, 28, "15xliu@stu.edu.cn", "13411957776", 8000.00, "2015-09-10");    if(result==1){      System.out.println("添加成功");    }else{      System.out.println("添加失敗");    }*/ //多行注釋,ctrl+shift+/    AiMember mem = new AiMember("李娜", 6, 25, "15nli@stu.edu.cn", "13411957775", 8000.00, "2015-09-03");    int result = addMember2(mem);    if(result==1){      System.out.println("添加成功");    }else{      System.out.println("添加失敗");    }  }}

②改:

package com.mysqltest.jdbc.two3;import java.sql.Connection;import java.sql.Statement;import com.mysqltest.jdbc.model.AiMember;import com.mysqltest.jdbc.util.DbUtil;public class Demo4 {  private static DbUtil dbUtil = new DbUtil();//  @SuppressWarnings("unused")  /**   * 修改成員   * @param mem   * @return   * @throws Exception   */  private static int updateMember(AiMember mem) throws Exception {    Connection con = dbUtil.getCon(); // 獲取數(shù)據(jù)庫連接    String sql = "update aistu set name='" + mem.getName() + "',id=" + mem.getId() + ",age='" + mem.getAge()        + "',email='" + mem.getEmail() + "',tel='" + mem.getTel() + "',salary='" + mem.getSalary() + "',riqi='"        + mem.getRiqi() + "' where id=" + mem.getId();    //格式化,ctrl+a全選,然后ctrl+shift+f格式化    Statement stmt = con.createStatement();// 獲取statement    int result = stmt.executeUpdate(sql);    dbUtil.close(stmt, con);    return result;//    return 0;  }  public static void main(String[] args) throws Exception {    AiMember mem = new AiMember("勞爾", 6, 24, "14elao@stu.edu.cn", "13411957770", 18000.00, "2014-09-03");    int result = updateMember(mem);    if (result==1) {      System.out.println("更新成功");    } else {      System.out.println("更新失敗");    }  }}

③刪:

package com.mysqltest.jdbc.two4;import java.sql.Connection;import java.sql.Statement;import com.mysqltest.jdbc.model.AiMember;import com.mysqltest.jdbc.util.DbUtil;public class Demo5 {  private static DbUtil dbUtil = new DbUtil();  public static int deletMember(AiMember mem) throws Exception{    Connection con = dbUtil.getCon(); // 獲取數(shù)據(jù)庫連接    String sql = "delete from aistu where id="+mem.getId();    Statement stmt = con.createStatement();// 獲取statement    int result = stmt.executeUpdate(sql);    dbUtil.close(stmt, con);    return result;  }  public static void main(String[] args) throws Exception {    AiMember mem = new AiMember(5);    int result = deletMember(mem);    if (result==1) {      System.out.println("成功刪除成員");    } else {      System.out.println("刪除成員失敗");    }  }}

希望本文所述對大家java程序設計有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 江城| 怀仁县| 兴仁县| 含山县| 阿坝县| 蓬溪县| 仲巴县| 罗平县| 枣阳市| 泗阳县| 八宿县| 耒阳市| 娄烦县| 望江县| 连州市| 吐鲁番市| 厦门市| 麻江县| 遵义市| 隆林| 微博| 子长县| 六盘水市| 金沙县| 瓮安县| 都昌县| 岳普湖县| 广元市| 宿松县| 长宁县| 南和县| 麻栗坡县| 云安县| 宕昌县| 芜湖县| 福安市| 亚东县| 金山区| 清镇市| 郧西县| 全南县|