Connecting to an RDBMS
The following code snippet illustrates how the connection parameters are used to retrieve a database connection.
1 |
try { |
2 |
Class.forName( "com.mysql.jdbc.Driver" ).newInstance(); //using MySQL JDBC Driver |
3 |
String url = "jdbc:mysql://hostname:port/database" ; //format of MySQL connection string |
4 |
conn = DriverManager.getConnection(url, "username" , "password" ); |
5 |
|
6 |
} catch (Exception e){...} |
7 |
finally { |
8 |
conn.close(); |
9 |
} |
JDBC Connection Strings
The following table lists the Connection Strings of some of the popular RDBMS.
DataBase Type | DriverClassName | Connection String |
---|---|---|
MySQL | com.mysql.jdbc.Driver | jdbc:mysql://<hostname>:<portNumber>/<databaseName>
Default host: 127.0.0.1 |
PostgreSql | org.postgresql.Driver | jdbc:postgresql://<hostname>:<portNumber>/<databaseName>
Default host: 127.0.0.1 |
SQLServer | com.microsoft.sqlserver.jdbc.SQLServerDriver | jdbc:sqlserver://<hostname>:<portNumber>;<databaseName>
Default host: 127.0.0.1 |
Oracle | oracle.jdbc.driver.OracleDriver | jdbc:oracle:thin:@<hostname>:<portNumber>:<databaseName>
Default host: 127.0.0.1 |
DB2 | COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver (DB2 JDBC Type 4 Driver) |
jdbc:db2://<hostname>:<portNumber>/<databaseName>
Default host: 127.0.0.1 |
DB2 (on As/400) |
com.ibm.as400.access.AS400JDBCDriver | jdbc:as400://<hostname>:<portNumber>/<databaseName>; |
Sybase | com.sybase.jdbc.SybDriver | jdbc:Sybase:Tds:<hostname>:<portNumber>/<databaseName> |
Sybase | net.sourceforge.jtds.jdbc.Driver (jTDS driver) |
jdbc:jtds:Sybase://<hostname>:<portNumber>/<databaseName> |
Sybase | com.sybase.jdbc2.jdbc.SybDriver (jdbc2 driver version) |
jdbc:Sybase:Tds:<hostname>:<port>?ServiceName=<databaseName> |
时间: 2024-10-08 09:11:25