Monday, January 13, 2014

Java JDBC programming Example 7

//Note: create  a DSN in the name of sql that is pointing the table name limbs in the database with three String column  names things,legs,arms//Avoid Hacking through PreparedStatement 



//PreparedStatement 
import java.sql.*;
class Jdbc7{
public static void main(String...arg) throws Exception{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection c =  DriverManager.getConnection("jdbc:odbc:sql");
         PreparedStatement pstmt = c.prepareStatement("select * from limbs where arms >?");
         pstmt.setInt(1,0); 
         ResultSet r = pstmt.executeQuery();
         try{  
 
while(r.next()){
       
              System.out.println(r.getString("things"));
              System.out.println(r.getInt("legs"));
              System.out.println(r.getInt("arms"));
   
        }
        
         }catch(Exception e){System.out.println("Error :");e.printStackTrace();}
      r.close();
      pstmt.close();
      c.close();
}
}

No comments: