一、Déclaration
复制代码代码如下 :
importer java.sql.* ;
classe publique TestJDBC {
public static void main (String[] arguments) {
Connexion oracle_conn = null ;
Déclaration oracle_stmt = null ;
ResultSet oracle_rs = null ;
Connexion mssql_conn = null ;
Instruction mssql_stmt = null ;
ResultSet mssql_rs = null ;
essayer {
Class.forName("oracle.jdbc.driver.OracleDriver");
oracle_conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.215:1521:orcl", "nom d'utilisateur", "mot de passe");
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", "nom d'utilisateur", "mot de passe");
mssql_stmt = mssql_conn.createStatement();
mssql_rs = mssql_stmt.executeQuery("select * from VideoBaseInfo");
while(mssql_rs.next()) {
System.out.println("正在插入VideoId:" + mssql_rs.getInt("VideoId") + "的记录...");
oracle_stmt.executeUpdate("insérer dans les valeurs VIDEO_BASEINFO("
+ mssql_rs.getInt("VideoId") + ","'"
+ mssql_rs.getString("NomVidéo") + "','"
+ mssql_rs.getString("VersionVidéo") + "',"
+ 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();
} enfin {
essayer {
si(oracle_rs != null) {
oracle_rs.close();
oracle_rs = nul ;
}
si(oracle_stmt != null) {
oracle_stmt.close();
oracle_stmt = nul ;
}
si(oracle_conn != null) {
oracle_conn.close();
oracle_conn = nul ;
}
si(mssql_rs != null) {
mssql_rs.close();
mssql_rs = nul ;
}
si(mssql_stmt != null) {
mssql_stmt.close();
mssql_stmt = nul ;
}
si(mssql_conn != null) {
mssql_conn.close();
mssql_conn = nul ;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Par exemple, PreparedStatement
复制代码代码如下 :
importer java.sql.* ;
classe publique TestPreparedStatement {
public static void main (String[] arguments) {
Connexion oracle_conn = null ;
PreparedStatement oracle_stmt = null ;
ResultSet oracle_rs = null ;
Connexion mssql_conn = null ;
Instruction mssql_stmt = null ;
ResultSet mssql_rs = null ;
essayer {
Class.forName("oracle.jdbc.driver.OracleDriver");
oracle_conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.215:1521:orcl", "nom d'utilisateur", "mot de passe");
oracle_stmt = oracle_conn.prepareStatement("insérer dans les valeurs Video_ItemInfo (?, ?, ?, ?, ?, ?, ?, ?, ?)");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
mssql_conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.213:1433;DatabaseName=VIS", "nom d'utilisateur", "mot de passe");
mssql_stmt = mssql_conn.createStatement();
mssql_rs = mssql_stmt.executeQuery("select * from 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();
} enfin {
essayer {
si(oracle_rs != null) {
oracle_rs.close();
oracle_rs = nul ;
}
si(oracle_stmt != null) {
oracle_stmt.close();
oracle_stmt = nul ;
}
si(oracle_conn != null) {
oracle_conn.close();
oracle_conn = nul ;
}
si(mssql_rs != null) {
mssql_rs.close();
mssql_rs = nul ;
}
si(mssql_stmt != null) {
mssql_stmt.close();
mssql_stmt = nul ;
}
si(mssql_conn != null) {
mssql_conn.close();
mssql_conn = nul ;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
à propos de CallableStatement
复制代码代码如下 :
importer java.sql.* ;
classe publique TestProc {
/**
* @param arguments
*/
public static void main (String[] args) lève une exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connexion 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();
}
}
四、Lot
复制代码代码如下 :
importer java.sql.* ;
classe publique TestBatch {
public static void main (String[] args) lève une exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connexion conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
/*
Instruction stmt = conn.createStatement();
stmt.addBatch("insérer dans les valeurs dept2 (51, '500', 'haha')");
stmt.addBatch("insérer dans les valeurs dept2 (52, '500', 'haha')");
stmt.addBatch("insérer dans les valeurs dept2 (53, '500', 'haha')");
stmt.executeBatch();
stmt.close();
*/
PreparedStatement ps = conn.prepareStatement("insérer dans les valeurs dept2 (?, ?, ?)");
ps.setInt(1, 61);
ps.setString(2, "haha");
ps.setString(3, "bj");
ps.addBatch();
ps.setInt(1, 62);
ps.setString(2, "haha");
ps.setString(3, "bj");
ps.addBatch();
ps.setInt(1, 63);
ps.setString(2, "haha");
ps.setString(3, "bj");
ps.addBatch();
ps.executeBatch();
ps.close();
conn.close();
}
}
à propos de Transaction
复制代码代码如下 :
importer java.sql.* ;
classe publique TestTransaction {
public static void main (String[] arguments) {
Connexion de connexion = null ;
Instruction stmt = null ;
essayer {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:SXT", "scott", "tiger");
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.addBatch("insérer dans les valeurs dept2 (51, '500', 'haha')");
stmt.addBatch("insérer dans les valeurs dept2 (52, '500', 'haha')");
stmt.addBatch("insérer dans les valeurs dept2 (53, '500', 'haha')");
stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLExceptione) {
e.printStackTrace();
essayer {
si(conn != nul)
{
conn.rollback();
conn.setAutoCommit(true);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}enfin {
essayer {
si(stmt != nul)
stmt.close();
si(conn != nul)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Par exemple, ScrollResultSet
复制代码代码如下 :
importer java.sql.* ;
classe publique TestScroll {
public static void main(String args[]) {
essayer {
nouveau oracle.jdbc.driver.OracleDriver();
URL de chaîne = "jdbc:oracle:thin:@192.168.0.1:1521:SXT" ;
Connexion de connexion = DriverManager
.getConnection(url, "scott", "tigre");
Instruction stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt
.executeQuery("select * from emp order 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.absolu(6);
System.out.println(rs.getString(1));
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Par exemple, UpdateResultSet
复制代码代码如下 :
importer java.sql.* ;
classe publique TestUpdataRs {
public static void main(String args[]){
essayer{
nouveau oracle.jdbc.driver.OracleDriver();
Chaîne url="jdbc:oracle:thin:@192.168.0.1:1521:SXT";
Connexion conn=DriverManager.getConnection(url,"scott","tiger");
Instruction stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("select * from 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("sal", 99,99);
rs.insertRow();
//将光标移动到新建的行
rs.moveToCurrentRow();
//删除行
rs.absolu(5);
rs.deleteRow();
//取消更新
//rs.cancelRowUpdates();
}catch(SQLException e){
e.printStackTrace();
}
}
}