一、Statement

import java.sql.*;  

public class TestJDBC {  

    public static void main(String[] args) {
Connection oracle_conn = null;
Statement oracle_stmt = null;
ResultSet oracle_rs = null; Connection mssql_conn = null;
Statement mssql_stmt = null;
ResultSet mssql_rs = null; try {
Class.forName("oracle.jdbc.driver.OracleDriver");
oracle_conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.215:1521:orcl", "username", "password"); 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", "username", "password"); 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("insert into VIDEO_BASEINFO values("
+ 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();
} finally {
try {
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;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} }
import java.sql.*; public class TestJDBC { public static void main(String[] args) {
Connection oracle_conn = null;
Statement oracle_stmt = null;
ResultSet oracle_rs = null; Connection mssql_conn = null;
Statement mssql_stmt = null;
ResultSet mssql_rs = null; try {
Class.forName("oracle.jdbc.driver.OracleDriver");
oracle_conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.215:1521:orcl", "username", "password"); 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", "username", "password"); 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("insert into VIDEO_BASEINFO values("
+ 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();
} finally {
try {
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;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} }

二、PreparedStatement

import java.sql.*;

public class TestPreparedStatement {

	public static void main(String[] args) {
Connection oracle_conn = null;
PreparedStatement oracle_stmt = null;
ResultSet oracle_rs = null; Connection mssql_conn = null;
Statement mssql_stmt = null;
ResultSet mssql_rs = null; try {
Class.forName("oracle.jdbc.driver.OracleDriver");
oracle_conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.215:1521:orcl", "username", "password"); oracle_stmt = oracle_conn.prepareStatement("insert into Video_ItemInfo values(?, ?, ?, ?, ?, ?, ?, ?, ?)"); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
mssql_conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.213:1433;DatabaseName=VIS", "username", "password"); 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();
} finally {
try {
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;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
} }

三、CallableStatement

import java.sql.*;
public class TestProc { /**
* @param args
*/
public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver");
Connection 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();
} }

四、Batch

import java.sql.*;
public class TestBatch { public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
/*
Statement stmt = conn.createStatement();
stmt.addBatch("insert into dept2 values (51, '500', 'haha')");
stmt.addBatch("insert into dept2 values (52, '500', 'haha')");
stmt.addBatch("insert into dept2 values (53, '500', 'haha')");
stmt.executeBatch();
stmt.close();
*/ PreparedStatement ps = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
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(); } }

五、Transaction

import java.sql.*;
public class TestTransaction { public static void main(String[] args) { Connection conn = null;
Statement stmt = null; try {
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("insert into dept2 values (51, '500', 'haha')");
stmt.addBatch("insert into dept2 values (52, '500', 'haha')");
stmt.addBatch("insert into dept2 values (53, '500', 'haha')");
stmt.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) { e.printStackTrace(); try {
if(conn != null)
{
conn.rollback();
conn.setAutoCommit(true);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}finally {
try {
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} } }

六、ScrollResultSet

import java.sql.*;

public class TestScroll {
public static void main(String args[]) { try {
new oracle.jdbc.driver.OracleDriver();
String url = "jdbc:oracle:thin:@192.168.0.1:1521:SXT";
Connection conn = DriverManager
.getConnection(url, "scott", "tiger");
Statement 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.absolute(6);
System.out.println(rs.getString(1));
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

七、UpdateResultSet

import java.sql.*;
public class TestUpdataRs {
public static void main(String args[]){ try{
new oracle.jdbc.driver.OracleDriver();
String url="jdbc:oracle:thin:@192.168.0.1:1521:SXT";
Connection conn=DriverManager.getConnection(url,"scott","tiger");
Statement 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.absolute(5);
rs.deleteRow(); //取消更新
//rs.cancelRowUpdates(); }catch(SQLException e){
e.printStackTrace();
}
}
}

java_JDBC(4)的更多相关文章

  1. JAVA_JDBC

    测试类: 1 import java.util.ArrayList; import java.util.List; /** * 创建数据库: * 1.加载驱动 * Class.forName(&quo ...

  2. java_JDBC字段对应

    地址: http://otndnld.oracle.co.jp/document/products/oracle10g/102/doc_cd/java.102/B19275-03/datacc.htm ...

  3. java_JDBC(3)

    Batch和Fetch两个特性非常重要.Batch相当于JDBC的写缓冲,Fetch相当于读缓冲 如果把JDBC类比为JAVA IO的话,不使用Fetch和Batch相当于直接使用FileInputS ...

  4. java_JDBC(2)

    1.Statement 每次执行sql语句,数据库都要执行sql语句的编译 ,最好用于仅执行一次查询并返回结果的情形,效率高于PreparedStatement. 2.PreparedStatemen ...

  5. java_JDBC(1)

    Java连接Oracle步骤: 1.注册加载驱动 驱动名:DRIVER="oracle.jdbc.driver.OracleDriver";Class.forName(" ...

  6. Java_JDBC一般写法

    JDBC是Java DataBase Connectivity,Java程序访问数据库的标准接口. 如果是maven工程先加入依赖的jar包: <dependency> <group ...

  7. Java_JDBC连接数据库_使用读取配置文件的方式

    package com.homewoek3_4.dao; import java.io.IOException; import java.io.InputStream; import java.sql ...

  8. Java_JDBC连接数据库

    package com.accp.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pre ...

  9. Java_JDBC 连接

    今天,接着上一篇( mysql 数据库 )的基础上,我就写一下 Java 怎样连接数据库,并且操作数据库. 首先,我们先来准备一下数据库连接的驱动: mysql 的 jar 包下载地址:https:/ ...

随机推荐

  1. linux下文件和目录的颜色表示

    蓝色表示目录: 绿色表示可执行文件: 红色表示压缩文件: 浅蓝色表示链接文件: 灰色表示其它文件: 红色闪烁表示链接的文件有问题了: 黄色是设备文件,包括block, char, fifo. (摘自: ...

  2. /etc/profile文件

    导读:Linux /etc/profile文件的改变会涉及到系统的环境,也就是有关Linux环境变量的东西,学习Linux要了解Linux profile文件的相关原理,这里对则以文件进行具体分析.这 ...

  3. iOS开发——NSArray中的字典排序

    手头上碰到一个项目,需要给数组中的字典中的一个字段排序,想了想,干脆再字典中增加一个字段,用来记录需要排序字段的第一个字符,用它来作为比较的对象,进行排序. - (void)viewDidLoad { ...

  4. asp.net 输出Excel

    private void lbtExportToExcel_Click(object sender, EventArgs e) { string strdate = DateTime.Now.Mont ...

  5. hibernate---性能优化, 1+N问题

    session级缓存 保存一个hashmap, 读出来的对象放在里面, 如果读出来50条放50条, 如果另起session原来的50条就被清除.可以手动session.clear清除. 如果同一个se ...

  6. Servlet中的过滤器Filter用法

    1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对Http ...

  7. Codeforces 242E:XOR on Segment(位上的线段树)

    http://codeforces.com/problemset/problem/242/E 题意:给出初始n个数,还有m个操作,操作一种是区间求和,一种是区间xor x. 思路:昨天比赛出的一道类似 ...

  8. R Student Companion(R语言初学指南)的源代码_数据_插图

    下载内容见附件:http://files.cnblogs.com/files/ml-cv/data_And_R_script.zip.

  9. java8 泛型声明 The diamond operator ("<>") should be used

    The diamond operator ("<>") should be used Java 7 introduced the diamond operator (& ...

  10. Spring.NET 的IOC(依赖注入)

    (1)  ioc,意思是Inversion of control,(反转控制),控制反转,就是交换控制权的意思.现在一般不使用这个词,一般使用dependency injection(依赖注入).依赖 ...