Java語言實現(xiàn)對MySql數(shù)據(jù)庫中數(shù)據(jù)的增刪改查操作的代碼
簡單說操作的步驟:
1.連接數(shù)據(jù)庫
2.將SQL語句發(fā)送到數(shù)據(jù)庫
3.執(zhí)行SQL語句
這里舉個例子:
在一個數(shù)據(jù)庫中有個students表,表中有學號(Id),姓名(Name),性別(Sex),地址(Address),電話(Phone),專業(yè)(Dept)。
這里把這個表寫成一個學生信息類(Info_student)
(請先確??戳死诱f明,不然代碼有的地方可能看不明白)
要實現(xiàn)操縱我們首先得連接數(shù)據(jù)庫,因為每個操作都要進行連接操作,所以我們直接把連接的操作封裝在一個類中,需要連接的時候直接調用可。
數(shù)據(jù)庫連接類:
import java.sql.Connection; import java.sql.DriverManager; public class DB_Helper { public static Connection connect = null; static { try { Class.forName("com.mysql.jdbc.Driver"); // 加載MYSQL JDBC驅動程序 // 觀察以下2個語句的差別, // connect = // DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", ""); connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/students?useUnicode=true&characterEncoding=utf-8", "root", ""); System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } } public static Connection getConnection() { return connect; } }
數(shù)據(jù)庫已經(jīng)連接了,那么接下來就是要發(fā)送SQL語句和執(zhí)行語句。
發(fā)送語句用到了PreparedStatement對象和Connection對象的操作prepareStatement()
執(zhí)行語句用到PreparedStatement對象的操作execute()
提示:以下是一些對象的說明,可以先看代碼,遇到的時候再回來看。
************************
PreparedStatement
表示預編譯的 SQL 語句的對象。
SQL 語句被預編譯并存儲在 PreparedStatement 對象中。然后可以使用此對象多次高效地執(zhí)行該語句。
*************************
Connection
與特定數(shù)據(jù)庫的連接(會話)。在連接上下文中執(zhí)行 SQL 語句并返回結果。
Connection 對象的數(shù)據(jù)庫能夠提供描述其表、所支持的 SQL 語法、存儲過程、此連接功能等等的信息。
**********************
以下代碼是要實現(xiàn)在數(shù)據(jù)庫中實現(xiàn)學生信息的增刪改查操作。
一、增
public void add(Info_student student) throws SQLException{ // 與特定數(shù)據(jù)庫的連接(會話)。 Connection conn = (Connection) DB_Helper.getConnection(); String sql = "insert into student(Sno,Sname,Ssex,Saddress,Sphone,Sdept) values(?,?,?,?,?,?)"; // 創(chuàng)建一個 PreparedStatement 對象來將參數(shù)化的 SQL 語句發(fā)送到數(shù)據(jù)庫。 PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); /* * void setBigDecimal(int parameterIndex,BigDecimal x)throws SQLException * 將指定參數(shù)設置為給定 Java String 值。在將此值發(fā)送給數(shù)據(jù)庫時,驅動程序將它轉換成一個 SQL VARCHAR * 或 LONGVARCHAR 值(取決于該參數(shù)相對于驅動程序在 VARCHAR 值上的限制的大小)。 */ ptmt.setString(1, student.getId()); ptmt.setString(2, student.getName()); ptmt.setString(3, student.getSex()); ptmt.setString(4, student.getAddress()); ptmt.setString(5, student.getPhone()); ptmt.setString(6, student.getDept()); // 在此 PreparedStatement 對象中執(zhí)行 SQL 語句 ptmt.execute(); }
二、刪
public void delete(String id) throws SQLException{ Connection conn = (Connection) DB_Helper.getConnection(); String sql = "delete from student where Sno=?"; PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); ptmt.setString(1, id); ptmt.execute(); }
三、改
public void update(Info_student student) throws SQLException{ Connection conn = (Connection) DB_Helper.getConnection(); String sql = "update student set Sname=?,Ssex=?,Saddress=?,Sphone=?,Sdept=? where Sno=?"; PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); ptmt.setString(1, student.getName()); ptmt.setString(2, student.getSex()); ptmt.setString(3, student.getAddress()); ptmt.setString(4, student.getPhone()); ptmt.setString(5, student.getDept()); ptmt.setString(6, student.getId()); ptmt.execute(); }
四、查
public Info_student search(String id) throws SQLException{ Info_student student = null; Connection conn = (Connection) DB_Helper.getConnection(); String sql = "select * from student where Sno=?"; PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); ptmt.setString(1, id); /* * ResultSet executeQuery()throws SQLException * 在此 PreparedStatement 對象中執(zhí)行 SQL 查詢,并返回該查詢生成的 ResultSet 對象。 */ /* * public interface ResultSet extends Wrapper * 表示數(shù)據(jù)庫結果集的數(shù)據(jù)表,通常通過執(zhí)行查詢數(shù)據(jù)庫的語句生成。 ResultSet 對象具有指向其當前數(shù)據(jù)行的光標。 * 最初,光標被置于第一行之前。next 方法將光標移動到下一行;因為該方法在 ResultSet 對象沒有下一行時 * 返回 false,所以可以在 while 循環(huán)中使用它來迭代結果集。 * */ ResultSet rs = ptmt.executeQuery(); /* * boolean next()throws SQLException * 將光標從當前位置向前移一行。 * ResultSet 光標最初位于第一行之前; * 第一次調用 next 方法使第一行成為當前行; * 第二次調用使第二行成為當前行,依此類推。 */ while(rs.next()){ student = new Info_student(); student.setId(rs.getString("Sno")); student.setName(rs.getString("Sname")); student.setSex(rs.getString("Ssex")); student.setAddress(rs.getString("Saddress")); student.setPhone(rs.getString("Sphone")); student.setDept(rs.getString("Sdept")); } return student; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java實戰(zhàn)在線選課系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個在線選課系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11IntelliJ IDEA修改內(nèi)存大小,使得idea運行更流暢
今天小編就為大家分享一篇關于IntelliJ IDEA修改內(nèi)存大小,使得idea運行更流暢的文章,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10SpringBoot中yml的數(shù)據(jù)綁定示例
本文主要介紹了SpringBoot中yml的數(shù)據(jù)綁定示例,借助于YAML的簡潔語法和結構化特性,我們能夠輕松地管理應用程序的配置信息,使得配置文件更加清晰易讀,感興趣的可以了解一下2023-11-11SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法
這篇文章主要介紹了SpringBoot使用Mybatis&Mybatis-plus文件映射配置方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05