Home Answers Viewqa JDBC SELECT query using executeUpdate() instread of executeQuery()

 
 


Kiran Kumar Reddy
SELECT query using executeUpdate() instread of executeQuery()
1 Answer(s)      2 years and 4 months ago
Posted in : JDBC

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 Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.