Using Network Address To Connect to a Database

In this tutorial you will learn how to use a network address to connect a database.


Share on Google+Share on Google+

Using Network Address To Connect to a Database


In this tutorial you will learn how to use a network address to connect a database.

How To Use Network Address To Connect To The Database

If a database being connecte over a network then you need to get the network address of that database and also identify the network port no of that database. The default port no of MySql database is 3306 and default host name is A JDBC URL provides a way to identify a database host, so that appropriate database driver could be loaded for that database. After the port no of database you must provide a database name. Suppose you want to connect a MySql database with your application over a network then you must load the "com.mysql.jdbc.Driver". Then you specify the connection URL. The syntax for MySql conection Url is as


For Example-

Connection connection = DriverManager.getConnection("jdbc:mysql://","root","root");

Here Network address the database is, and database port no is 3306. The name of the database is student, the user name and password is "root"

An example of using network address to connect to database is given below


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCCOnnectionExample {
	Connection connection = null;

	public JDBCCOnnectionExample() {
		try {
		} catch (ClassNotFoundException e) {

	public Connection createConnection() {
		Connection con = null;
		if (connection != null) {
			System.out.println("Cant create a connection");
		} else {
			try {
				con = DriverManager.getConnection(
						"jdbc:mysql://", "root",
				System.out.println("Connection created Successfully");
				DatabaseMetaData dbMetaData = con.getMetaData();
				ResultSet dbInfo = dbMetaData.getCatalogs();
				System.out.println("Getting Concurrency of MetaData");
			} catch (SQLException e) {
		return con;

	public static void main(String[] args) throws SQLException {
		JDBCCOnnectionExample jdbccOnnectionExample = new JDBCCOnnectionExample();
		Connection conn = jdbccOnnectionExample.createConnection();


Posted on: October 20, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles