簡單說操作的步驟:
1.連接數據庫
2.將SQL語句發送到數據庫
3.執行SQL語句
這里舉個例子:
在一個數據庫中有個students表,表中有學號(Id),姓名(Name),性別(Sex),地址(Address),電話(Phone),專業(Dept)。
這里把這個表寫成一個學生信息類(Info_student)
(請先確保看了例子說明,不然代碼有的地方可能看不明白)
要實現操縱我們首先得連接數據庫,因為每個操作都要進行連接操作,所以我們直接把連接的操作封裝在一個類中,需要連接的時候直接調用可。
數據庫連接類:
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; } } 數據庫已經連接了,那么接下來就是要發送SQL語句和執行語句。
發送語句用到了PreparedStatement對象和Connection對象的操作prepareStatement()
執行語句用到PreparedStatement對象的操作execute()
提示:以下是一些對象的說明,可以先看代碼,遇到的時候再回來看。
************************
PreparedStatement
表示預編譯的 SQL 語句的對象。
SQL 語句被預編譯并存儲在 PreparedStatement 對象中。然后可以使用此對象多次高效地執行該語句。
*************************
Connection
與特定數據庫的連接(會話)。在連接上下文中執行 SQL 語句并返回結果。
Connection 對象的數據庫能夠提供描述其表、所支持的 SQL 語法、存儲過程、此連接功能等等的信息。
**********************
以下代碼是要實現在數據庫中實現學生信息的增刪改查操作。
一、增
public void add(Info_student student) throws SQLException{ // 與特定數據庫的連接(會話)。 Connection conn = (Connection) DB_Helper.getConnection(); String sql = "insert into student(Sno,Sname,Ssex,Saddress,Sphone,Sdept) values(?,?,?,?,?,?)"; // 創建一個 PreparedStatement 對象來將參數化的 SQL 語句發送到數據庫。 PreparedStatement ptmt = (PreparedStatement) conn.prepareStatement(sql); /* * void setBigDecimal(int parameterIndex,BigDecimal x)throws SQLException * 將指定參數設置為給定 Java String 值。在將此值發送給數據庫時,驅動程序將它轉換成一個 SQL VARCHAR * 或 LONGVARCHAR 值(取決于該參數相對于驅動程序在 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 對象中執行 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 對象中執行 SQL 查詢,并返回該查詢生成的 ResultSet 對象。 */ /* * public interface ResultSet extends Wrapper * 表示數據庫結果集的數據表,通常通過執行查詢數據庫的語句生成。 ResultSet 對象具有指向其當前數據行的光標。 * 最初,光標被置于第一行之前。next 方法將光標移動到下一行;因為該方法在 ResultSet 對象沒有下一行時 * 返回 false,所以可以在 while 循環中使用它來迭代結果集。 * */ 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; } 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答