
/* This is a sample program demonstrates the use of Prepared statement.
 * Replace mylogin & mypassword by your login & password respectively.
 */ 

import java.sql.*;

public class preparedStatement {
	public static void main( String[] args ) throws SQLException {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ResultSetMetaData rsmd = null;

		try {
			DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
			/* Please substitute mylogin, mypassword by your login 
			 * and password.
			 */
			con = DriverManager.getConnection( "jdbc:oracle:thin:@everest:1521:GEN", "mylogin","mypassword");

			pstmt = con.prepareStatement( "select sno, sname from dbis.supplier where sno = ?" );
			pstmt.setInt( 1, 1 );
			rs = pstmt.executeQuery();

			rsmd = rs.getMetaData();

			for( int i = 1; i <= rsmd.getColumnCount(); i++ ) {
				System.out.print( rsmd.getColumnName(i)  + "\t" );
			}

			System.out.print( "\n" );

			while (rs.next()) {
				int no = rs.getInt("SNO");
				String name = rs.getString("SNAME");
				System.out.println( no + "\t" + name);
			}
		} catch(SQLException sqle){
			System.out.println("Exception " + sqle);
		}
	}
}
