Java SQLException:“无法使用 executeQuery() 发出数据操作语句”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20153725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:32:33  来源:igfitidea点击:

SQLException: "Can not issue data manipulation statements with executeQuery()"

javamysqlsqljdbc

提问by jkl

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import ="java.sql.*" %>
<%@ page import = "com.mysql.jdbc.Driver" %>
<jsp:useBean id="vo" class="my.member.MemberVo"/>
 <jsp:setProperty property="*" name="vo"/>
<%

  Connection conn = null;
  Statement  stmt = null;
  PreparedStatement ps = null;

  String queryPerson = new String(); //Query statement
  String queryUserInfo = new String();
  String queryAccount = new String();

  try {
    Class.forName("org.gjt.mm.mysql.Driver");
    } catch (ClassNotFoundException e ) {
        System.out.println("JDBC error");
    }
  try{
   conn = DriverManager.getConnection("jdbc:mysql://mysql2.cs.stonybrook.edu/jaehyeolee", "root", "");
  } catch(SQLException e) {
      System.out.println("Database Error");
  }
   System.out.println("Database connected");


   try{
      stmt = conn.createStatement(); 


      queryAccount = "insert into member values('testID','password','name');";
      System.out.println(queryAccount);
      stmt.executeQuery(queryAccount);
  }catch(SQLException e){
      System.out.println("not inserted!");
  }finally{
        conn.close();
    }

 %>

This is my really simple mysql query in jsp file. I want to insert values into table member(id, password, name) but it keeps throwing an exception and print "not inserted!".

这是我在jsp文件中非常简单的mysql查询。我想将值插入表成员(id,密码,名称),但它不断抛出异常并打印“未插入!”。

Does anyone know what cause an exception? I tried to figure it out from yesterday noon but still I don't know the reason.

有谁知道是什么导致异常?我试图从昨天中午开始弄清楚,但我仍然不知道原因。

Please help!

请帮忙!

Here is my stack trace!

这是我的堆栈跟踪!

Database connected
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
insert into member('userid','password','name') values('jhlee127','108512012','JayZ');
not inserted!
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
    at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:499)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1518)
    at org.apache.jsp.member.regFormProc_jsp._jspService(regFormProc_jsp.java:109)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

采纳答案by Donato Szilagyi

Instead of:

代替:

stmt.executeQuery(queryAccount);

use:

用:

stmt.executeUpdate(queryAccount);

回答by Andrei Nicusan

You need to call statement.executeUpdateinstead of statement.executeQuery

你需要打电话statement.executeUpdate而不是statement.executeQuery

回答by Denis de Bernardy

My Java is very rusty, but isn't there some kind of executeUpdate() method that ought to be used here, instead of executeQuery()?

我的 Java 非常生疏,但是这里不是应该使用某种 executeUpdate() 方法而不是 executeQuery() 吗?

回答by S Melikian

PreparedStatement ps = conn.prepareStatement("insert into member (testID, password, name) values (?, ?, ?)");
ps.setInt(1, idValue);
ps.setString(2, passwordValue);
ps.setString(3, nameValue);
try {
    ps.executeUpdate();
    ps.close();
}

Try this and let me know if it's fixed.

试试这个,让我知道它是否已修复。