生活随笔
收集整理的這篇文章主要介紹了
Java 数据库基本操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
連接數據庫
要訪問數據庫,首先要加載數據庫的驅動程序(只需要在第一次訪問數據庫時加載一次),然后每次訪問數據時創建一個Connection對象,之后執行操作數據庫的SQL語句,最后在完成數據庫操作后銷毀前面創建的Connection對象,釋放與數據庫的連接。
import java.sql.*; //導入java.sql包public class Conn { // 創建類ConnConnection con; // 聲明Connection對象public Connection getConnection() { // 建立返回值為Connection的方法try { // 加載數據庫驅動類Class.forName("com.mysql.jdbc.Driver");System.out.println("數據庫驅動加載成功");} catch (ClassNotFoundException e) {e.printStackTrace();}try { // 通過訪問數據庫的URL獲取數據庫連接對象con = DriverManager.getConnection("jdbc:mysql:"+ "//127.0.0.1:3306/test", "root", "123456");System.out.println("數據庫連接成功");} catch (SQLException e) {e.printStackTrace();}return con; // 按方法要求返回一個Connection對象}public static void main(String[] args) { // 主方法Conn c = new Conn(); // 創建本類對象c.getConnection(); // 調用連接數據庫的方法}
}
向數據庫發送SQL語句
getConnection()方法只是獲取與數據庫的連接,要執行SQL語句首先要獲得Statement類對象,通過創建的連接數據庫對象的createStatement()方法可獲得Statement對象。
處理查詢結果集
有了Statement對象以后,可調用相應的方法實現對數據庫的查詢和修改,并將查詢的結果集存放在ResultSet類的對象中。
順序查詢
ResultSet類的next()方法的返回值是boolean類型的數據,當游標移動到最后一行之后會返回false。
import java.sql.*;public class Gradation { // 創建類static Connection con; // 聲明Connection對象static Statement sql; // 聲明Statement對象static ResultSet res; // 聲明ResultSet對象public Connection getConnection() { // 連接數據庫方法try {Class.forName("com.mysql.jdbc.Driver");con = DriverManager.getConnection("jdbc:mysql:"+ "//127.0.0.1:3306/test", "root", "123456");} catch (Exception e) {e.printStackTrace();}return con; // 返回Connection對象}public static void main(String[] args) { // 主方法Gradation c = new Gradation(); // 創建本類對象con = c.getConnection(); // 與數據庫建立連接try {sql = con.createStatement(); // 實例化Statement對象// 執行SQL語句,返回結果集res = sql.executeQuery("select * from tb_stu");while (res.next()) { // 如果當前語句不是最后一條則進入循環String id = res.getString("id"); // 獲取列名是"id"的字段值// 獲取列名是"name"的字段值String name = res.getString("name");// 獲取列名是"sex"的字段值String sex = res.getString("sex");// 獲取列名是"birthday"的字段值String birthday = res.getString("birthday");System.out.print("編號:" + id); // 將列值輸出System.out.print(" 姓名:" + name);System.out.print(" 性別:" + sex);System.out.println(" 生日:" + birthday);}} catch (Exception e) {e.printStackTrace();}}
}
模糊查詢
SQL語句中提供了LIKE操作符用于模糊查詢,可使用“%”來代替0個或多個字符,使用下劃線“_”來代替一個字符。
import java.sql.*;public class Train { // 創建類Trainstatic Connection con; // 聲明Connection對象static Statement sql; // 聲明Statement對象static ResultSet res; // 聲明ResultSet對象public Connection getConnection() { // 與數據庫連接方法try {Class.forName("com.mysql.jdbc.Driver");con = DriverManager.getConnection("jdbc:mysql:"+ "//127.0.0.1:3306/test", "root", "123456");} catch (Exception e) {e.printStackTrace();}return con; // 返回Connection對象}public static void main(String[] args) { // 主方法Train c = new Train(); // 創建本類對象con = c.getConnection(); // 獲取與數據庫的連接try { // try語句捕捉異常sql = con.createStatement(); // 實例化Statement對象res = sql.executeQuery("select * from tb_stu where name like '張%'");// 執行SQL語句while (res.next()) { // 如果當前記錄不是結果集中的最后一條,進入循環體String id = res.getString(1); // 獲取id字段值String name = res.getString("name"); // 獲取name字段值String sex = res.getString("sex"); // 獲取sex字段值String birthday = res.getString("birthday"); // 獲取birthday字段值System.out.print("編號:" + id); // 輸出信息System.out.print(" 姓名:" + name);System.out.print(" 性別:" + sex);System.out.println(" 生日:" + birthday);}} catch (Exception e) { // 處理異常e.printStackTrace(); // 輸出異常信息}}
}
預處理語句
????????向數據庫發送一個SQL語句,數據庫中的SQL解釋器負責把SQL語句生成底層的內部命令,然后執行該命令,完成相關的數據操作。如果不斷地向數據庫提交SQL語句,肯定會增加數據庫中SQL解釋器的負擔,影響執行的速度。
????????對于JDBC,可以通過Connection對象的preparedStatement(String sql)方法對SQL語句進行預處理,生成數據庫底層的內部命令,并將該命令封裝在PreparedStatement對象中,通過調用該對象的相應方法執行底層數據庫命令。這樣應用程序能針對連接的數據庫實現將SQL語句解釋為數據庫底層的內部命令,然后讓數據庫執行這個命令,這樣可以減輕數據庫的負擔,提高訪問數據庫的速度。
import java.sql.*;
public class Prep { // 創建類Perpstatic Connection con; // 聲明Connection對象static PreparedStatement sql; // 聲明預處理對象static ResultSet res; // 聲明結果集對象public Connection getConnection() { // 與數據庫連接方法try {Class.forName("com.mysql.jdbc.Driver");con = DriverManager.getConnection("jdbc:mysql:"+ "//127.0.0.1:3306/test", "root", "123456");} catch (Exception e) {e.printStackTrace();}return con; // 返回Connection對象}public static void main(String[] args) { // 主方法Prep c = new Prep(); // 創建本類對象con = c.getConnection(); // 獲取與數據庫的連接try {// 實例化預處理對象sql = con.prepareStatement("select * from tb_stu"+ " where id = ?");sql.setInt(1, 4); // 設置參數res = sql.executeQuery(); // 執行預處理語句// 如果當前記錄不是結果集中最后一行,則進入循環體while (res.next()) {String id = res.getString(1); // 獲取結果集中第一列的值String name = res.getString("name"); // 獲取name列的列值String sex = res.getString("sex"); // 獲取sex列的列值// 獲取birthday列的列值String birthday = res.getString("birthday");System.out.print("編號:" + id); // 輸出信息System.out.print(" 姓名:" + name);System.out.print(" 性別:" + sex);System.out.println(" 生日:" + birthday);}} catch (Exception e) {e.printStackTrace();}}
}
添加、修改、刪除記錄
可以通過SQL語句對數據執行添加、修改和刪除操作。可通過PreparedStatement類的指定參數動態地對數據表中原有數據進行修改操作,并通過executeUpdate()方法執行更新語句。
import java.sql.*;public class Renewal { // 創建類static Connection con; // 聲明Connection對象static PreparedStatement sql; // 聲明PreparedStatement對象static ResultSet res; // 聲明ResultSet對象public Connection getConnection() {try {Class.forName("com.mysql.jdbc.Driver");con = DriverManager.getConnection("jdbc:mysql:"+ "//127.0.0.1:3306/test", "root", "123456");} catch (Exception e) {e.printStackTrace();}return con;}public static void main(String[] args) {Renewal c = new Renewal(); // 創建本類對象con = c.getConnection(); // 調用連接數據庫方法try {sql = con.prepareStatement("select * from tb_stu"); // 查詢數據庫res = sql.executeQuery(); // 執行SQL語句System.out.println("執行增加、修改、刪除前數據:");while (res.next()) {String id = res.getString(1);String name = res.getString("name");String sex = res.getString("sex");String birthday = res.getString("birthday"); // 遍歷查詢結果集System.out.print("編號:" + id);System.out.print(" 姓名:" + name);System.out.print(" 性別:" + sex);System.out.println(" 生日:" + birthday);}sql = con.prepareStatement("insert into tb_stu(name,sex,birthday) values(?,?,?)");sql.setString(1, "張一"); // 預處理添加數據sql.setString(2, "女");sql.setString(3, "2012-12-1");sql.executeUpdate();sql = con.prepareStatement("update tb_stu set birthday "+ "= ? where id = ? ");sql.setString(1, "2012-12-02"); // 更新數據sql.setInt(2, 1); // 更新數據sql.executeUpdate();Statement stmt = con.createStatement();stmt.executeUpdate("delete from tb_stu where id = 1");// 查詢修改數據后的tb_stu表中數據sql = con.prepareStatement("select * from tb_stu");res = sql.executeQuery(); // 執行SQL語句System.out.println("執行增加、修改、刪除后的數據:");while (res.next()) {String id = res.getString(1);String name = res.getString("name");String sex = res.getString("sex");String birthday = res.getString("birthday");System.out.print("編號:" + id);System.out.print(" 姓名:" + name);System.out.print(" 性別:" + sex);System.out.println(" 生日:" + birthday);}} catch (Exception e) {e.printStackTrace();}}
}
總結
以上是生活随笔為你收集整理的Java 数据库基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。