Monday, October 25, 2010

Database Connectivity Using JAVA

1..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);
      }
    }
}

2....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 ();
}
}
}

3...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 ();
        }
    }
}

4...import java.sql.*;
import java.io.*;
class Select1
{
public static void main( String argv[] )
{
try
{
Class.forName ("jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:msaccessdb";
Connection dbcon = DriverManager.getConnection (url, "",
"");
String queries = "select * from loadtest";
Statement smnt = dbcon.createStatement( );
ResultSet res = smnt.executeQuery( queries );
boolean more = res.next();
while ( more )
{
System.out.println( "Col1: " +
res.getInt( "col1" ));
more = res.next();
}
}
catch (java.lang.Exception ex)
{
// Print description of the exception.
System.out.println( "** Error on data select.** " );
ex.printStackTrace ();
}
}
}

5...import java.sql.*;
import java.io.*;
class SelectGen1 {
public static void main( String argv[] ) {
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:db1";
Connection dbcon = DriverManager.getConnection (
url, "", "");
String tableName = "emp1";
if ( argv.length > 0 )
tableName = argv[0];
String queries = "select * from " + tableName;
Statement smnt = dbcon.createStatement();
ResultSet res = smnt.executeQuery( queries );
ResultSetMetaData resmetadata = res.getMetaData();
int n = 0;
boolean more = res.next();
while ( more ) {
for ( n = 1; n <= resmetadata.getColumnCount(); n++ ) {
System.out.println( "Col " + n +" Name: " +resmetadata.getColumnName( n ) +" value: " +res.getString( n ));
}
}
}
catch (java.lang.Exception ex) {
// Print description of the exception.
System.out.println( "** Error on data select. ** " );
ex.printStackTrace ();
}
}
}

No comments:

Post a Comment