db2 java连接字符,Java JDBC连接字符串汇总(MySQL,Postgres,SQL Server和DB2),postgresdb2,本文汇总了常见的Java...

燕鸿文
2023-12-01

Java JDBC连接字符串汇总(MySQL,Postgres,SQL Server和DB2),postgresdb2,本文汇总了常见的Java

本文汇总了常见的Java JDBC关系型数据库连接字符串,包括MySQL,Postgres,SQL Server和DB2。DatabaseURL (JDBC Connection String)JDBC DriverMySQLjdbc:mysql://HOST/DATABASEcom.mysql.jdbc.Driver

Postgresqljdbc:postgresql://HOST/DATABASEorg.postgresql.Driver

SQL Serverjdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASEcom.microsoft.jdbc.sqlserver.SQLServerDriver

DB2jdbc:as400://HOST/DATABASE;com.ibm.as400.access.AS400JDBCDriver

MySQL JDBC 使用示例 (JDBC URL)

Here's a sample MySQL JDBC connection string and JDBC driver string, taken from a Java properties file:

如下是在java的properties文件中配置mysql的jdbc连接字符串的示例:db_url = jdbc:mysql://HOST/DATABASEdb_driver = com.mysql.jdbc.Driverdb_username = USERNAMEdb_password = PASSWORD

下面是一个java连接MYSQL 执行查询的代码示例:try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://HOST/DATABASE"; conn = DriverManager.getConnection(url, "username", "password"); doTests(); conn.close();}catch (ClassNotFoundException ex) {System.err.println(ex.getMessage());}catch (IllegalAccessException ex) {System.err.println(ex.getMessage());}catch (InstantiationException ex) {System.err.println(ex.getMessage());}catch (SQLException ex) {System.err.println(ex.getMessage());}

如果你的mysql数据库监听的端口不是标准的3306,那么需要在连接字符串中指定端口号,如下使用了3336端口的示例:db_url = jdbc:mysql://HOST:3336/DATABASEdb_driver = com.mysql.jdbc.Driverdb_username = USERNAMEdb_password = PASSWORD

Postgresql JDBC 连接字符串示例

下面是一个Postgresql jdbc连接字符串的properties文件的示例:db_url = jdbc:postgresql://HOST/DATABASEdb_driver = org.postgresql.Driverdb_username = USERNAMEdb_password = PASSWORD

下面是java连接Postgresql的例子:Class.forName("org.postgresql.Driver");String url = "jdbc:postgresql://HOST/DATABASE";Connection conn = DriverManager.getConnection(url,"username", "password");

SQL Server JDBC连接字符串示例

如下java properties文件;db_url = jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASEdb_driver = com.microsoft.jdbc.sqlserver.SQLServerDriverdb_username = USERNAMEdb_password = PASSWORD

DB2 on an As/400 (iSeries) jdbc 连接示例

下面是jdbc连接db2数据库的示例,使用spring 配置文件配置。

当然你也可以在java properties文件中配置:db_url = jdbc:as400://HOST/DATABASE;db_driver = com.ibm.as400.access.AS400JDBCDriverdb_username = USERNAMEdb_password = PASSWORD

 类似资料: