一、Pernyataan
复制代码代码如下:
impor java.sql.*;
Tes kelas publikJDBC {
public static void main(String[] args) {
Koneksi Oracle_conn = null;
Pernyataan Oracle_stmt = null;
HasilSet Oracle_rs = null;
Koneksi mssql_conn = null;
Pernyataan mssql_stmt = null;
HasilSet mssql_rs = null;
mencoba {
Kelas.forName("Oracle.jdbc.driver.OracleDriver");
Oracle_conn = DriverManager.getConnection("jdbc:Oracle:thin:@192.168.0.215:1521:orcl", "nama pengguna", "kata sandi");
Oracle_stmt = Oracle_conn.createStatement();
Kelas.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
mssql_conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.213:1433;DatabaseName=VIS", "nama pengguna", "kata sandi");
mssql_stmt = mssql_conn.createStatement();
mssql_rs = mssql_stmt.executeQuery("pilih * dari VideoBaseInfo");
while(mssql_rs.next()) {
System.out.println("正在插入VideoId:" + mssql_rs.getInt("VideoId") + "的记录...");
Oracle_stmt.executeUpdate("masukkan ke nilai VIDEO_BASEINFO("
+ mssql_rs.getInt("VideoId") + ",'"
+ mssql_rs.getString("Nama Video") + "','"
+ mssql_rs.getString("Versi Video") + "',"
+ mssql_rs.getInt("VideoMp4Items") + ","
+ mssql_rs.getInt("VideoRmvbItems") + ",'"
+ mssql_rs.getString("NamaAlias Video") + "','"
+ mssql_rs.getString("Usia Video") + "'"
+ ")");
}
} tangkapan (ClassNotFoundException e) {
e.printStackTrace();
} tangkapan (SQLException e) {
e.printStackTrace();
} Akhirnya {
mencoba {
jika(Oracle_rs != nol) {
Oracle_rs.close();
Oracle_rs = nol;
}
jika(Oracle_stmt != nol) {
Oracle_stmt.close();
Oracle_stmt = nol;
}
jika(Oracle_conn != nol) {
Oracle_conn.close();
Oracle_conn = nol;
}
jika(mssql_rs != nol) {
mssql_rs.close();
mssql_rs = nol;
}
jika(mssql_stmt != nol) {
mssql_stmt.close();
mssql_stmt = nol;
}
jika(mssql_conn != nol) {
mssql_conn.close();
mssql_conn = nol;
}
} tangkapan (SQLException e) {
e.printStackTrace();
}
}
}
}
二、Pernyataan yang Disiapkan
复制代码代码如下:
impor java.sql.*;
kelas publik TestPreparedStatement {
public static void main(String[] args) {
Koneksi Oracle_conn = null;
Pernyataan Disiapkan Oracle_stmt = null;
HasilSet Oracle_rs = null;
Koneksi mssql_conn = null;
Pernyataan mssql_stmt = null;
HasilSet mssql_rs = null;
mencoba {
Kelas.forName("Oracle.jdbc.driver.OracleDriver");
Oracle_conn = DriverManager.getConnection("jdbc:Oracle:thin:@192.168.0.215:1521:orcl", "nama pengguna", "kata sandi");
Oracle_stmt = Oracle_conn.prepareStatement("masukkan ke dalam nilai Video_ItemInfo(?, ?, ?, ?, ?, ?, ?, ?, ?)");
Kelas.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
mssql_conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.213:1433;DatabaseName=VIS", "nama pengguna", "kata sandi");
mssql_stmt = mssql_conn.createStatement();
mssql_rs = mssql_stmt.executeQuery("pilih * dari 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("NamaItemVideo"));
Oracle_stmt.setString(4, mssql_rs.getString("VideoExtName"));
Oracle_stmt.setDouble(5, mssql_rs.getDouble("Ukuran Video"));
Oracle_stmt.setString(6, mssql_rs.getString("VideoPath"));
Oracle_stmt.setString(7, mssql_rs.getString("Jenis Video"));
Oracle_stmt.setDate(8, mssql_rs.getDate("Tanggal Video"));
Oracle_stmt.setString(9, mssql_rs.getString("Jalan Aplikasi"));
Oracle_stmt.executeUpdate();
}
System.out.println("插入数据到Video_ItemInfo表中操作已完成!");
} tangkapan (ClassNotFoundException e) {
e.printStackTrace();
} tangkapan (SQLException e) {
e.printStackTrace();
} Akhirnya {
mencoba {
jika(Oracle_rs != nol) {
Oracle_rs.close();
Oracle_rs = nol;
}
jika(Oracle_stmt != nol) {
Oracle_stmt.close();
Oracle_stmt = nol;
}
jika(Oracle_conn != nol) {
Oracle_conn.close();
Oracle_conn = nol;
}
jika(mssql_rs != nol) {
mssql_rs.close();
mssql_rs = nol;
}
jika(mssql_stmt != nol) {
mssql_stmt.close();
mssql_stmt = nol;
}
jika(mssql_conn != nol) {
mssql_conn.close();
mssql_conn = nol;
}
} tangkapan (SQLException e) {
e.printStackTrace();
}
}
}
}
misalnya CallableStatement
复制代码代码如下:
impor java.sql.*;
kelas publik TestProc {
/**
* @param argumen
*/
public static void main(String[] args) melempar Pengecualian {
Kelas.forName("Oracle.jdbc.driver.OracleDriver");
Sambungan koneksi = DriverManager.getConnection("jdbc:Oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
CallableStatement cstmt = conn.prepareCall("{panggilan p(?, ?, ?, ?)}");
cstmt.registerOutParameter(3, Tipe.INTEGER);
cstmt.registerOutParameter(4, Tipe.INTEGER);
cstmt.setInt(1, 3);
cstmt.setInt(2, 4);
cstmt.setInt(4, 5);
cstmt.execute();
Sistem.keluar.println(cstmt.getInt(3));
Sistem.keluar.println(cstmt.getInt(4));
cstmt.close();
samb.close();
}
}
四、Batch
复制代码代码如下:
impor java.sql.*;
TestBatch kelas publik {
public static void main(String[] args) melempar Pengecualian {
Kelas.forName("Oracle.jdbc.driver.OracleDriver");
Sambungan koneksi = DriverManager.getConnection("jdbc:Oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
/*
Pernyataan stmt = samb.createStatement();
stmt.addBatch("masukkan ke nilai dept2 (51, '500', 'haha')");
stmt.addBatch("masukkan ke nilai dept2 (52, '500', 'haha')");
stmt.addBatch("masukkan ke nilai dept2 (53, '500', 'haha')");
stmt.executeBatch();
stmt.tutup();
*/
PreparedStatement ps = conn.prepareStatement("masukkan ke nilai 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.tutup();
samb.close();
}
}
五、Transaksi
复制代码代码如下:
impor java.sql.*;
Kelas publik TestTransaction {
public static void main(String[] args) {
Sambungan koneksi = null;
Pernyataan stmt = null;
mencoba {
Kelas.forName("Oracle.jdbc.driver.OracleDriver");
samb = DriverManager.getConnection("jdbc:Oracle:thin:@127.0.0.1:1521:SXT", "scott", "harimau");
samb.setAutoCommit(salah);
stmt = samb.createStatement();
stmt.addBatch("masukkan ke nilai dept2 (51, '500', 'haha')");
stmt.addBatch("masukkan ke nilai dept2 (52, '500', 'haha')");
stmt.addBatch("masukkan ke nilai dept2 (53, '500', 'haha')");
stmt.executeBatch();
samb.commit();
samb.setAutoCommit(benar);
} tangkapan (ClassNotFoundException e) {
e.printStackTrace();
} tangkapan(SQLException e) {
e.printStackTrace();
mencoba {
jika(sambungan != nol)
{
samb.rollback();
samb.setAutoCommit(benar);
}
} tangkapan (SQLException e1) {
e1.printStackTrace();
}
}Akhirnya {
mencoba {
jika(stmt != nol)
stmt.tutup();
jika(sambungan != nol)
samb.close();
} tangkapan (SQLException e) {
e.printStackTrace();
}
}
}
}
六、ScrollResultSet
复制代码代码如下:
impor java.sql.*;
kelas publik TestScroll {
public static void main(String args[]) {
mencoba {
oracle.jdbc.driver.OracleDriver();
String url = "jdbc:Oracle:thin:@192.168.0.1:1521:SXT";
Sambungan koneksi = DriverManager
.getConnection(url, "scott", "harimau");
Pernyataan stmt = samb.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
HasilSet rs = stmt
.executeQuery("pilih * dari pesanan kosong oleh sal");
rs.next();
Sistem.keluar.println(rs.getInt(1));
rs.last();
Sistem.keluar.println(rs.getString(1));
Sistem.keluar.println(rs.isLast());
Sistem.keluar.println(rs.isAfterLast());
Sistem.keluar.println(rs.getRow());
rs.sebelumnya();
Sistem.keluar.println(rs.getString(1));
rs.mutlak(6);
Sistem.keluar.println(rs.getString(1));
rs.close();
stmt.tutup();
samb.close();
} tangkapan (SQLException e) {
e.printStackTrace();
}
}
}
七、UpdateResultSet
复制代码代码如下:
impor java.sql.*;
kelas publik TestUpdataRs {
public static void main(String args[]){
mencoba{
oracle.jdbc.driver.OracleDriver();
String url="jdbc:Oracle:thin:@192.168.0.1:1521:SXT";
Sambungan koneksi=DriverManager.getConnection(url,"scott","tiger");
Pernyataan stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=stmt.executeQuery("pilih * dari 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.mutlak(5);
rs.deleteRow();
//取消更新
//rs.cancelRowUpdates();
}menangkap(SQLException e){
e.printStackTrace();
}
}
}