使用executeUpdate()而不是executeQuery()进行SELECT查询 [英] SELECT query using executeUpdate() instead of executeQuery()

查看:184
本文介绍了使用executeUpdate()而不是executeQuery()进行SELECT查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


推荐答案

正如其他人所指出的: executeUpdate()不能用于运行查询。

As others have pointed out: executeUpdate() can not be used to run queries.

如果您正在寻找一种执行语句而不知道它们是什么的方法,那么您应该看一下execute()方法。

http: //download.oracle.com/javase/6/docs/api/java/sql/Statement.html#execute%28java.lang.String%29

If you are looking for a way to execute statement without knowing what they do, you should have a look at the execute() method.
http://download.oracle.com/javase/6/docs/api/java/sql/Statement.html#execute%28java.lang.String%29

返回的布尔值将告诉您它是返回结果还是更新计数。然后,您可以使用getResultSet()来获取结果,或使用getUpdateCount()来获取受影响的行数。

The returned boolean will tell you if it returned a result or just update counts. You can then use getResultSet() to obtain the result or getUpdateCount() to get number of affected rows.

请注意,允许语句返回多个结果和/或udpate计数。请参阅getMoreResults()中的示例。

Note that a statement is allowed to return more than one result and/or udpate count. See the example in getMoreResults().

这篇关于使用executeUpdate()而不是executeQuery()进行SELECT查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆