Home Tutorial Java Jdbc Using Network Address To Connect to a Database

 
 

Share on Google+Share on Google+
Using Network Address To Connect to a Database
Posted on: October 20, 2010 at 12:00 AM
Advertisement
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 127.0.0.1. 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

jdbc:mysql://[host][,failoverhost...][:port]/[database]
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...

For Example-

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

Here Network address the database is 192.168.18.45, 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

JDBCCOnnectionExample.java

package roseindia.net;

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 {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
	}

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

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

Related Tags for Using Network Address To Connect to a Database:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 20, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com