Thursday, 31 March 2016

Database connection URLs for common databases


When working with a database system via JDBC, the following information is required for making connection to the database :
  • Driver class name
    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.

1. MySQL

Driver class name : com.mysql.jdbc.Driver
Format of database URL :  jdbc:mysql:// [host] [,failoverhost...] [:port] / [database]
                       [?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
Examples : 
jdbc:mysql://localhost:3306/test
jdbc:mysql://localhost:3306/test?user=root&password=secret


2. SQL Server

Driver class name : com.microsoft.sqlserver.jdbc.SQLServerDriver
Format of database URL: jdbc:sqlserver:// [serverName [\instanceName] 
                                             [:portNumber] ] [;property=value[;property=value]]
Examples :
jdbc:sqlserver://localhost;integratedSecurity=true;
jdbc:sqlserver://localhost\\sqlexpress;integratedSecurity=true
jdbc:sqlserver://localhost\\sqlexpress;user=sa;password=secret


3. Oracle

Driver class name : oracle.jdbc.OracleDriver
Format of database URL : jdbc:oracle:<driver-type>:@<database>
                                             jdbc:oracle:<driver-type>:<user>/<password>@<database>
where driver-type 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


4. PostgreSQL

Driver class name : org.postgresql.Driver
Format of database URL : jdbc:postgresql:<database>
                                             jdbc:postgresql://<host>/<database>
                                             jdbc:postgresql://<host>:<port>/<database>
Examples : 
jdbc:postgresql:testdb
jdbc:postgresql://localhost/testdb
jdbc:postgresql://localhost:5432/testdb


5. 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

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.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.