使用executeUpdate()而不是executeQuery()进行SELECT查询

编程入门 行业动态 更新时间:2024-10-28 06:25:03
本文介绍了使用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/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. download.oracle/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查询

本文发布于:2023-10-21 19:53:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1515252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   executeUpdate   SELECT   executeQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!