19.11.14

Connect to database via JDBC

Though we have numerous methods [and sophisticated too] to connect to a database with java, if you are looking for a simple snippet to just connect, run a query, and get the result, this below code will suffice.
Also, if your purpose is just to query and get results, just having the right jar would be enough, no need for the database client/driver installation.

--------------------------------------
//function to connect to database using JDBC/JConnect
public Connection getJDBCConnection(){

System.out.println("Getting DB Connection properties");
Properties properties = getPropertyValues("test1.properties");

String dbDriver = properties.getProperty("database.driver");
String dbServer = properties.getProperty("database.server");
String dbServerPort = properties.getProperty("database.port");
String dbName = properties.getProperty("database.name");
String dbUsername = properties.getProperty("database.username");
String dbPassword = properties.getProperty("database.password");

String dbURL = "jdbc:sybase:Tds:" + dbServer + ":" + dbServerPort + "/" + dbName;

//Initializing the connection object
Connection connection= null;

try{
//Register the database driver
Class.forName(dbDriver);

//Open a connection
System.out.println("Connecting to database - " + dbURL);
connection = DriverManager.getConnection(dbURL, dbUsername, dbPassword);
System.out.println("DB connection successful");

}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}

return connection;
}
--------------------------------------

//function to run query and get the resultset back
public ResultSet runDBQuery(Connection connection, String sqlToRun){

Statement statement = null;
ResultSet resultSet = null;

try{

statement = connection.createStatement();

System.out.println("Executing SQL Query - " + sqlToRun);
resultSet = statement.executeQuery(sqlToRun);

}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(connection.isClosed() == false)
connection.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try

return resultSet;
}
--------------------------------------

Now to use all of these...
--------------------------------------
String sqlToRun = "SELECT EmployeeID FROM Employee Where EmployeeID = 245145";
try{
ResultSet rs = commonLib.runDBQuery(commonLib.getJDBCConnection(),sqlToRun);

while(rs.next()){
//Retrieve by column name
int id = rs.getInt("EmployeeID");
//String last = rs.getString("last");

System.out.println("EmployeeID: " + id);
}
rs.close();
}
catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
--------------------------------------
Output will be...
EmployeeID: 245145


The jdbc properties are as follows:

#This is the standard driver class for connecting to Sybase database using JConnect jar
database.driver=com.sybase.jdbc4.jdbc.SybDriver
database.server=abc.xyz.net
database.port=5500
database.name=emp_comp
database.username=autobot
database.password=autobot1

No comments:

Post a Comment