Sponsered Links
Categories
Sponsered Links

JDBC Prepared Statement Example

 

This is a simple JDBC Prepared Statement Example.

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

class PreparedStatementExample {
    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 );
            seconds = (end_second - seconds)/1000;
            System.out.println( "Elapsed time: " + seconds + " secondsfor " + n + " records." );
        } catch (java.lang.Exception ex) {
            System.out.println( "** Error on data select. ** " );
            ex.printStackTrace ();
        }
    }
}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved