`

(转)Java使用JDBC连接Oracle_MSSQL实例

阅读更多

转自:http://blog.csdn.net/tungkee/article/details/6620561

一、Statement
[java] view plaincopy
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
[java] view plaincopy
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
[java] view plaincopy
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
[java] view plaincopy
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
[java] view plaincopy
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
[java] view plaincopy
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
[java] view plaincopy
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连接Oracle_MSSQL实例代码

    本示例展示了如何使用JDBC连接Oracle和MSSQL两种不同类型的数据库,并执行SQL语句。首先,我们需要了解以下几个关键概念: 1. **驱动管理类**: 在Java中,`Class.forName()` 方法用于加载数据库驱动类。例如,...

    JDBC连接MsSQL/MYSQL/Oracle等多数据库的JSP代码.rar

    以及基于JDBC连接MsSQL/MYSQL/Oracle/Access等常用数据库的示例代码,比较经典的低级问题,笔者也曾遇到过当初学JSp的时候,曾经因为不会连接数据库而很苦恼,这次你可参考压缩包中的对应实例实战JSP连接各种常见的...

    kettle7连接oracle mssql的java驱动

    `ojdbc6.jar`是Oracle数据库使用的JDBC驱动,它允许Java应用程序与Oracle数据库进行通信。这个驱动是Oracle JDBC Thin Driver的一个版本,适用于Oracle 11g Release 2及更低版本。它是一个纯Java实现,无需本地库...

    oracle.mysql.mssql数据库JDBC连接

    下面,我们将深入探讨如何使用JDBC实现Oracle、MySQL和MSSQL数据库的连接。 ### JDBC简介 JDBC是一种用于执行SQL语句的Java API,它可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。...

    JSP_JDBC.rar_oracle

    以及基于JDBC连接MsSQL/MYSQL/Oracle/Access等常用数据库的示例代码,比较经典的低级问题,笔者也曾遇到过当初学JSp的时候,曾经因为不会连接数据库而很苦恼,这次你可参考压缩包中的对应实例实战JSP连接各种常见的...

    Jsp 使用JDBC连接各种数据库的实例代码

    以及基于JDBC连接MsSQL/MYSQL/Oracle/Access等常用数据库的示例代码,比较经典的低级问题,笔者也曾遇到过当初学JSp的时候,曾经因为不会连接数据库而很苦恼,这次你可参考压缩包中的对应实例实战JSP连接各种常见的...

    MSSQL2000与MSSQL2005 JDBC URL的区别

    通过JDBC,开发人员可以轻松地将Java应用程序连接到不同的数据库,并使用SQL语句执行数据操作。为了实现这一目标,JDBC提供了DriverManager类来管理各种驱动程序,并提供了一个标准接口来访问数据库。 ### MSSQL...

    4种数据库JDBC连接jar包及连接字符串

    Oracle使用的是Oracle JDBC Thin驱动,连接字符串如下: ```java url = "jdbc:oracle:thin:@服务器地址:端口号:服务名"; username = "用户名"; password = "密码"; Class.forName("oracle.jdbc.driver.OracleDriver...

    jdbc连接实例(sqlserver 2000/2005、 oracle 10g)

    本教程将详细介绍如何使用JDBC连接SQL Server 2000/2005和Oracle 10g数据库。 首先,让我们了解JDBC连接的基本步骤: 1. **加载驱动**:在Java程序中,我们需要加载对应的数据库驱动。例如,对于SQL Server,我们...

    java jsp sqlserver数据表转移到oracle实例 源代码

    2. **JDBC驱动**:Java使用JDBC(Java Database Connectivity)来连接数据库。需要分别为SQL Server和Oracle添加对应的JDBC驱动,如`mssql-jdbc`和`ojdbc`。 3. **SQL查询和事务管理**:Java代码中会包含读取SQL ...

    JAVA与SQLServer数据库简单连接实例.rar

    在Java编程语言中,与SQL Server...总的来说,这个压缩包提供的实例应该是一个基础的Java-SQL Server连接教程,涵盖了JDBC的核心概念和实践。通过学习和实践,你可以掌握在Java中与SQL Server数据库交互的基本技能。

    mysql,sqlserver,oracle,java连接池,连接程序

    对于SQL Server,我们需要使用对应的JDBC驱动,如mssql-jdbc。配置过程与MySQL类似,只是数据库连接参数会有所不同,例如使用`jdbc:sqlserver://<servername>:<port>`作为URL。 Oracle数据库的连接池配置则需要用到...

    JDBC连接实例

    **JDBC连接实例详解** Java Database Connectivity(JDBC)是Java平台中用于与数据库交互的一组接口和类,由Sun Microsystems(现为Oracle公司的一部分)开发。它为Java程序员提供了标准且统一的方法来访问各种...

    jdbc jar包(oracle+SQLServer+mySql)+连接各种数据库的方法总结。

    对于SQL Server,需要使用mssql-jdbc.jar。连接代码如下: ```java Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection( "jdbc:sqlserver://...

    JDBC 连接数据库的3个驱动jar包及使用方法

    2. **JDBC网络协议驱动**:这种类型的驱动,如MySQL的Connector/J,Oracle的JDBC驱动等,是纯Java实现,可以直接与数据库服务器通过TCP/IP通信。这些驱动通常由数据库供应商提供,性能优于JDBC-ODBC桥驱动,因为它们...

    sqljdbc42 jdbc for java

    在Java编程中,数据库操作是不可或缺的一部分,而SQLJDBC42则是Oracle公司提供的一款高效、可靠的Java数据库连接(JDBC)驱动程序,专为Java开发者设计,使得Java应用程序能够与SQL Server数据库进行无缝交互。...

    常用jdbc连接方法

    - 使用微软官方的JDBC驱动,对应JAR包如`mssql-jdbc.jar`。 - 加载驱动:`Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")`。 - 连接数据库:`cn = DriverManager.getConnection("jdbc:microsoft...

    java连接各种数据库大全

    使用JDBC连接Oracle,你需要下载并引入Oracle的JDBC驱动ojdbc.jar。连接代码通常包括加载驱动、建立连接和关闭连接: ```java Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn = ...

    java开发数据库连接

    总的来说,Java开发者在处理MySQL、SQL Server和Oracle等数据库时,需要了解JDBC接口和相应的驱动程序,并可能采用连接池技术来优化数据库操作。理解这些基础知识对于任何Java DBA或后端开发者都是至关重要的。

    mysql,sqlServer,oracle 驱动包 for java

    - 对于SQL Server,Java应用通常使用Microsoft提供的JDBC驱动,如`sqljdbc_auth.dll`和`mssql-jdbc.jar`(适用于SQL Server 2017及以上版本),或早期的`jtds.jar`第三方驱动。 - SQL Server JDBC驱动支持身份验证...

Global site tag (gtag.js) - Google Analytics