本文實例講述了Java使用Statement接口執行SQL語句操作的方法。分享給大家供大家參考,具體如下:
Statement執行SQL語句:
1. 對數據庫的曾刪改操作時,使用stmt.executeUpdate(sql)
執行給定 SQL 語句,分別為 insert
、update
、delete
.
2. 對數據庫做查詢時,直接使用 stmt.executeQuery(sql)
,返回結果可以為一個resultSet結果集。
首先做一些準備工作:
①對要進行操作的數據庫表進行封裝,比如說我的數據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 添加構造函數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數據庫,和關閉連接方法進行封裝,這里用DbUtil.java進行封裝,具體如下:
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; }}
準備工作做好了,下面開始使用Statement接口執行sql語句來實現增刪改:
①增:
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(); //獲取數據庫連接 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(); //獲取數據庫連接 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, "[email protected]", "13411957776", 8000.00, "2015-09-10"); if(result==1){ System.out.println("添加成功"); }else{ System.out.println("添加失敗"); }*/ //多行注釋,ctrl+shift+/ AiMember mem = new AiMember("李娜", 6, 25, "[email protected]", "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(); // 獲取數據庫連接 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, "[email protected]", "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(); // 獲取數據庫連接 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程序設計有所幫助。
新聞熱點
疑難解答
圖片精選