您好,登錄后才能下訂單哦!
本文實(shí)例講述了Java使用Statement接口執(zhí)行SQL語(yǔ)句操作的方法。分享給大家供大家參考,具體如下:
Statement執(zhí)行SQL語(yǔ)句:
1. 對(duì)數(shù)據(jù)庫(kù)的曾刪改操作時(shí),使用stmt.executeUpdate(sql)
執(zhí)行給定 SQL 語(yǔ)句,分別為 insert
、update
、delete
.
2. 對(duì)數(shù)據(jù)庫(kù)做查詢時(shí),直接使用 stmt.executeQuery(sql)
,返回結(jié)果可以為一個(gè)resultSet結(jié)果集。
首先做一些準(zhǔn)備工作:
①對(duì)要進(jìn)行操作的數(shù)據(jù)庫(kù)表進(jìn)行封裝,比如說(shuō)我的數(shù)據(jù)mydata中的aistu表,用AiMember.java進(jìn)行封裝,以便后面操作。具體如下:
package com.mysqltest.jdbc.model; /** * 定義一個(gè)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 添加構(gòu)造函數(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; } //重構(gòu) 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; } }
②對(duì)連接MySQL數(shù)據(jù)庫(kù),和關(guān)閉連接方法進(jìn)行封裝,這里用DbUtil.java進(jìn)行封裝,具體如下:
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; } }
準(zhǔn)備工作做好了,下面開始使用Statement接口執(zhí)行sql語(yǔ)句來(lái)實(shí)現(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ù)庫(kù)連接 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ù)庫(kù)連接 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ù)庫(kù)連接 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ù)庫(kù)連接 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("刪除成員失敗"); } } }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java+MySQL數(shù)據(jù)庫(kù)程序設(shè)計(jì)總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java文件與目錄操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。