Sunday, November 2, 2008

Jdbc Sample codes

Example1:
------------------------------
------------------------------

import java.sql.*;

public class JDBCapp {

public static void main(java.lang.String[] args) {
try {
// This is where we load the driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException e) {
System.out.println("Unable to load Driver Class");
return;
}

try {
// All database access is within a try/catch block. Connect to database,
// specifying particular database, username, and password
Connection dbcon = DriverManager.getConnection("jdbc:odbc:db1","", "");

// Create and execute an SQL Statement
Statement smnt = dbcon.createStatement();
ResultSet res = smnt.executeQuery("SELECT ENAME FROM EMP");

// Display the SQL Results
while(res.next()) {
System.out.println(res.getString("ENAME"));
}

// Make sure our database resources are released
res.close();
smnt.close();
dbcon.close();

}
catch (SQLException se) {
// Inform user of any SQL errors
System.out.println("SQL Exception: " + se.getMessage());
se.printStackTrace(System.out);
}
}
}


Example for Preparestatement:
---------------------------------
---------------------------------

import java.sql.*;
import java.io.*;
import java.util.Date;
import java.text.DateFormat;

class Prep1 {
public static void main( String argv[] ) {
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:db1";
Connection dbcon = DriverManager.getConnection (
url, "", "");
Statement smnt = dbcon.createStatement();
Date dates = new Date();
long seconds = dates.getTime();
String start_time = DateFormat.getTimeInstance()
.format( dates );
System.out.println( "Start Time: " + start_time );
int n = 1;
String queries = "select * from emp1 where salary = ";
String queryString = queries + n;
ResultSet res = smnt.executeQuery( queryString );
boolean more = res.next();
for (; n < 2000 && more ; n++ ) {
queryString = queries + n;
res = smnt.executeQuery( queryString );
more = res.next();
}
Date end_date = new Date();
long end_second = end_date.getTime();
String end_time =
DateFormat.getTimeInstance().format( end_date );
System.out.println( "End Time:" + end_time );
// display elapsed time
seconds = (end_second - seconds)/1000;
System.out.println( "Elapsed time: " + seconds + " seconds for " + n + " records.");
}
catch (java.lang.Exception ex) {
// Print description of the exception.
System.out.println( "** Error on data insert. ** " );
ex.printStackTrace ();
}
}
}


Example for preparestatement2:
----------------------------------
----------------------------------

import java.sql.*;
import java.io.*;
import java.util.Date;
import java.text.DateFormat;
class Prep2
{
public static void main( String argv[] )
{
try
{
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:db1";
Connection dbcon = DriverManager.getConnection (url, "", "");
String queries = "select * from emp1 where salary = ? ";
PreparedStatement psmnt =
dbcon.prepareStatement( queries );
Date dates = new Date();
long seconds = dates.getTime();
String start_time = DateFormat.getTimeInstance().format(dates );
System.out.println( "Start Time: " + start_time );
int n = 3;
boolean result;
psmnt.setInt( 1, n );
ResultSet res = psmnt.executeQuery();
boolean more = res.next();
for (; n < 2000 && more ; n++ )
{
res.close();
psmnt.setInt( 1, n );
res = psmnt.executeQuery();
more = res.next();
}
Date end_date = new Date();
long end_second = end_date.getTime();
String end_time =
DateFormat.getTimeInstance().format( end_date );
System.out.println( "End Time:" + end_time );
// display elapsed time
seconds = (end_second - seconds)/1000;
System.out.println( "Elapsed time: " + seconds + " secondsfor " + n + " records." );
}
catch (java.lang.Exception ex)
{
// Print description of the exception.
System.out.println( "** Error on data select. ** " );
ex.printStackTrace ();
}
}
}

No comments: