SELECT query using executeUpdate() instread of executeQuery()

SELECT query using executeUpdate() instread of executeQuery()

for Ex:  class sample{
   public static void main(String a[]){
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:orcl", "", "");
        String str="Slect * from EMP";
        Statement st=con.createStatement();
        try{
            st.executeUpdate("select * from EMP"); //gives us Exception
                }
        catch(SQLException ex)
        {
                    // I want actuval code here.
                   //CODE here

 }
 //catch
 }
 //try
 }
 //main
 }
 //class
View Answers

January 21, 2011 at 11:14 AM

Hi Friend,

No, it is not possible.If you use executeUpdate() method with select quesry then you can't be able to retrieve data from the database.

Try this:

import java.sql.*;
class sample{
    public static void main(String[] args){
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           Connection con = DriverManager.getConnection("jdbc:odbc:student");
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from data");
           while(rs.next()){
               System.out.println(rs.getString("name")+" "+rs.getString("address"));
           }
        }
        catch(Exception e){
        }
    }
}

Thanks









Related Tutorials/Questions & Answers:

Ads