簡(jiǎn)介:
Java是一種面向?qū)ο?,安全靈活的編程語(yǔ)言,已經(jīng)成為了世界上最流行的編程語(yǔ)言之一。Java語(yǔ)言的一個(gè)重要應(yīng)用領(lǐng)域就是數(shù)據(jù)庫(kù)開(kāi)發(fā)。通過(guò)Java?JDBC(Java?Database?Connectivity)?API,開(kāi)發(fā)人員可以使用Java語(yǔ)言來(lái)訪問(wèn)并操作各種數(shù)據(jù)庫(kù)。
本篇資料將會(huì)詳細(xì)介紹Java?JDBC數(shù)據(jù)庫(kù)開(kāi)發(fā)的相關(guān)知識(shí),從入門(mén)到精通,涵蓋了JDBC的基本概念、如何連接數(shù)據(jù)庫(kù)、如何執(zhí)行SQL語(yǔ)句以及如何處理數(shù)據(jù)庫(kù)事務(wù)等方面。此外,還會(huì)給出許多實(shí)際例子,來(lái)演示如何應(yīng)用JDBC?API來(lái)實(shí)現(xiàn)各種數(shù)據(jù)庫(kù)操作。讀者可以通過(guò)本篇資料來(lái)了解和掌握J(rèn)ava?JDBC數(shù)據(jù)庫(kù)開(kāi)發(fā)的相關(guān)技能。
章節(jié)列表:
1.?JDBC的基本概念
2.?連接不同類(lèi)型的數(shù)據(jù)庫(kù)
3.?執(zhí)行SQL語(yǔ)句
4.?處理查詢(xún)結(jié)果集
5.?處理事務(wù)
6.?JDBC的高級(jí)特性
第一章:JDBC的基本概念
Java?Database?Connectivity(JDBC)是一種用于連接各種關(guān)系型數(shù)據(jù)庫(kù)的API,它允許Java應(yīng)用程序通過(guò)標(biāo)準(zhǔn)的Java編程語(yǔ)言方式訪問(wèn)數(shù)據(jù)庫(kù),包括連接數(shù)據(jù)庫(kù)、執(zhí)行SQL語(yǔ)句、處理查詢(xún)結(jié)果等操作。
JDBC的三個(gè)核心組件:
1.?Driver:用于連接數(shù)據(jù)庫(kù)的驅(qū)動(dòng)程序。
2.?Connection:與數(shù)據(jù)庫(kù)的連接對(duì)象。
3.?Statement:執(zhí)行SQL語(yǔ)句的對(duì)象。
第二章:連接不同類(lèi)型的數(shù)據(jù)庫(kù)
在JDBC中,不同的數(shù)據(jù)庫(kù)需要使用特定數(shù)據(jù)庫(kù)的驅(qū)動(dòng)程序。常見(jiàn)的數(shù)據(jù)庫(kù)有MySQL、Oracle、SQL?Server等。
連接MySQL數(shù)據(jù)庫(kù)示例:
import?java.sql.*;
public?class?ConnectMySQL?{
???static?final?String?USER?=?"username";
???static?final?String?PASS?=?"password";
???static?final?String?DB_URL?=?"jdbc:mysql://localhost:3306/Employee";
???public?static?void?main(String[]?args)?{
??????Connection?conn?=?null;
??????Statement?stmt?=?null;
??????try{
?????????Class.forName("com.mysql.jdbc.Driver");
?????????System.out.println("Connecting?to?database...");
?????????conn?=?DriverManager.getConnection(DB_URL,USER,PASS);
?????????System.out.println("Connected?database?successfully...");
?????????stmt?=?conn.createStatement();
?????????String?sql?=?"SELECT?id,?first,?last,?age?FROM?Employees";
?????????ResultSet?rs?=?stmt.executeQuery(sql);
?????????while(rs.next()){
????????????System.out.print("ID:?"?+?rs.getInt("id"));
????????????System.out.print(",?Age:?"?+?rs.getInt("age"));
????????????System.out.print(",?First:?"?+?rs.getString("first"));
????????????System.out.println(",?Last:?"?+?rs.getString("last"));
?????????}
?????????rs.close();
??????}catch(SQLException?se){
?????????se.printStackTrace();
??????}catch(Exception?e){
?????????e.printStackTrace();
??????}finally{
?????????try{
????????????if(stmt!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
?????????}
?????????try{
????????????if(conn!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
????????????se.printStackTrace();
?????????}
??????}
??????System.out.println("Goodbye!");
???}
}
連接Oracle數(shù)據(jù)庫(kù)示例:
import?java.sql.*;
public?class?ConnectOracle?{
???static?final?String?USER?=?"username";
???static?final?String?PASS?=?"password";
???static?final?String?DB_URL?=?"jdbc:oracle:thin:@localhost:1521:xe";
???public?static?void?main(String[]?args)?{
??????Connection?conn?=?null;
??????Statement?stmt?=?null;
??????try{
?????????Class.forName("oracle.jdbc.driver.OracleDriver");
?????????System.out.println("Connecting?to?database...");
?????????conn?=?DriverManager.getConnection(DB_URL,USER,PASS);
?????????System.out.println("Connected?database?successfully...");
?????????stmt?=?conn.createStatement();
?????????String?sql?=?"SELECT?id,?first,?last,?age?FROM?Employees";
?????????ResultSet?rs?=?stmt.executeQuery(sql);
?????????while(rs.next()){
????????????System.out.?print("ID:?"?+?rs.getInt("id"));
????????????System.out.print(",?Age:?"?+?rs.getInt("age"));
????????????System.out.print(",?First:?"?+?rs.getString("first"));
????????????System.out.println(",?Last:?"?+?rs.getString("last"));
?????????}
?????????rs.close();
??????}catch(SQLException?se){
?????????se.printStackTrace();
??????}catch(Exception?e){
?????????e.printStackTrace();
??????}finally{
?????????try{
????????????if(stmt!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
?????????}
?????????try{
????????????if(conn!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
????????????se.printStackTrace();
?????????}
??????}
??????System.out.println("Goodbye!");
???}
}
第三章:執(zhí)行SQL語(yǔ)句
在JDBC中,使用Statement和PreparedStatement對(duì)象分別執(zhí)行靜態(tài)SQL和動(dòng)態(tài)SQL語(yǔ)句。
創(chuàng)建Statement對(duì)象執(zhí)行靜態(tài)SQL示例:
?
import?java.sql.*;
public?class?ExecuteStatement?{
???static?final?String?DB_URL?=?"jdbc:mysql://localhost:3306/Employee";
???static?final?String?USER?=?"username";
???static?final?String?PASS?=?"password";
???static?final?String?INSERT_SQL?=?"INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(100,?'John',?'Doe',?25)";
???public?static?void?main(String[]?args)?{
??????Connection?conn?=?null;
??????Statement?stmt?=?null;
??????try{
?????????Class.forName("com.mysql.jdbc.Driver");
?????????System.out.println("Connecting?to?database...");
?????????conn?=?DriverManager.getConnection(DB_URL,USER,PASS);
?????????System.out.println("Connected?database?successfully...");
?????????stmt?=?conn.createStatement();
?????????System.out.println("Executing?SQL?query...");
?????????stmt.executeUpdate(INSERT_SQL);
?????????System.out.println("SQL?query?executed?successfully...");
??????}catch(SQLException?se){
?????????se.printStackTrace();
??????}catch(Exception?e){
?????????e.printStackTrace();
??????}finally{
?????????try{
????????????if(stmt!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
?????????}
?????????try{
????????????if(conn!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
????????????se.printStackTrace();
?????????}
??????}
??????System.out.println("Goodbye!");
???}
}
創(chuàng)建PreparedStatement對(duì)象執(zhí)行動(dòng)態(tài)SQL示例:
import?java.sql.*;
public?class?ExecutePreparedStatement?{
???static?final?String?DB_URL?=?"jdbc:mysql://localhost:3306/Employee";
???static?final?String?USER?=?"username";
???static?final?String?PASS?=?"password";
???static?final?String?SQL?=?"INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(?,??,??,??)";
???public?static?void?main(String[]?args)?{
??????Connection?conn?=?null;
??????PreparedStatement?pstmt?=?null;
??????try{
?????????Class.forName("com.mysql.jdbc.Driver");
?????????System.out.println("Connecting?to?database...");
?????????conn?=?DriverManager.getConnection(DB_URL,USER,PASS);
?????????System.out.println("Connected?database?successfully...");
?????????pstmt?=?conn.prepareStatement(SQL);
?????????pstmt.setInt(1,?101);
?????????pstmt.setString(2,?"Tom");
?????????pstmt.setString(3,?"Smith");
pstmt.setInt(4,?30);
?????????System.out.println("Executing?SQL?query...");
?????????pstmt.executeUpdate();
?????????System.out.println("SQL?query?executed?successfully...");
??????}catch(SQLException?se){
?????????se.printStackTrace();
??????}catch(Exception?e){
?????????e.printStackTrace();
??????}finally{
?????????try{
????????????if(pstmt!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
?????????}
?????????try{
????????????if(conn!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
????????????se.printStackTrace();
?????????}
??????}
??????System.out.println("Goodbye!");
???}
}
第四章:處理查詢(xún)結(jié)果集
在JDBC中,通過(guò)ResultSet對(duì)象來(lái)處理查詢(xún)結(jié)果集,包括遍歷查詢(xún)結(jié)果、獲取指定列的值等操作。
處理查詢(xún)結(jié)果集示例:
import?java.sql.*;
public?class?ProcessResultSet?{
???static?final?String?DB_URL?=?"jdbc:mysql://localhost:3306/Employee";
???static?final?String?USER?=?"username";
???static?final?String?PASS?=?"password";
???static?final?String?SQL?=?"SELECT?id,?first,?last,?age?FROM?Employees";
???public?static?void?main(String[]?args)?{
??????Connection?conn?=?null;
??????Statement?stmt?=?null;
??????try{
?????????Class.forName("com.mysql.jdbc.Driver");
?????????System.out.println("Connecting?to?database...");
?????????conn?=?DriverManager.getConnection(DB_URL,USER,PASS);
?????????System.out.println("Connected?database?successfully...");
?????????stmt?=?conn.createStatement();
?????????ResultSet?rs?=?stmt.executeQuery(SQL);
?????????while(rs.next()){
????????????int?id??=?rs.getInt("id");
????????????int?age?=?rs.getInt("age");
????????????String?first?=?rs.getString("first");
????????????String?last?=?rs.getString("last");
????????????System.out.print("ID:?"?+?id);
????????????System.out.print(",?Age:?"?+?age);
????????????System.out.print(",?First:?"?+?first);
????????????System.out.println(",?Last:?"?+?last);
?????????}
?????????rs.close();
??????}catch(SQLException?se){
?????????se.printStackTrace();
??????}catch(Exception?e){
?????????e.printStackTrace();
??????}finally{
?????????try{
????????????if(stmt!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
?????????}
?????????try{
????????????if(conn!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
????????????se.printStackTrace();
?????????}
??????}
??????System.out.println("Goodbye!");
???}
}
第五章:處理事務(wù)
在JDBC中,事務(wù)處理對(duì)于保證數(shù)據(jù)的一致性非常重要。通過(guò)對(duì)Connection對(duì)象開(kāi)啟事務(wù)、設(shè)置保存點(diǎn)、提交或回滾,可以實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)中數(shù)據(jù)的增刪改操作的原子性和持久性。
處理事務(wù)示例:
?
import?java.sql.*;
public?class?ProcessTransaction?{
???static?final?String?DB_URL?=?"jdbc:mysql://localhost:3306/Employee";
???static?final?String?USER?=?"username";
???static?final?String?PASS?=?"password";
???public?static?void?main(String[]?args)?{
??????Connection?conn?=?null;
??????Statement?stmt?=?null;
??????try{
?????????Class.forName("com.mysql.jdbc.Driver");
?????????System.out.println("Connecting?to?database...");
?????????conn?=?DriverManager.getConnection(DB_URL,USER,PASS);
?????????System.out.println("Connected?datab?ase?successfully...");
?????????conn.setAutoCommit(false);
?????????stmt?=?conn.createStatement();
?????????String?sql1?=?"INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(200,?'Jack',?'Jones',?25)";
?????????stmt.executeUpdate(sql1);
?????????String?sql2?=?"INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(201,?'Mary',?'Smith',?32)";
?????????stmt.executeUpdate(sql2);
?????????Savepoint?savepoint?=?conn.setSavepoint("Savepoint1");
?????????String?sql3?=?"INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(202,?'Tom',?'Brown',?28)";
?????????stmt.executeUpdate(sql3);
?????????String?sql4?=?"INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(203,?'Linda',?'White',?41)";
?????????stmt.executeUpdate(sql4);
?????????String?sql5?=?"INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(204,?'Mike',?'Green',?38)";
?????????stmt.executeUpdate(sql5);
?????????conn.commit();
?????????System.out.println("Transaction?committed?successfully...");
??????}catch(SQLException?se){
?????????try{
????????????if(conn!=null)
???????????????conn.rollback();
?????????}catch(SQLException?se2){
????????????se2.printStackTrace();
?????????}
?????????se.printStackTrace();
??????}catch(Exception?e){
?????????e.printStackTrace();
??????}finally{
?????????try{
????????????if(stmt!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
?????????}
?????????try{
????????????if(conn!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
????????????se.printStackTrace();
?????????}
??????}
??????System.out.println("Goodbye!");
???}
}
第六章:JDBC的高級(jí)特性
除了基本的JDBC操作,還有很多高級(jí)特性可以進(jìn)一步優(yōu)化數(shù)據(jù)庫(kù)開(kāi)發(fā),包括批處理、元數(shù)據(jù)查詢(xún)、數(shù)據(jù)庫(kù)連接池等。
JDBC批處理示例:
?
import?java.sql.*;
public?class?ExecuteBatch?{
???static?final?String?DB_URL?=?"jdbc:mysql://localhost:3306/Employee";
???static?final?String?USER?=?"username";
???static?final?String?PASS?=?"password";
???public?static?void?main(String[]?args)?{
??????Connection?conn?=?null;
??????Statement?stmt?=?null;
??????try{
?????????Class.forName("com.mysql.jdbc.Driver");
?????????System.out.println("Connecting?to?database...");
?????????conn?=?DriverManager.getConnection(DB_URL,USER,PASS);
?????????System.out.println("Connected?database?successfully...");
?????????stmt?=?conn.createStatement();
?????????conn.setAutoCommit(false);
?????????stmt.addBatch("INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(300,?'John',?'Doe',?25)");
?????????stmt.addBatch("INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(301,?'Mary',?'Smith',?32)");
?????????stmt.addBatch("INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(302,?'Tom',?'Brown',?28)");
?????????stmt.addBatch("INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(303,?'Linda',?'White',?41)");
?????????stmt.addBatch("INSERT?INTO?Employees?(id,?first,?last,?age)?VALUES?(304,?'Mike',?'Green',?38)");
?????????int[]?count?=?stmt.executeBatch();
?????????conn.commit();
?????????System.out.println("Batch?executed?successfully...");
?????????for(int?i=0;i<count.length;i++){
????????????System.out.println(count[i]?+?"?row(s)?affected.");
?????????}
??????}catch(SQLException?se){
?????????try{
????????????if(conn!=null)
???????????????conn.rollback();
?????????}catch(SQLException?se2){
????????????se2.printStackTrace();
?????????}
?????????se.printStackTrace();
??????}catch(Exception?e){
?????????e.printStackTrace();
??????}finally{
?????????try{
????????????if(stmt!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
?????????}try{
????????????if(conn!=null)
???????????????conn.close();
?????????}catch(SQLException?se){
????????????se.printStackTrace();
?????????}
??????}
??????System.out.println("Goodbye!");
???}
}
JDBC元數(shù)據(jù)查詢(xún)可以通過(guò)Connection對(duì)象的getMetaData方法來(lái)獲取數(shù)據(jù)庫(kù)的元數(shù)據(jù)信息,包括表結(jié)構(gòu)、列信息、索引信息等。通過(guò)元數(shù)據(jù)查詢(xún),可以方便地了解數(shù)據(jù)庫(kù)的基本信息,從而更好地進(jìn)行數(shù)據(jù)庫(kù)開(kāi)發(fā)。
元數(shù)據(jù)查詢(xún)示例:
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-433826.html
import java.sql.*;
public class MetadataQuery {
static final String DB_URL = "jdbc:mysql://localhost:3306/Employee";
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Connected database successfully...");
DatabaseMetaData dbmd=conn.getMetaData();
System.out.println("Driver Name: "+dbmd.getDriverName());
System.out.println("Driver Version: "+dbmd.getDriverVersion());
System.out.println("Database Name: "+dbmd.getDatabaseProductName());
System.out.println("Database Version: "+dbmd.getDatabaseProductVersion());
System.out.println("Tables:");
ResultSet rs=dbmd.getTables(null,null,null,new String[]{"TABLE"});
while(rs.next()){
System.out.println(rs.getString("TABLE_NAME"));
}
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
數(shù)據(jù)庫(kù)連接池是一種重要的數(shù)據(jù)庫(kù)優(yōu)化方案,通過(guò)建立連接池,可以避免頻繁地創(chuàng)建和銷(xiāo)毀數(shù)據(jù)庫(kù)連接,從而提高系統(tǒng)性能并減少數(shù)據(jù)庫(kù)開(kāi)銷(xiāo)。JDBC連接池可以通過(guò)一些現(xiàn)成的開(kāi)源項(xiàng)目進(jìn)行實(shí)現(xiàn),包括Apache Commons DBCP和C3P0等。
本篇資料到此結(jié)束,希望能夠通過(guò)學(xué)習(xí)JDBC,掌握J(rèn)ava對(duì)關(guān)系型數(shù)據(jù)庫(kù)的訪問(wèn)和操作,提高系統(tǒng)性能和穩(wěn)定性。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-433826.html
到了這里,關(guān)于JAVA JDBC數(shù)據(jù)庫(kù)開(kāi)發(fā)詳解:從入門(mén)到精通的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!