一、성명
复代码代码如下:
import java.sql.*;
공개 클래스 TestJDBC {
공개 정적 무효 메인(String[] 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에서 * 선택");
동안(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") + "'"
+ ")");
}
} 잡기(ClassNotFoundException e) {
e.printStackTrace();
} 잡기(SQLException e) {
e.printStackTrace();
} 마지막으로 {
노력하다 {
if(oracle_rs != null) {
oracle_rs.close();
oracle_rs = null;
}
if(oracle_stmt != null) {
oracle_stmt.close();
oracle_stmt = null;
}
if(oracle_conn != null) {
oracle_conn.close();
oracle_conn = null;
}
if(mssql_rs != null) {
mssql_rs.close();
mssql_rs = null;
}
if(mssql_stmt != null) {
mssql_stmt.close();
mssql_stmt = null;
}
if(mssql_conn != null) {
mssql_conn.close();
mssql_conn = null;
}
} 잡기(SQLException e) {
e.printStackTrace();
}
}
}
}
2、PreparedStatement
复代码代码如下:
import java.sql.*;
공개 클래스 TestPreparedStatement {
공개 정적 무효 메인(String[] args) {
연결 oracle_conn = null;
ReadyStatement 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에서 * 선택");
동안(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表中操작성已完成!");
} 잡기(ClassNotFoundException e) {
e.printStackTrace();
} 잡기(SQLException e) {
e.printStackTrace();
} 마지막으로 {
노력하다 {
if(oracle_rs != null) {
oracle_rs.close();
oracle_rs = null;
}
if(oracle_stmt != null) {
oracle_stmt.close();
oracle_stmt = null;
}
if(oracle_conn != null) {
oracle_conn.close();
oracle_conn = null;
}
if(mssql_rs != null) {
mssql_rs.close();
mssql_rs = null;
}
if(mssql_stmt != null) {
mssql_stmt.close();
mssql_stmt = null;
}
if(mssql_conn != null) {
mssql_conn.close();
mssql_conn = null;
}
} 잡기(SQLException e) {
e.printStackTrace();
}
}
}
}
3, CallableStatement
复代码代码如下:
import java.sql.*;
공개 클래스 TestProc {
/**
* @param 인수
*/
public static void main(String[] 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();
}
}
넷, 일괄
复代码代码如下:
import java.sql.*;
공개 클래스 TestBatch {
public static void main(String[] 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', 'haha'))에 삽입");
stmt.addBatch("dept2 값(52, '500', 'haha'))에 삽입");
stmt.addBatch("dept2 값(53, '500', 'haha'))에 삽입");
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();
}
}
五、거래
复代码代码如下:
import java.sql.*;
공개 클래스 TestTransaction {
공개 정적 무효 메인(String[] args) {
연결 연결 = 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(false);
stmt = conn.createStatement();
stmt.addBatch("dept2 값(51, '500', 'haha'))에 삽입");
stmt.addBatch("dept2 값(52, '500', 'haha'))에 삽입");
stmt.addBatch("dept2 값(53, '500', 'haha'))에 삽입");
stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} 잡기(ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
노력하다 {
if(콘!= 널)
{
conn.rollback();
conn.setAutoCommit(true);
}
} 잡기(SQLException e1) {
e1.printStackTrace();
}
}마지막으로 {
노력하다 {
if(stmt != null)
stmt.close();
if(콘!= 널)
conn.close();
} 잡기(SQLException e) {
e.printStackTrace();
}
}
}
}
六、ScrollResultSet
复代码代码如下:
import java.sql.*;
공개 클래스 TestScroll {
공개 정적 무효 메인(문자열 인수[]) {
노력하다 {
새로운 oracle.jdbc.driver.OracleDriver();
문자열 url = "jdbc:oracle:thin:@192.168.0.1:1521:SXT";
연결 연결 = DriverManager
.getConnection(url, "스콧", "호랑이");
명령문 stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
결과 집합 rs = stmt
.executeQuery("sal의 emp 순서에서 *를 선택하세요");
RS.다음();
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.이전();
System.out.println(rs.getString(1));
rs.absolute(6);
System.out.println(rs.getString(1));
rs.close();
stmt.close();
conn.close();
} 잡기(SQLException e) {
e.printStackTrace();
}
}
}
七、UpdateResultSet
复代码代码如下:
import 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.다음();
//새로운 일행数据
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.absolute(5);
rs.deleteRow();
//取消更新
//rs.cancelRowUpdates();
}catch(SQLException e){
e.printStackTrace();
}
}
}