JDBC连接各种数据库的字符串



oracle
    driverClass:oracle.jdbc.driver.OracleDriver
    url:jdbc:oracle:thin:@127.0.0.1:1521:dbname
mysql
    driverClass:com.mysql.jdbc.Driver
    url:jdbc:mysql://localhost:3306/mydb
DB2
    driverClass:com.ibm.db2.jcc.DB2Driver
    url:jdbc:db2://127.0.0.1:50000/dbname
sybase
    driverClass:com.sybase.jdbc.SybDriver
    url:jdbc:sybase:Tds:localhost:5007/dbname
PostgreSQL
    driverClass:org.postgresql.Driver
    url:jdbc:postgresql://localhost/dbname
Sql Server2000
    driverClass:com.microsoft.jdbc.sqlserver.SQLServerDriver
    url:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dbname
Sql Server2005
    driverClass:com.microsoft.sqlserver.jdbc.SQLServerDriver
    url:jdbc:sqlserver://localhost:1433; DatabaseName=dbname


package com.java.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class OracleConTest {

 public static void main(String[] args) {
  String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
  Connection con = null;
  Statement st = null;
  ResultSet rs = null;
  
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   con = DriverManager.getConnection(url, username, password);
   st = con.createStatement();
   rs = st.executeQuery("select * from table");
   while(rs.next()){
    System.out.println(rs.getString("user") );
   }
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   if(rs != null){
    try {
     rs.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
   
   if(st != null){
    try {
     st.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
   
   if(con != null){
    try {
     con.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
 }

}

你可能感兴趣的:(JDBC连接各种数据库的字符串)