Java connect to database

Connecting to the SQL database in Java is very simple and takes only two lines of code.

Java connect to database – the code

import java.sql.Connection;  
import java.sql.DriverManager;

...

// Load the JDBC Driver
Class.forName("[JDBC-DRIVER-GOES-HERE]");
// Establish the connection
Connection connection = DriverManager.getConnection("[CONNECTION-URL]", "[USERNAME]", "[PASSWORD]");

// Use the connection
...

// !! Close the connection
connection.close();

Where:
[JDBC-DRIVER-GOES-HERE] is the JDBC Driver name
[CONNECTION-URL] is the JDBC connection URL
[USERNAME] is the database username
[PASSWORD] is the database username’s password

In the first line (line 7) you are loading JDBC driver. The second line (line 9) is creating the connection. To run this code you need the JDBC Driver which comes with your database engine. The JDBC Driver needs to be present in your program’s CLASSPATH. Very important thing is to remember to close the connection after it has been used. You don’t want to multiply opened connections because you can easily reach the database limit and no new connections will open. Your program needs to manage database connections in some way.

Here is how you connect to the popular database engines:
Connect to the Oracle database
Connect to the MySQL database
Connect to the DB2 database
Connect to the MS SQL database
Connect to the Derby database
Connect to the HSQLDB database
Connect to the SQLite database
Connect to the Firebird database

Connect to the Oracle database

// Load the JDBC Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Establish the connection
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@[HOST]:[PORT]:[DB_NAME]", "[USERNAME]", "[PASSWORD]");

Where
[HOST] is your database IP address or host name.
[PORT] is your database port (the default one is 1521)
[DB_NAME] is your database name
[USERNAME] is your database username
[PASSWORD] is your database user password

Google the Oracle JDBC Driver download

Connect to the MySQL database

// Load the JDBC Driver
Class.forName("com.mysql.jdbc.Driver");
// Establish the connection
Connection connection = DriverManager.getConnection("jdbc:mysql://[HOST]:[PORT]/[DB_NAME]", "[USERNAME]", "[PASSWORD]");

Where
[HOST] is your database IP address or host name.
[PORT] is your database port (the default one is 3306)
[DB_NAME] is your database name
[USERNAME] is your database username
[PASSWORD] is your database user password

Google the MySQL JDBC Driver download

Connect to the DB2 database

// Load the JDBC Driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
// Establish the connection
Connection connection = DriverManager.getConnection("jdbc:db2://[HOST]:[PORT]/[DB_NAME]", "[USERNAME]", "[PASSWORD]");

Where
[HOST] is your database IP address or host name.
[PORT] is your database port (the default one is 5021)
[DB_NAME] is your database name
[USERNAME] is your database username
[PASSWORD] is your database user password

Google the DB2 JDBC Driver download

Connect to the MS SQL database

// Load the JDBC Driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Establish the connection
Connection connection = DriverManager.getConnection("jdbc:sqlserver://[HOST]:[PORT];databaseName=[DB_NAME];selectMethod=cursor", "[USERNAME]", "[PASSWORD]");

Where
[HOST] is your database IP address or host name.
[PORT] is your database port (the default one is 1433)
[DB_NAME] is your database name
[USERNAME] is your database username
[PASSWORD] is your database user password

Google the MSSQL JDBC Driver download

Connect to the Derby database

// Load the JDBC Driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
// Establish the connection
Connection connection = DriverManager.getConnection("jdbc:derby:[PATH-TO-THE-DATABASE]");

Where
[PATH-TO-THE-DATABASE] is a database path for example: c:\\databases\\pets

Google the Derby JDBC Driver download (look for the derby.jar, derbytools.jar)

Connect to the HSQLDB database

// Load the JDBC Driver
Class.forName("org.hsqldb.jdbcDriver");
// Establish the connection
Connection connection = DriverManager.getConnection("jdbc:hsqldb:file:[PATH-TO-THE-DATABASE", "[USERNAME]", "[PASSWORD]");

Where
[PATH-TO-THE-DATABASE] is a database path for example: c:\\databases\\pets
[USERNAME] is your database username
[PASSWORD] is your database user password

Google the HSQLDB JDBC Driver download

Connect to the SQLite database

// Load the JDBC Driver
Class.forName("org.sqlite.JDBC");
// Establish the connection
Connection connection = DriverManager.getConnection("jdbc:sqlite:[PATH-TO-THE-DATABASE]");

Where
[PATH-TO-THE-DATABASE] is a database path for example: c:\\databases\\pets

Google the SQLite JDBC Driver download

Connect to the Firebird database

// Load the JDBC Driver
Class.forName("org.firebirdsql.jdbc.FBDriver");
// Establish the connection
Connection connection = DriverManager.getConnection("jdbc:firebirdsql://[HOST]:[PORT]/[PATH-TO-THE-DATABASE]", "[USERNAME]", "[PASSWORD]");

Where
[HOST] is your database IP address or host name.
[PORT] is your database port (the default one is 3050)
[PATH-TO-THE-DATABASE] is a database path for example: c:\\databases\\pets
[USERNAME] is your database username
[PASSWORD] is your database user password

Google the Firebird JDBC Driver download

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Java by Example App is available at Google Play Store NOW