一、聲明
複製程式碼如下:
導入 java.sql.*;
公共類別 TestJDBC {
公共靜態無效主(字串[] args){
連接 oracle_conn = null;
語句 oracle_stmt = null;
結果集 oracle_rs = null;
連接 mssql_conn = null;
語句 mssql_stmt = null;
結果集 mssql_rs = null;
嘗試 {
Class.forName("oracle.jdbc.driver.OracleDriver");
oracle_conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.215:1521:orcl", "使用者名稱", "密碼");
oracle_stmt = oracle_conn.createStatement();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
mssql_conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.213:1433;DatabaseName=VIS", "使用者名稱", "密碼");
mssql_stmt = mssql_conn.createStatement();
mssql_rs = mssql_stmt.executeQuery("從VideoBaseInfo選擇*");
while(mssql_rs.next()) {
System.out.println("正在插入VideoId:" + mssql_rs.getInt("VideoId") + "的記錄...");
oracle_stmt.executeUpdate("插入 VIDEO_BASEINFO 值("
+ mssql_rs.getInt("VideoId") + ",'"
+ mssql_rs.getString("VideoName") + "','"
+ mssql_rs.getString("VideoVersion") + "',"
+ mssql_rs.getInt("VideoMp4Items") + ","
+ mssql_rs.getInt("VideoRmvbItems") + ",'"
+ mssql_rs.getString("VideoAliasName") + "','"
+ mssql_rs.getString("VideoAge") + "'"
+“)”);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} 最後 {
嘗試 {
如果(oracle_rs!= null){
oracle_rs.close();
oracle_rs = null;
}
如果(oracle_stmt!= null){
oracle_stmt.close();
oracle_stmt = null;
}
如果(oracle_conn!= null){
oracle_conn.close();
oracle_conn = 空;
}
if(mssql_rs != null) {
mssql_rs.close();
mssql_rs = 空;
}
if(mssql_stmt != null) {
mssql_stmt.close();
mssql_stmt = null;
}
if(mssql_conn != null) {
mssql_conn.close();
mssql_conn = 空;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
二、準備語句
複製程式碼如下:
導入 java.sql.*;
公共類別 TestPreparedStatement {
公共靜態無效主(字串[] args){
連接 oracle_conn = null;
準備語句 oracle_stmt = null;
結果集 oracle_rs = null;
連接 mssql_conn = null;
語句 mssql_stmt = null;
結果集 mssql_rs = null;
嘗試 {
Class.forName("oracle.jdbc.driver.OracleDriver");
oracle_conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.215:1521:orcl", "使用者名稱", "密碼");
oracle_stmt = oracle_conn.prepareStatement("插入 Video_ItemInfo 值(?, ?, ?, ?, ?, ?, ?, ?, ?)");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
mssql_conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.213:1433;DatabaseName=VIS", "使用者名稱", "密碼");
mssql_stmt = mssql_conn.createStatement();
mssql_rs = mssql_stmt.executeQuery("從VideoItemInfo選擇*");
while(mssql_rs.next()) {
System.out.println("正在插入ItemIndex:" + mssql_rs.getInt("ItemIndex") + "的記錄...");
oracle_stmt.setInt(1, mssql_rs.getInt("ItemIndex"));
oracle_stmt.setInt(2, mssql_rs.getInt("VideoId"));
oracle_stmt.setString(3, mssql_rs.getString("VideoItemName"));
oracle_stmt.setString(4, mssql_rs.getString("VideoExtName"));
oracle_stmt.setDouble(5, mssql_rs.getDouble("VideoSize"));
oracle_stmt.setString(6, mssql_rs.getString("VideoPath"));
oracle_stmt.setString(7, mssql_rs.getString("VideoType"));
oracle_stmt.setDate(8, mssql_rs.getDate("VideoDate"));
oracle_stmt.setString(9, mssql_rs.getString("ApplicationWay"));
oracle_stmt.executeUpdate();
}
System.out.println("插入資料到Video_ItemInfo表中操作已完成!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} 最後 {
嘗試 {
如果(oracle_rs!= null){
oracle_rs.close();
oracle_rs = null;
}
如果(oracle_stmt!= null){
oracle_stmt.close();
oracle_stmt = null;
}
如果(oracle_conn!= null){
oracle_conn.close();
oracle_conn = 空;
}
if(mssql_rs != null) {
mssql_rs.close();
mssql_rs = 空;
}
if(mssql_stmt != null) {
mssql_stmt.close();
mssql_stmt = null;
}
if(mssql_conn != null) {
mssql_conn.close();
mssql_conn = 空;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
三、CallableStatement
複製程式碼如下:
導入 java.sql.*;
公共類別測試程序{
/**
* @參數參數
*/
公共靜態無效主(字串[] args)拋出異常{
Class.forName("oracle.jdbc.driver.OracleDriver");
連接 conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
CallableStatement cstmt = conn.prepareCall("{call p(?, ?, ?, ?)}");
cstmt.registerOutParameter(3, Types.INTEGER);
cstmt.registerOutParameter(4, Types.INTEGER);
cstmt.setInt(1, 3);
cstmt.setInt(2, 4);
cstmt.setInt(4, 5);
cstmt.execute();
System.out.println(cstmt.getInt(3));
System.out.println(cstmt.getInt(4));
cstmt.close();
conn.close();
}
}
四、批量
複製程式碼如下:
導入 java.sql.*;
公共類別測試批次{
公共靜態無效主(字串[] args)拋出異常{
Class.forName("oracle.jdbc.driver.OracleDriver");
連接 conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
/*
語句 stmt = conn.createStatement();
stmt.addBatch("插入 dept2 值(51, '500', '哈哈')");
stmt.addBatch("插入 dept2 值(52, '500', '哈哈')");
stmt.addBatch("插入 dept2 值(53, '500', '哈哈')");
stmt.executeBatch();
stmt.close();
*/
ReadyStatement ps = conn.prepareStatement("插入 dept2 值 (?, ?, ?)");
ps.setInt(1, 61);
ps.setString(2, "哈哈");
ps.setString(3, "bj");
ps.addBatch();
ps.setInt(1, 62);
ps.setString(2, "哈哈");
ps.setString(3, "bj");
ps.addBatch();
ps.setInt(1, 63);
ps.setString(2, "哈哈");
ps.setString(3, "bj");
ps.addBatch();
ps.executeBatch();
ps.close();
conn.close();
}
}
五、交易
複製程式碼如下:
導入 java.sql.*;
公共類別測試事務{
公共靜態無效主(字串[] args){
連接 conn = null;
語句 stmt = null;
嘗試 {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:SXT", "scott", "tiger");
conn.setAutoCommit(假);
stmt = conn.createStatement();
stmt.addBatch("插入 dept2 值(51, '500', '哈哈')");
stmt.addBatch("插入 dept2 值(52, '500', '哈哈')");
stmt.addBatch("插入 dept2 值(53, '500', '哈哈')");
stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
嘗試 {
if(conn != null)
{
conn.rollback();
conn.setAutoCommit(true);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}最後 {
嘗試 {
if(stmt!= null)
stmt.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
六、ScrollResultSet
複製程式碼如下:
導入 java.sql.*;
公共類別測試滾動{
公共靜態無效主(字串參數[]){
嘗試 {
新的 oracle.jdbc.driver.OracleDriver();
String url = "jdbc:oracle:thin:@192.168.0.1:1521:SXT";
連接 conn = DriverManager
.getConnection(url, "史考特", "老虎");
語句 stmt = conn.createStatement(
結果集.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
結果集 rs = stmt
.executeQuery("從員工訂單中選擇 * by sal");
rs.next();
System.out.println(rs.getInt(1));
rs.last();
System.out.println(rs.getString(1));
System.out.println(rs.isLast());
System.out.println(rs.isAfterLast());
System.out.println(rs.getRow());
rs.previous();
System.out.println(rs.getString(1));
rs.absolute(6);
System.out.println(rs.getString(1));
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
七、更新結果集
複製程式碼如下:
導入 java.sql.*;
公有類別 TestUpdataRs {
公共靜態無效主(字串參數[]){
嘗試{
新的 oracle.jdbc.driver.OracleDriver();
字串 url="jdbc:oracle:thin:@192.168.0.1:1521:SXT";
連接 conn=DriverManager.getConnection(url,"scott","tiger");
語句 stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("從 emp2 選取 *");
rs.next();
//更新一行數據
rs.updateString("ename","AAAA");
rs.updateRow();
//插入新行
rs.moveToInsertRow();
rs.updateInt(1, 9999);
rs.updateString("ename","AAAA");
rs.updateInt("mgr", 7839);
rs.updateDouble(“薩爾”, 99.99);
rs.insertRow();
//將視覺移到新建的行
rs.moveToCurrentRow();
//刪除行
rs.absolute(5);
rs.deleteRow();
//取消更新
//rs.cancelRowUpdates();
}catch(SQLException e){
e.printStackTrace();
}
}
}