Home Tutorial Java Jdbc JDBC DataSource Interface Example

 
 

Share on Google+Share on Google+
JDBC DataSource Interface Example
Posted on: October 20, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn about javax.sql.DataSource interface and its implementations

Using javax.sql.DataSource Interface

DataSource interface is an alternative to the DriverManager class. The DataSource object has some properties that can be modified when required. For instance if a database moved on different server the properties of connection changed, the benefit of this, you only need to change in property not in whole program.

An Example of javax.sql.DataSource interface is given below

CREATE TABLE student (
RollNo int(9)  PRIMARY KEY NOT NULL,
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text
 );

Insert Value Into student table

INSERT INTO student VALUES(1, 'Ram', 'B.Tech', 'Delhi') ;

JdbcDataSourceExample.java

package roseindia.net;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.sql.DataSource;

public class JdbcDataSourceExample implements DataSource {
	Connection connection = null;

	@Override
	public Connection getConnection() throws SQLException {
		if (connection != null) {
			System.out.println("Cant craete a Connection");
		} else {
			connection = DriverManager.getConnection(
					"jdbc:mysql://192.168.10.13:3306/student", "root", "root");
		}
		return connection;
	}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		// TODO Auto-generated method stub
		if (connection != null) {
			System.out.println("Cant craete a Connection");
		} else {
			connection = DriverManager.getConnection(
					"jdbc:mysql://192.168.10.13:3306/student", username,
					password);
		}
		return connection;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		PrintWriter logWriter = null;
		try {
			logWriter = new PrintWriter("C:/work/vinay/Logfile.log");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return logWriter;
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub
		try {
			DriverManager.setLogWriter(new PrintWriter(
					"C:/work/vinay/Logfile.log"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean isWrapperFor(Class <> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public  T unwrap(Class iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public static void main(String[] args) throws SQLException {
		JdbcDataSourceExample ds = new JdbcDataSourceExample();
		Connection connection = ds.getConnection();
		PrintWriter out = DriverManager.getLogWriter();
		ds.setLogWriter(out);
		PrintWriter res = ds.getLogWriter();
		System.out.println(res.toString());
		Statement stmt = connection.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT * FROM student");
		while (rs.next()) {
			System.out.println("Roll No-" + rs.getInt(1) + ", Name- "
					+ rs.getString(2));
		}
		ds.setLoginTimeout(5);
	}
}
When you run this application it will display message as shown below:

java.io.PrintWriter@9971ad

Roll No-1, Name- Ram

Download this example code

Advertisement

Related Tags for JDBC DataSource Interface Example:


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