zoukankan      html  css  js  c++  java
  • JDBC各种数据库连接URL关键代码

    通过JDBC连接数据库时,各个数据库有着不同的URL格式,为了方便大家使用,我在以下提供了常见的7种数据库连接示例代码,请根据实际需要进行相应的更改。

    1.Oracle数据库

    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

    String url = "jdbc:oracle:thin:@localhost:1521:orcle";

    String user = "test";

    String password = "test";

    Connection conn = DriverManager.getConnection(url, user, password);


    2.DB2数据库

    Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();

    String url = "jdbc:db2://localhost:5000/testDB";

    String user = "admin";

    String password = "test";

    Connection conn = DriverManager.getConnection(url, user, password);

    3.SQL Server数据库

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

    String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDB";

    String user = "sa";

    String password = "test";

    Connection conn = DriverManager.getConnection(url, user, password);

    4. Sybase数据库

    Class.forName("com.sybase.jdbc.SybDriver").newInstance();

    String url = "jdbc:sybase:Tds:localhost:5007/testDB";

    Properties sysProps = System.getProperties();

    sysProps.put("user", "userid");

    sysProps.put("password", "user_password");

    Connection conn = DriverManager.getConnection(url, sysProps);

    5.Informix数据库

    Class.forName("com.infoxmix.jdbc.IfxDriver").newInstance();

    String url = "jdbc:infoxmix-sqli://localhost:1533/testDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";

    Connection conn = DriverManager.getConnection(url);

    6.MySQL数据库

    Class.forName("org.gjt.mm.mysql.Driver").newInstance();

    String url = "jdbc:mysql://localhost/testDB?user=testuser&password=testpassword&useUnicode=true&characterEncoding=GB2312";

    Connection conn = DriverManager.getConnection(url);

    7.PostgreSQL数据库

    Class.forName("org.postgresql.Driver").newInstance();

    String url = "jdbc:postgresql://localhost/testDB";

    String user = "myuser";

    String password = "test";

    Connection conn = DriverManager.getConnection(url, user, password);

  • 相关阅读:
    HDU 1863 畅通工程(Kruskal)
    HDU 1879 继续畅通工程(Kruskra)
    HDU 1102 Constructing Roads(Kruskal)
    POJ 3150 Cellular Automaton(矩阵快速幂)
    POJ 3070 Fibonacci(矩阵快速幂)
    ZOJ 1648 Circuit Board(计算几何)
    ZOJ 3498 Javabeans
    ZOJ 3490 String Successor(模拟)
    Java实现 LeetCode 749 隔离病毒(DFS嵌套)
    Java实现 LeetCode 749 隔离病毒(DFS嵌套)
  • 原文地址:https://www.cnblogs.com/chenglc/p/8421573.html
Copyright © 2011-2022 走看看