Java使用Statement接口執(zhí)行SQL語句操作實例分析
本文實例講述了Java使用Statement接口執(zhí)行SQL語句操作的方法。分享給大家供大家參考,具體如下:
Statement執(zhí)行SQL語句:
1. 對數(shù)據(jù)庫的曾刪改操作時,使用stmt.executeUpdate(sql)
執(zhí)行給定 SQL 語句,分別為 insert
、update
、delete
.
2. 對數(shù)據(jù)庫做查詢時,直接使用 stmt.executeQuery(sql)
,返回結(jié)果可以為一個resultSet結(jié)果集。
首先做一些準備工作:
①對要進行操作的數(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 添加構(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; } }
②對連接MySQL數(shù)據(jù)庫,和關(guān)閉連接方法進行封裝,這里用DbUtil.java進行封裝,具體如下:
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; } }
準備工作做好了,下面開始使用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("刪除成員失敗"); } } }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java+MySQL數(shù)據(jù)庫程序設計總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java文件與目錄操作技巧匯總》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
- Java使用PreparedStatement接口及ResultSet結(jié)果集的方法示例
- Java連接數(shù)據(jù)庫JDBC技術(shù)之prepareStatement的詳細介紹
- java中PreparedStatement和Statement詳細講解
- Java數(shù)據(jù)庫連接PreparedStatement的使用詳解
- java 中createStatement()方法的實例詳解
- Java的JDBC中Statement與CallableStatement對象實例
- 詳解Java的JDBC中Statement與PreparedStatement對象
- java中Statement 與 PreparedStatement接口之間的關(guān)系和區(qū)別
相關(guān)文章
Java打包之后讀取Resources下的文件失效原因及解決方法
這篇文章主要給大家介紹了Java打包之后讀取Resources下的文件失效的問題分析和解決方法,文中通過代碼示例和圖文結(jié)合給大家講解非常詳細,需要的朋友可以參考下2023-12-12Spring?Cloud?Eureka基礎(chǔ)應用及原理
這篇文章主要介紹了Spring?Cloud?Eureka基礎(chǔ)應用,Eureka?Client中內(nèi)置一個負載均衡器,用來進行基本的負載均衡,下面我們將通過搭建一個簡單的Eureka例子來了解Eureka的運作原理,感興趣的朋友一起看看吧2022-05-05@ConfigurationProperties綁定配置信息至Array、List、Map、Bean的實現(xiàn)
這篇文章主要介紹了@ConfigurationProperties綁定配置信息至Array、List、Map、Bean的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05Mybatis關(guān)聯(lián)查詢之一對多和多對一XML配置詳解
這篇文章主要介紹了Mybatis關(guān)聯(lián)查詢之一對多和多對一XML配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10Spring?Boot多數(shù)據(jù)源事務@DSTransactional的使用詳解
本文主要介紹了Spring?Boot多數(shù)據(jù)源事務@DSTransactional的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06