Establishes a connection from the given database URL in the form of: jdbc:subprotocol:subname
Establishes a connection from the given database URL and a Properties object which includes additional information such as user and password.
Establishes a connection from the given database URL, user and password.
As you notice, all methods require a database URL which is a string in a special format that contains information about the database to connect to, such as server name, database name, user, password… Database URL is specific to a particular database system. Following is an example of a database URL for MySQL:jdbc:mysql://localhost:3306/test
where localhost is server name, 3306 is port number, and test is database name.All the methods return a Connection object which is used for making SQL queries to the connected database.The following code examples illustrate establishing connection to a MySQL database. Follow this tutorial to download JDBC driver for MySQL.import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectJDBC3 {
public static void main(String[] args) {
String databaseURL = "jdbc:mysql://localhost:3306/test";
String user = "user";
String password = "password";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(databaseURL, user, password);
if (conn != null) {
System.out.println("Connected to the database");
}
} catch (ClassNotFoundException ex) {
System.out.println("Could not find database driver class");
ex.printStackTrace();
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
} import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnect1 {
public static void main(String[] args) {
String databaseURL = "jdbc:mysql://localhost:3306/test?user=root&password=root123";
Connection conn = null;
try {
conn = DriverManager.getConnection(databaseURL);
if (conn != null) {
System.out.println("Connected to the database");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
} Example 2: getConnection(String url, Properties info) :In this way, we put the user and password in a Properties object:import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBConnect2 {
public static void main(String[] args) {
String databaseURL = "jdbc:mysql://localhost:3306/test";
Connection conn = null;
try {
Properties props = new Properties();
props.put("user", "root");
props.put("password", "root123");
conn = DriverManager.getConnection(databaseURL, props);
if (conn != null) {
System.out.println("Connected to the database");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
} Example 3: getConnection(String url, String user, String password)In this way, we supply the user and password directly into the method’s arguments:import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnect3 {
public static void main(String[] args) {
String databaseURL = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root123";
Connection conn = null;
try {
conn = DriverManager.getConnection(databaseURL, user, password);
if (conn != null) {
System.out.println("Connected to the database");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
} NOTE: Since Java 1.7, you can use the try-with-resources statement to make connection to database without explicitly closing the connection, for example:public void connectDatabase(String url, String user, String password) {
try (Connection conn = DriverManager.getConnection(url, user, password)) {
if (conn != null) {
System.out.println("Connected to the database");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
}
}The try-with-resources statement automatically close the resource (database connection) for you.That's some Java code examples for connecting to a database with JDBC.
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.