JDBC Database Connection URLs for Common Databases
- Details
- Written by Nam Ha Minh
- Last Updated on 02 March 2022   |   Print Email
This post lists JDBC database URLs for common databases so you can refer when programming database in Jjava.
You know, when working with a database system via JDBC, the following information is required for making connection to the database:
- Driver class name: is name of the class that implements java.sql.Driver interface. The JDBC’s driver manager needs to load this class in order to work with the database driver.
- Database URL: a string that contains information about the database to connect to and other configuration properties. This string has its own format and is varied among different databases.
For reference, this article provides a summary of JDBC’s database connection URLs for the most common databases including MySQL, SQL Server, Oracle, PostgreSQL, Apache Derby (Java DB), SQLite and H2.
1. JDBC Database URL for MySQL
- Driver class name: com.mysql.jdbc.Driver
- Format of database URL:
jdbc:mysql://[host][,failoverhost...][:port]/[database]
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
See more information about MySQL connection properties.
- Examples:
- jdbc:mysql://localhost:3306/test
- jdbc:mysql://localhost:3306/test?user=root&password=secret
- Examples:
2. JDBC Database URL for SQL Server
- Driver class name: com.microsoft.sqlserver.jdbc.SQLServerDriver
- Format of database URL:
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
- See more information about SQL Server connection properties.
- Examples:
jdbc:sqlserver://localhost;integratedSecurity=true;
jdbc:sqlserver://localhost\\sqlexpress;integratedSecurity=true
jdbc:sqlserver://localhost\\sqlexpress;user=sa;password=secret
3. JDBC Database URL for Oracle
- Driver class name: oracle.jdbc.OracleDriver
- Format of database URL:
jdbc:oracle:<drivertype>:@<database>
jdbc:oracle:<drivertype>:<user>/<password>@<database>
where drivertype
can be thin, oci or kprb.
- Examples:
jdbc:oracle:thin:@localhost:1521:testdb
jdbc:oracle:thin:root/secret@localhost:1521:testdb
jdbc:oracle:oci:@hoststring
jdbc:oracle:oci:@localhost:1521:testdb
jdbc:oracle:oci:root/secret@hoststring>
Jdbc:oracle:oci:root/secret@localhost:1521:testdb
- See more information about Oracle driver type and connection properties.
- View connection code example for Oracle database.
4. JDBC Database URL for PostgreSQL
- Driver class name: org.postgresql.Driver
- Format of database URL:
jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
- See more information about Postgre’s connection parameters.
- Examples:
jdbc:postgresql:testdb
jdbc:postgresql://localhost/testdb
jdbc:postgresql://localhost:5432/testdb
5. JDBC Database URL for JavaDB (Apache Derby)
- Driver class name: org.apache.derby.jdbc.EmbeddedDriver
- Format of database URL:
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
where subsubprotocol can take one of the following values: directory (default), memory, classpath, and jar.
- Examples:
- jdbc:derby:testdb
- jdbc:derby:centraldb/sales
- jdbc:derby:sample;create=true
- jdbc:derby:memory:testdb
- jdbc:derby:classpath:testdb
- jdbc:derby:directory:testdb
- jdbc:derby:jar(C:/testdb.jar)/market/asia
- See more information about Derby JDBC database connection URL.
- View code example for Apache Derby .
- Examples:
6. JDBC Database URL for SQLite
Because there is no official JDBC driver for SQLite from www.sqlite.org, the following information is applied for a SQLite JDBC driver provided by www.xerial.org.
- Driver class name: org.sqlite.JDBC
- Format of database URL:
jdbc:sqlite:database_file_path
Where database_file_path points to relative path or absolute path of a SQLite database file.
Syntax for in-memory SQLite database:
jdbc:sqlite::memory:
jdbc:sqlite:
- Examples:
- jdbc:sqlite:sample.db
- jdbc:derby:D:/work/project/data.db
- View JDBC code example for SQLite.
- Examples:
NOTE: From JDBC 4.0, loading the driver class explicitly is no longer needed, so you don’t need to care about the driver class name.
7. JDBC Database URL for H2
Driver class name: org.h2.Driver
Database URLs for Embedded Mode:
- jdbc:h2:~/test('test' database in user home directory)
- jdbc:h2:/data/test('test' database in /data directory - Unix)
- jdbc:h2:D:/data/test ('test' database in D:/data directory - Windows)
- jdbc:h2:./test ('test' datase in current directory)
Database URLs for In-Memory Mode:
- jdbc:h2:mem:test (multiple connections in one process)
- jdbc:h2:mem: (unamed private; one connection)
Database URLs for Server Mode:
- jdbc:h2:tcp://localhost/~/test ('test' database in user home)
- jdbc:h2:tcp://localhost//data/test ('test' database in /data directory - Unix)
- jdbc:h2:tcp://localhost/D:/data/test ('test' database in D:/data directory - Windows)
Related JDBC Tutorials:
- JDBC Drivers Download
- How to connect to a database with JDBC
- JDBC Create, Retrieve, Update and Delete (CRUD) Tutorial
- JDBC Transaction Tutorial
- How to call stored procedure with JDBC
- How to read database metadata in JDBC
- How to insert binary data into database with JDBC
- How to read binary data from database with JDBC
- How to use Scrollable ResultSet
- How to use Updatable ResultSet
- How to use CachedRowSet
Comments