JDBC
的全稱是Java Database Connectivity
,即Java
數據庫鏈接,它是一種能夠執行SQL
語句的Java API
。程序可經過JDBC API
鏈接到關係數據庫,並使用結構化查詢語言(SQL
,數據庫標準的查詢語言)來完成對數據庫的查詢、更新html與其餘數據庫編程環境相比,
JDBC
爲數據庫開發提供了標準的API
,使用JDBC
開發的數據庫應用能夠跨平臺運行,並且還能夠跨數據庫(若是所有使用標準的SQL
語句)。也就是說若是使用JDBC開發一個數據庫應用,則該應用既能夠在Windows操做系統上運行,又能夠在Unix
等其餘操做系統上運行,既可使用MySQ
L數據庫,又可使用Oracle
等其餘的數據庫,應用程序不須要作任何的修改java
Class.forName(classDriver)
其中classDrive
r就是數據庫驅動類對應的字符串,下面給出加載mysql
,oracle
數據庫的例子:python
Class.forName("com.mysql.jdbc.Driver"); //mysql Class.forName("oracle.jabc.driver.OracleDriver"); //oracle
得到數據庫鏈接的方法爲DriverManager.getConnection()
,其中有不一樣的參數,也對應不一樣的方法,下面將會詳細介紹mysql
DriverManager.getConnection(String url)
git
DriverManager.getConnection(String url, Properties prop)
這裏的Properties是一個屬性集,詳情請看文檔github
DriverManager.getConnection(String url,String user,String password)
這裏的url
是jdbc:mysql://localhost:3306/java_demo
,其中java_demo
表示你本身建立的數據庫名字,urser
表示當前數據庫的登陸的用戶名,password
表示密碼sql
//第二種方法 String url="jdbc:mysql://localhost:3306/java_demo"; //這是鏈接的url String user="root"; String password="root"; Properties properties=new Properties(); //建立屬性集 properties.setProperty("password", password); //向起中添加屬性,很想python中的字典 properties.setProperty("user",user); Class.forName("com.mysql.jdbc.Driver"); //加載數據庫驅動 Connection conn=DriverManager.getConnection(url,properties); //鏈接數據庫 //第三種方法 Connection conn=DriverManager.getConnection(url,user,password); //鏈接數據庫
注意:以上只是一些例子,並非完整的代碼,其中並無處理異常,還應該注意的是要關閉connection數據庫
查詢數據有兩種方法,分別爲靜態查詢和動態查詢,靜態查詢使用的Statement,動態查詢使用的PrepareStatement,下面詳細介紹這兩種查詢方法編程
使用的是
Statement
,其中經常使用的函數以下:api
boolean execute(String SQL)
若是ResultSet
對象能夠被檢索,則返回的布爾值爲true
,不然返回false
。當你須要使用真正的動態SQL
時,可使用這個方法來執行SQL DDL
語句
int executeUpdate(String SQL)
返回執行SQL
語句影響的行的數目。使用該方法來執行SQL
語句,是但願獲得一些受影響的行的數目,例如,INSERT
,UPDATE
或DELETE
語句
ResultSet executeQuery(String SQL)
: 返回一個ResultSet
對象。當你但願獲得一個結果集時使用該方法,就像你使用一個SELECT
語句。
close()
關閉statement
對象,這個是必須有的,爲了程序的安全,必須在結束以前關閉實例:
Statement stmt = null; //申請對象 try { stmt = connection.createStatement( ); //經過Connection對象建立statement對象 String sql_1="select * from course;"; String sql_2="select * from course where id=2;"; ResultSet res_1=stm.executeQuery(sql_1); //執行查詢語句,返回的是一個結果集合,上面已經說明了 ResultSet res_2=stm.executeQuery(sql_1); while(res_1.next()) { System.out.println(res_1.getInt(1)+"---"+res_1.getString(2)); //分別查詢第一列和第二列的值,經過列數查詢 System.out.println(res_1.getInt("id")+"---"+res_1.getString("name")); //經過列名查詢 } } catch (SQLException e) { //捕捉異常 . . . } finally { if(connection!=null) { connection.close(); //關閉鏈接 } if(stmt!=null) { stmt.close(); //關閉 } }
說明:
ResultSet
經常使用的方法以下:注意下面的方法會發生SQLException
異常
public void beforeFirst()
將光標移動到第一行以前。
public void afterLast()
將光標移動到最後一行以後。
public boolean first()
將光標移動到第一行。從第一行的數據開始讀取
public void last()
將光標移動到最後一行。
public boolean absolute(int row)
將光標移動到指定的第row
行。
public boolean previous()
將光標移動到上一行,若是超過結果集的範圍則返回false
。
public boolean next()
將光標移動到下一行,若是是結果集的最後一行則返回 false。
public int getRow()
返回當前光標指向的行數的值。
public void moveToInsertRow()
將光標移動到結果集中指定的行,能夠在數據庫中插入新的一行。當前光標位置將被記住
public void moveToCurrentRow()
若是光標處於插入行,則將光標返回到當前行,其餘狀況下,這個方法不執行任何操做
public int getInt(String columnName)
返回當前行中名爲columnName
的列的int
值。
public int getInt(int columnIndex)
返回當前行中指定列的索引的int
值。列索引從1
開始,意味着行中的第一列是1
,第二列是2
,以此類推。
getString(int columIndex)
返回指定列的String
類型的數據
getString(String columName)
返回當前行中名爲columName
的String
類型的值
動態查詢使用的
PrepareStatement
這個類實現的,PreparedStatement
接口擴展了Statement
接口,它讓你用一個經常使用的Statement
對象增長几個高級功能。這個statement
對象能夠提供靈活多變的動態參數實例:
PreparedStatement pstmt = null; try { String SQL = "select * from course where age=? and name=?"; pstmt = conn.prepareStatement(SQL); //建立對象 pstmt.setInt(1,22); //設置參數age的值 ,1表示第一個參數 pstmt.setString(2,"chenjiabing"); //設置name的值,其中2表示第二個參數 ResultSet res=pstmt.execteQuery(); while(res.next) { .... } . . . } catch (SQLException e) { . . . } finally { if(connection!=null) { connection.close(); } if(pstmt!=null) { pstmt.close(); //關閉 } . . . }
說明:
JDBC
中全部的參數都被用?
符號表示,這是已知的參數標記。在執行SQL
語句以前,你必須賦予每個參數確切的數值。其中PrepareStatement
的經常使用函數以下,固然Statement
中的execute
,executeQuery
,executeUpdate
也可使用
void setInt(int parameterIndex, int x)
parameterIndex
表示第幾個?
,這裏的int x
表示是mysql
中定義的int
類型的值
void setString(int parameterIndex,String x)
爲第parameterIndex
個String
類型的?賦予x
的值
這裏還有
delete
,update
,alter
等一系列的操做都是和上面的同樣,就是把sql
語句改變如下,若是使用的是靜態的就要爲delete
,update
,使用Statement.execteUpdate(sql)
這個函數,固然要使用動態的也是executeUpdate
函數