java  -Djdbc.drivers=oracle.jdbc.driver.OracleDriver classname 这样注册drivers后,怎么用呢?

解决方案 »

  1.   

    我是想问我写一个java程序访问数据库,不用Class.forName()来注册driver,而在编译的时候执行java  -Djdbc.drivers=oracle.jdbc.driver.OracleDriver classname,那么怎么用这个driver来获取数据库数据
      

  2.   

    一个例子:
    public static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
    public static final String ORACLE_URL = "jdbc:oracle:thin:@192.168.0.23:1521:test"; public static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
    public static final String MYSQL_URL = "jdbc:mysql://127.0.0.1:3306/jdbcguides"; public static Connection getConnection() {
    return getMySQLConnection();
    } public static Connection getOracleConnection() {
    try {
    // register driver
    Class.forName(ORACLE_DRIVER);
    // get connection
    Connection con = DriverManager.getConnection(ORACLE_URL, "openlab",
    "open123");
    return con;
    } catch (ClassNotFoundException e) {
    System.out.println("failed to register driver.");
    throw new RuntimeException(e);
    } catch (SQLException e) {
    System.out.println("failed to execute sql.");
    throw new RuntimeException(e);
    }