PREPARED SELECT STATEMENTS

1 package jdbcapplication;
 2 import java.sql.*;
 3 public class Selectinfo 
 4 {
 5     Connection cnt;
 6     PreparedStatement pstm;
 7     String driver,url;
 8     ResultSet rs;
 9     String qry="select * from emp";
10     Selectinfo() 
11     {
12     driver="com.mysql.jdbc.Driver";
13     url="jdbc:mysql://localhost/bhupesh";
14     try
15     {
16     cnt=DriverManager.getConnection
        (url,"root","ashok");
17     pstm=cnt.prepareStatement(qry);
18     rs=pstm.executeQuery();
19         while(rs.next())
20         {
21             int id=rs.getInt(1);
22             String name=rs.getString(2);
23             int sal=rs.getInt(3);
24             String job=rs.getString(4); 
25             
26             System.out.println(" | "+id+" | "+name+
" | "+sal+" | "+job);
27         }
28     System.out.println("Sucessfull Records Done");
29     rs.close();
30     cnt.close();
31     
32     }
33         catch (SQLException ex) {
34             System.out.println("Error : "+ex.getMessage());
35         }
36     }
37 public static void main(String args[])
38 { 
39     Selectinfo demo=new Selectinfo(); 
40 }
41 }

No comments:

Post a Comment