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