一样啦,只不过jdbc driver不一样,url格式不一样罢了

解决方案 »

  1.   

    对应不同的数据库,只是dburl格式不一样罢了。
    比如我访问本地sql server数据库
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection(dbUrl, user, password);
    访问其它的数据库,dburl不同就是了
    当然,首先是你得有该数据库的驱动,或者到控制面版-管理工具-数据源-选择系统DSN,添加对应数据库的数据源(选择对应数据库驱动)即可。
      

  2.   

    if (getDbType().equalsIgnoreCase("Sybase"))//create the connection to the Sybase with the jdbc thin driver and  the parameters
                      {
                      DriverManager.registerDriver(new SybDriver());
           conn = DriverManager.getConnection ("jdbc:sybase:Tds:" + getDbServer()  + ":" + getDbPort() + "/" + getDbSID()+"?charset=eucgb", getDbUserID(), getDbPassword());
                      }
                    else if(getDbType().equalsIgnoreCase("Oracle"))//create the connection to the Oracle with the jdbc thin driver and  the parameters
                      {
                      DriverManager.registerDriver(new OracleDriver());
                      conn = DriverManager.getConnection ("jdbc:oracle:thin:@"+getDbServer()  + ":" + getDbPort() + ":" + getDbSID(), getDbUserID(), getDbPassword());
                      }
                    statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
      

  3.   

    兄弟我可以告诉你
    如果你在Windows平台下干活的话。请你还是使用ODBC桥吧。
    如果你在Linux/Unix平台下作的话。你可以用JDBC的第四类驱动。
    比如mysql在网站有mmsql的第四类驱动是用纯java写的真正跨平台
    比如oracle自带第四类驱动。
      

  4.   

    to chu888(皮皮鲁) 
    如果你在Windows平台下干活的话。请你还是使用ODBC桥吧。
    不太赞同你的观点,撇开速度不谈,
    如果机器上没有装Oracle的客户端,你用ODBC桥能连起来吗?
      

  5.   

    如果用ODBC桥的化,用java的无平台的性质就不能很好的体现,我想让系统能自由跨度,而不需要设置过多.
    其实我需要的是DriverManger的包,还有就是url,请各位兄弟能支持我.
      

  6.   

    Access数据库:
    public DataBase(String dbURL, DrawFrame frame){
           try{
               df = frame;
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               if( dbURL == null )
                  url = "jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=rolepay.mdb";
               else   
                  url = "jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=" + dbURL;
               connection = DriverManager.getConnection(url, "", "");           
               DatabaseMetaData md = connection.getMetaData();           
               String[] type = new String[1];
               type[0] = "TABLE";
               int i = 0;
               ResultSet  rs = md.getTables(null,null,"%",type);
               while(rs.next()){
                  i++;
               }
               tableName = new String[i];
               i=0;
               rs = md.getTables(null,null,"%",type);
               while(rs.next()){
                  tableName[i++] = rs.getString(3);
               }
           }
           catch(SQLException e){ 
            e.printStackTrace(); 
           }
           catch(ClassNotFoundException e){ e.printStackTrace();}
        }
      

  7.   

    非常感谢楼上的兄弟,我需要的是JDBC的连接,
    不需要JDBC-ODBC桥的解决方案,不过我也非常感谢你.
      

  8.   

    你先下载你想要连接数据库的jdbc驱动程序
    一般在他的帮助文件里都有连接说明
      

  9.   

    大哥如果你已经搞定了Sql server与java连接,使用微软提供的jdbc的接口,给小弟发一份成功样码好吗?
      

  10.   

    建议使用jndi服务,制作一个properties文件,然后使用datasource作为数据库连接,这样只需修改文件中的属性和datasource就可以连接不同的数据库了。
      

  11.   

    同意  thales(thales) 的这样很灵活,你也不用反复编译你的代码了.但不一定要用JNDI,用一个类的静态方法读入DB连接的信息.然后其他的连接从这个方法中取得信息进行连接.
      

  12.   

    public static Connection getConnection() throws Exception {
        Connection conn=null;
        String sDBDriver=Page.getDbDriver();
        String sConnUrlStr=Page.getConnUrl();
        try {
    Class.forName(sDBDriver);
    } catch (java.lang.ClassNotFoundException e) {
    System.out.println("Class forName Error:"+e.getMessage());
    }
        try {
    conn=DriverManager.getConnection(sConnUrlStr,Page.getDbUserID(),Page.getDbPassword());
          System.out.println("connection successfule!");
    } catch (SQLException ex) {
    System.out.println("getConnection Error:"+ex.getMessage());
    }
        return conn;  
    }
    注:Page的四个方法分别是为了取得属性文件中常量值。
    举例说明:(DBMS:DB2,DB name:WZCG,server name:wz-server)
     JDBC连接:  sDBDriver:COM.ibm.db2.jdbc.net.DB2Driver
                sConnUrlStr: jdbc:db2://wz-server/wzcg
     ODBC连接:  sDBDriver:sun.odbc.jdbc.JdbcOdbcDriver
                sConnUrlStr: jdbc:odbc:wzcg或者:
     public static Connection getConnection() throws Exception {
        InitialContext ic=new InitialContext();
        DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/wzcg");
        Connection conn=ds.getConnection();
        return conn;
      }
    注:lookup的常量是java:comp/env/加自己在WEBSERVER中定义的名字
    如用的是RESIN,则配置文件中应有:
        <resource-ref>
          <res-ref-name>jdbc/wzcg</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <init-param driver-name="COM.ibm.db2.jdbc.net.DB2Driver"/>
          <init-param url="jdbc:db2://wz-server/wzcg"/>
          <init-param user="db2admin"/>
          <init-param password="ok12"/>
          <init-param max-connections="20"/>
          <init-param max-idle-time="30"/>
        </resource-ref>
      

  13.   

    可以采用jdbc转odbc桥就可以实现了。
      

  14.   

    其实就是你要使用不同的jdbc driver,其他的设置都大同小议。这个问题没什么好讨论的了!除非针对不同的服务要用连接池,那也是服务设置的事