A simple JDBC connection tester Cont’d

8 04 2007

In my earlier article I tested the JDBC using the DriverManager but in this article I’m going to test it using DataSource Interface, this interface uses the Java Naming and Directory Interface (JNDI) to store Logical Name for the Data Source instead of using the driver name to connect to Data Source.

A small requirement urged me to write the below code, I got to thank the requester.

For running the below code I used MySQL - Version 5.0.16 as the Database and Eclipse SDK - Version: 3.2.2.

Also do replace the following: DB_USER_NAME, DB_PASSWORD, DB_NAME, DB_TABLE_NAME, DB_TABLE_COLUMN_NAME

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

public class JdbcConTestUsinDataSrcOnly {

public static void main(String[] args) {

MysqlDataSource mySQLDataSrc = null;

if (mySQLDataSrc == null) {
mySQLDataSrc = new MysqlDataSource();
mySQLDataSrc.setServerName(”localhost”);
mySQLDataSrc.setPortNumber(3306);
mySQLDataSrc.setUser(”DB_USER_NAME”);
mySQLDataSrc.setPassword(”DB_PASSWORD”);
mySQLDataSrc.setDatabaseName(”DB_NAME”);
}

Connection con = null;

try {
con = mySQLDataSrc.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}

Statement stmt = null;
try {
stmt = con.createStatement();
stmt.executeQuery(”select * from DB_TABLE_NAME”);
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
System.out.println(rs.getString(”DB_TABLE_COLUMN_NAME”));
}
} catch (SQLException e) {
e.printStackTrace();
}

}
}


Actions

Information

One response to “A simple JDBC connection tester Cont’d”

11 04 2007
A simple JDBC connection tester Cont’d « ABr’s Blog (02:40:50) :

[...] simple JDBC connection tester Cont’d Posted April 11, 2007 In my earlier article I tested the JDBC using the DataSource Interface but in this article I’m going to test it using [...]

Leave a comment

You must be logged in to post a comment