Showing posts with label generic jdbc. Show all posts
Showing posts with label generic jdbc. Show all posts

Tuesday, July 23, 2013

Generic JDBC Program for Derby


This is a simple JDBC Program for Derby database. No standards followed, you can see that connection is not closed, statement is not cleared.

Can be used to check database connectivity.

SQL Queries
create table test (id integer, name varchar(10), age integer, address varchar(30));

insert into test values (1, 'test1', 20, '80 Absolute Avenue');
insert into test values (2, 'test2', 30, '90 Absolute Avenue');

select * from test



package com.hibernate.demo;

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

public class GenericJDBC {

public static void main(String[] args) {
// TODO Auto-generated method stub
String url ="jdbc:derby://localhost:1527/sample;create=true";

Connection conn;

try
{

Class.forName("org.apache.derby.jdbc.ClientDriver");

conn = DriverManager.getConnection(url, "user", "user");

Statement st = conn.createStatement();

ResultSet rs = st.executeQuery("select * from test");

while(rs.next())
{
System.out.println(rs.getInt(1)
+" " + rs.getString(2) +
" " + rs.getInt(3)+ " "+
rs.getString(4));
}

}catch(Exception e)
{
e.printStackTrace();
}
finally
{

}

}

}