JDBC核心技術(shù)詳解
1、數(shù)據(jù)的持久化
持久化(persistence):把數(shù)據(jù)保存到可掉電式存儲(chǔ)設(shè)備中以供之后使用。大多數(shù)情況下,特別是企業(yè)級(jí)應(yīng)用,數(shù)據(jù)持久化意味著將內(nèi)存中的數(shù)據(jù)保存到硬盤上加以'固化”,而持久化的實(shí)現(xiàn)過程大多通過各種關(guān)系數(shù)據(jù)庫來完成。
持久化的主要應(yīng)用是將內(nèi)存中的數(shù)據(jù)存儲(chǔ)在關(guān)系型數(shù)據(jù)庫中,當(dāng)然也可以存儲(chǔ)在磁盤文件、XML數(shù)據(jù)文件中。
2、Java 中的數(shù)據(jù)存儲(chǔ)技術(shù) 在Java中,數(shù)據(jù)庫存取技術(shù)可分為如下幾類:
ØJDBC直接訪問數(shù)據(jù)庫ØJDO技術(shù)Ø第三方O/R工具,如Hibernate, mybatis 等JDBC是java訪問數(shù)據(jù)庫的基石,JDO, Hibernate等只是更好的封裝了JDBC
3、JDBC基礎(chǔ)
連接數(shù)據(jù)庫:
4、JDBC體系結(jié)構(gòu)
JDBC接口(API)包括兩個(gè)層次:
Ø 面向應(yīng)用的API:Java API,抽象接口,供應(yīng)用程序開發(fā)人員使用(連接數(shù)據(jù)庫,執(zhí)行SQL語句,獲得結(jié)果)。
Ø 面向數(shù)據(jù)庫的API:Java Driver API,供開發(fā)商開發(fā)數(shù)據(jù)庫驅(qū)動(dòng)程序用。
5、JDBC API
JDBC API 是一系列的接口,它使得應(yīng)用程序能夠進(jìn)行數(shù)據(jù)庫聯(lián)接,執(zhí)行SQL語句,并且得到返回結(jié)果。
6、JDBC程序訪問數(shù)據(jù)庫步驟
1、Driver 接口
java.sql.Driver 接口是所有 JDBC 驅(qū)動(dòng)程序需要實(shí)現(xiàn)的接口。這個(gè)接口是提供給數(shù)據(jù)庫廠商使用的,不同數(shù)據(jù)庫廠商提供不同的實(shí)現(xiàn)
在程序中不需要直接去訪問實(shí)現(xiàn)了 Driver 接口的類,而是由驅(qū)動(dòng)程序管理器類(java.sql.DriverManager)去調(diào)用這些Driver實(shí)現(xiàn)ØOracle的驅(qū)動(dòng):oracle.jdbc.driver.OracleDriverØmySql的驅(qū)動(dòng): com.mysql.jdbc.Driver
2、加載與注冊(cè) JDBC 驅(qū)動(dòng)
3、建立連接(Connection)
4、幾種常用數(shù)據(jù)庫的JDBC URL
連接mysql代碼實(shí)例:
import java.io.InputStream;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Properties;import org.junit.Test;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Driver;public class testconnction01 { /** * @throws SQLException */ @Test //方式一public void testConnection011() throws SQLException {Driver driver = new com.mysql.jdbc.Driver();String url = 'jdbc:mysql://localhost:3306/test';Properties info = new Properties();info.setProperty('user', 'root');info.setProperty('password', 'root');Connection collection = (Connection) driver.connect(url, info);System.out.println(collection);}@Test //方式二,對(duì)方式一的迭代,使得程序具有更好的可移植性 public void testConnection02() throws Exception { //獲取Driver實(shí)現(xiàn)類對(duì)象;使用反射 Class clazz= Class.forName('com.mysql.jdbc.Driver'); Driver driver=(Driver) clazz.newInstance(); //提供要連接的數(shù)據(jù)庫 String url = 'jdbc:mysql://localhost:3306/test'; //提供連接需要的用戶名和密碼 Properties info = new Properties(); info.setProperty('user', 'root'); info.setProperty('password', 'root'); //獲取連接 Connection collection = (Connection) driver.connect(url, info); System.out.println(collection); }@Test //方式三,使用DriverManager替換Driver public void testConnection03() throws Exception { Class clazz= Class.forName('com.mysql.jdbc.Driver'); Driver driver=(Driver) clazz.newInstance(); //提供另外三個(gè)連接的基本信息; String url ='jdbc:mysql://localhost:3306/test'; String user ='root'; String password ='root'; //注冊(cè)驅(qū)動(dòng) DriverManager.registerDriver(driver);//獲取連接 Connection connection= (Connection) DriverManager.getConnection(url,user,password); System.out.println(connection); }@Test //方式四,可以只是加載驅(qū)動(dòng),不用是示的注冊(cè)驅(qū)動(dòng)過了。 public void testConnection04() throws Exception { //提供另外三個(gè)連接的基本信息; String url ='jdbc:mysql://localhost:3306/test'; String user ='root'; String password ='root'; Class.forName('com.mysql.jdbc.Driver'); // Class clazz= Class.forName('com.mysql.jdbc.Driver');// Driver driver=(Driver) clazz.newInstance();// // //注冊(cè)驅(qū)動(dòng)// DriverManager.registerDriver(driver);////獲取連接 Connection connection= (Connection) DriverManager.getConnection(url,user,password); System.out.println(connection); }@Test //方式五,(final版):將數(shù)據(jù)庫連接需要的4個(gè)基本信息聲明在配置文件中,通過讀取配置文件的方式,獲取連接 //實(shí)現(xiàn)了數(shù)據(jù)和代碼的分離(解耦) public void testConnection05() throws Exception { //讀取配置文件的4個(gè)基本信息 InputStream is = testconnction01.class.getClassLoader().getResourceAsStream('jdbc.properties'); Properties p =new Properties(); p.load(is); String user = p.getProperty('user'); String password = p.getProperty('password'); String url = p.getProperty('url'); String driverClass =p.getProperty('driverClass'); Class.forName(driverClass); //獲取連接 Connection connection= (Connection) DriverManager.getConnection(url,user,password); System.out.println(connection); }}
5、使用Statement操作數(shù)據(jù)表的弊端 a、需要拼寫SQL語句
6、Statement操作會(huì)導(dǎo)致SQL注入攻擊
數(shù)據(jù)庫連接被用于向數(shù)據(jù)庫服務(wù)器發(fā)送命令和SQL語句,并接受數(shù)據(jù)庫服務(wù)器返回的結(jié)果。其實(shí)一個(gè)數(shù)據(jù)庫連接就是一個(gè)Socket連接。
在java.sql包中有3個(gè)接口分別定義了對(duì)數(shù)據(jù)庫的調(diào)用的不同方式:
Statement :用于執(zhí)行靜態(tài)SQL語句并返回它所生成結(jié)果的對(duì)象。PrepatedIatement : SQL語句被預(yù)編譯并存儲(chǔ)在此對(duì)象中,可以使用此對(duì)象多次高效地執(zhí)行該語句。CallableStatement :用于執(zhí)行SQL存儲(chǔ)過程
-
到此這篇關(guān)于JDBC核心技術(shù)詳解的文章就介紹到這了,更多相關(guān)JDBC核心技術(shù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Ajax返回值類型與用法實(shí)例分析2. XML入門精解之結(jié)構(gòu)與語法3. 如何使用CSS3畫出一個(gè)叮當(dāng)貓4. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式5. 如何在jsp界面中插入圖片6. jsp實(shí)現(xiàn)登錄界面7. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)8. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?9. WML學(xué)習(xí)之二基本格式和文件頭10. asp取整數(shù)mod 有小數(shù)的就自動(dòng)加1
