Java使用JDBC连接Oracle & MSSQL实例

  • A+
所属分类:Linux-数据库
摘要

Java使用JDBC连接Oracle & MSSQL实例

  1. import java.sql.*;  
  2.   
  3. public class TestJDBC {  
  4.   
  5.     public static void main(String[] args) {  
  6.         Connection oracle_conn = null;  
  7.         Statement oracle_stmt = null;  
  8.         ResultSet oracle_rs = null;  
  9.           
  10.         Connection mssql_conn = null;  
  11.         Statement mssql_stmt = null;  
  12.         ResultSet mssql_rs = null;  
  13.                   
  14.         try {  
  15.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  16.             oracle_conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.215:1521:orcl""username""password");  
  17.               
  18.             oracle_stmt = oracle_conn.createStatement();  
  19.               
  20.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
  21.             mssql_conn = DriverManager.getConnection("jdbc:sqlserver://192.168.0.213:1433;DatabaseName=VIS""username""password");  
  22.               
  23.             mssql_stmt = mssql_conn.createStatement();  
  24.             mssql_rs = mssql_stmt.executeQuery("select * from VideoBaseInfo");  
  25.               
  26.             while(mssql_rs.next()) {  
  27.                 System.out.println("正在插入VideoId:" + mssql_rs.getInt("VideoId") + "的记录...");  
  28.                 oracle_stmt.executeUpdate("insert into VIDEO_BASEINFO values("   
  29.                         + mssql_rs.getInt("VideoId") + ",'"  
  30.                         + mssql_rs.getString("VideoName") + "','"  
  31.                         + mssql_rs.getString("VideoVersion") + "',"  
  32.                         + mssql_rs.getInt("VideoMp4Items") + ","  
  33.                         + mssql_rs.getInt("VideoRmvbItems") + ",'"  
  34.                         + mssql_rs.getString("VideoAliasName") + "','"  
  35.                         + mssql_rs.getString("VideoAge") + "'"  
  36.                         + ")");  
  37.             }  
  38.               
  39.         } catch (ClassNotFoundException e) {  
  40.             e.printStackTrace();  
  41.         } catch (SQLException e) {  
  42.             e.printStackTrace();  
  43.         } finally {  
  44.             try {  
  45.                 if(oracle_rs != null) {  
  46.                     oracle_rs.close();  
  47.                     oracle_rs = null;  
  48.                 }  
  49.                   
  50.                 if(oracle_stmt != null) {  
  51.                     oracle_stmt.close();  
  52.                     oracle_stmt = null;  
  53.                 }  
  54.                   
  55.                 if(oracle_conn != null) {  
  56.                     oracle_conn.close();  
  57.                     oracle_conn = null;  
  58.                 }  
  59.                   
  60.                 if(mssql_rs != null) {  
  61.                     mssql_rs.close();  
  62.                     mssql_rs = null;  
  63.                 }  
  64.                   
  65.                 if(mssql_stmt != null) {  
  66.                     mssql_stmt.close();  
  67.                     mssql_stmt = null;  
  68.                 }  
  69.                   
  70.                 if(mssql_conn != null) {  
  71.                     mssql_conn.close();  
  72.                     mssql_conn = null;  
  73.                 }  
  74.             } catch (SQLException e) {  
  75.                 e.printStackTrace();  
  76.             }  
  77.         }  
  78.     }  
  79.   
  80. }  

除非注明,否则均为@蒲公英网原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.aipgy.com/51071.html

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: