回覆列表
  • 1 # palit4921

    1、Driver介面: 載入MySQL驅動:Class.forName(“com.mysql.jdbc.Driver”); 載入Oracle驅動:Class.forName(“oracle.jdbc.driver.OracleDriver”);

    2、DriverManager類: 透過例項化的資料庫驅動物件,能夠建立應用程式與資料庫之間的連線,並返回Connection介面型別的資料庫連線物件。 常用方法:getConnection(String jdbcUrl, String user, String password) 返回對應的資料庫的connection物件。

    3、Connection介面: 與資料庫的連線(會話)物件。可以透過該物件執行SQL語句並返回結果。 連線MySQL資料庫: Connection conn = DriverManager.getConnection(“jdbc:mysql://host:port/database” , “user” , “password”); 連線Oracle資料庫: Connection conn = DriverManager.getConnection(“jdbc:oracle:thin:@host:port:database” , ”user” , “password”); 連線SQLServer資料庫: Connection conn = DriverManager.getConnection(“jdbc:microsoft:sqlserver://host:port ; DatabaseName=database” , “user” , “password”); 常用方法: createStatement(): 建立向資料庫傳送SQL的Statement介面型別的物件。 preparedStatement(sql):建立向資料庫傳送預編譯sql的PrepareStatement介面型別的物件。 prepareCall(sql):建立執行儲存過程的CallableStatement介面型別的物件。 setAutoCommit(boolean autoCommit):設定事務是否自動提交。 commit():在連結上提交事務。 rollback():在此連結上回滾事務。

    4、Statement介面: 用於執行靜態SQL語句並返回它所生成結果的物件。 由createStatement建立,用於傳送見到的SQL語句(不支援動態繫結)。 常用方法: execute(String sql):執行引數中的SQL,返回是否有結果集。 executeQuery(String sql):執行select語句,返回ResultSet結果集。 executeUpdate(String sql):執行insert、update、delete操作,返回更新的行數。 addBatch(String sql): 把多條SQL語句放到一個批處理中。 executeBatch():向資料庫傳送一批SQL語句執行。

    5、PreparedStatement介面: 繼承自Statement介面,由PrepareStatement建立,用於傳送含有一個或多個引數的SQL語句。PreparedStatement物件比Statement物件的效率更高,並且可以防止SQL注入,所以一般使用PreparedStatement。 常用方法: addBatch():把當前SQL語句加入到一個批處理中。 execute():執行當前SQL,返回一個boolean值。 executeUpdate():執行insert、update、delete操作,返回更新的行數。 executeQuery():執行當前的查詢,返回一個結果集物件。

    6、setDate(int parameterIndex , Date x) :向當前SQL語句中的指定位置繫結一個java.sql.Date值。 提供檢索不同型別欄位的方法。 setDouble(int parameterIndex , double x):向當前SQL語句中的指定位置繫結一個double值。 setFloat(int parameterIndex , float x):向當前SQL語句中的指定位置繫結一個float值。 setInt(int parameterIndex , int x):向當前SQL語句中的指定位置繫結一個int值。 setString(int parameterIndex , String x):向當前SQL語句中的指定位置繫結一個String值。

    7、ResultSet介面: 常用方法: getString(int index) 、 getString(String columnName) 獲得在資料庫裡是varchar、char等型別的資料物件。 getFloat(int index)、 getFloat(String columnName) 獲得在資料庫裡是Float型別的資料物件 getDate(int index) 、 getDate(String columnName) 獲得在資料庫裡是Date型別的資料。 getBoolead(int index) 、 getBoolean(String columnName) 獲得在資料庫裡是Boolean型別的物件。 getObject(int index) 、 getObject(String columnName) 獲取在資料庫裡任意型別的資料。 ResultSet對結果集進行滾動的方法: next():移動到下一行 Previous():移動到前一行 absolute(int row):移動到指定行 beforeFirst():移動到ResultSet的最前面 afterLast():移動到ResultSe

  • 中秋節和大豐收的關聯?
  • “女為悅己者容”的下一句是什麼?