天天看點

java database connectivity前言正文總結

 jdbc是一種用來在java程式中執行sql的api,它為java連接配接資料庫提供了一組接口和類。

連接配接資料庫的過程:java app->jdbc api ->driver;

1.獲得目前資料庫連接配接的使用者名和密碼

2.獲得資料庫伺服器的位址(ip)

3.獲得資料庫連接配接的端口号 oracle預設的是1521

4.獲得書庫的執行個體sid,即資料庫名稱

url=jdbc:oracle:thin:@ip:port:sid

classes12.jar或ojdbc14.jar

導入oracle資料庫的驅動oracle.jdbc.OracleDriver或者oracle.jdbc.driver.OracleDriver

class.forname("oracle.jdbc.OracleDriver");//通過反射加載驅動程式,在記憶體中建立oracleDriver的執行個體

Connection conn=DriverManager.getConnection()url,user,password;;//通過驅動管理器獲得連接配接對象

生成statement實作類對象來編譯sql,并将sql語句輸送到資料庫

Statement stmt=conn.createStatemenr();

方式二:PreparedStatement pstmt=conn.prepareStatement(sql);//sql字元型不用加分号來結束

ResultSet rs=stmt.executeQuery(sql);;

方式二:ResultSet rs=pstmt.executeQuery();

while(rs.next()){

 int empno=rs.getInt(String columnName);

}

if(rs!=null){rs.close();}

if(pstmt!=null){pstmt.close();}

if(conn!=null){rconnclose();}

注:

1.prepareCall(String sql); 用于調用存儲過程

2.conn.setAutoCommit(false);//設定目前jdbc的食物處理設定為手動

conn.rollback();

conn.commit();

3preparedStatement和Statement接口的差別

PreparedStatement 允許資料庫預編譯sql語句,這樣在随後的運作中可以節省時間,并增加了查詢的可讀性;Statement每次執行sql語句相關的資料庫都要執行sql語句的編譯。

預編譯sql會将sql語句預先編譯号,執行的時候直接傳遞參數,不需要再次編譯。

PreparedStatement是Statement的子接口,它與Statement的差別有:

1.程式的可讀性和可維護性更好

2.更安全

3.執行效率高

select x.* from (select e.* ,rownum rr from emp e)x where x.rr between start and end;

分頁有基于查詢分頁和基于緩存分頁。

通過資料庫中的僞劣,每次查詢一部分資料傳回。

1.特點

1)适合大資料量的分頁操作

2)分頁執行效率低,與資料庫互動頻繁。

 執行一次sql,将結果集的所有資料查詢出來,存儲在目前應用伺服器的緩沖中,然後通過可滾動的結果集進行分頁顯示;

1)會将結果集全部放入記憶體,對于記憶體壓力大

2)不适合大資料量的分頁操作

3)分頁時 ,第一次執行效率慢,以後執行效率高。

ResultSet預設隻能向前周遊,并且隻能周遊一次。

1.static int TYPE_SCROLL_INSENSITIVE

The constant indicating the type for a ResultSet object that is scrollable but generally not sensitive to changes to the data that underlies the ResultSet.設定獲得的結果集是可滾動的,并且不敏感(資料庫的資料變動不能及時反映到結果集)不受ResultSet底層資料更改的影響;而敏感是指将sql結果集的rowid存于緩存中,顯示時通過rowId查詢。

2.static int TYPE_SCROLL_SENSITIVE

The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes to the data that underlies the ResultSet.

3.static int CONCUR_READ_ONLY

The constant indicating the concurrency mode for a ResultSet object that may NOT be updated.設定結果集隻讀

4.static int CONCUR_UPDATABLE

The constant indicating the concurrency mode for a ResultSet object that may be updated.設定可更新的結果集。

1).更新

1))void updateString(int columnIndex, String x)

Updates the designated column with a String value. 更新目前記錄的字段

2))void updateString(String columnLabel, String x)

Updates the designated column with a String value.

3))void updateRow()

Updates the underlying database with the new contents of the current row of this ResultSet object.更新記錄到資料庫(同步)

2).定位

1))int getRow()

Retrieves the current row number. 獲得行号

2))void beforeFirst()

Moves the cursor to the front of this ResultSet object, just before the first row. 定位到第一條記錄之前

3))void afterLast()

Moves the cursor to the end of this ResultSet object, just after the last row. 定位到最後一條記錄之後

4))void deleteRow()

Deletes the current row from this ResultSet object and from the underlying database.删除目前行的記錄,并同步到資料庫。

3).插入

1))void moveToInsertRow()

Moves the cursor to the insert row. 将指針指向将要插入資料的行

2))void updateInt(int columnIndex, int x)

Updates the designated column with an int value.

3))void insertRow()

Inserts the contents of the insert row into this ResultSet object and into the database.同步到資料庫

資料庫連接配接池節省建立連接配接的時間;減少連接配接打開和關閉所消耗的資源。

commons-dbcp-1.4.jar和commons-pool.jar

ojdbc14.jar

BasicDataSource dbs=new BasicDataSource();

dbs.setUrl(jdbc:oracle:thin:@ip:port:sid);

dbs.setUsername(“username”);

dbs.setPassword("password");

dbs.setDriverClassName("oracle.jdbc.OracleDriver");

Connection conn=bds.getConnection();

大批量執行sql,為了提高效率采用批處理

1.jdbc有一組應用程式的api,用來開發java連接配接資料庫的應用程式;jdbc驅動api提供給資料庫廠商,資料庫廠商負責實作底層的編碼。

  1)) createStatement()  獲得Statement

  2)) prepareStatement(String sql) 獲得preparedStatement

  3)) prepareCall(String sql) 調用存儲過程

   1))getConnection(String url, String user, String password)

   1))executeQuery(String sql)  執行DQL語句

 2))  executeUpdate(String sql) 執行DML語句

   3))executeBatch()    批處理執行SQL

  1)) setXXX方法   用來動态傳參

  1)) next() 預設ResultSet對象指向記錄的光标在第一條的前面

  2)) getXXX(String/int)  獲得對應字段的值

  3)) getInt()  接收整數類型

   4))getDouble() 接收浮點資料

 5))  getString() 接收字元串類型

6))   sql.Date getDate()  接收Date類型資料字段