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

时间:2013-11-22 20:15:41

标签: java mysql sql jdbc

<%@ 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();
    }

 %>

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

有谁知道导致异常的原因是什么?我试图从昨天中午算出来 但我仍然不知道原因。

请帮忙!

这是我的堆栈跟踪!

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)

4 个答案:

答案 0 :(得分:22)

而不是:

stmt.executeQuery(queryAccount);

使用:

stmt.executeUpdate(queryAccount);

答案 1 :(得分:3)

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

答案 2 :(得分:3)

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();
}

试试这个并告诉我它是否已修复。

答案 3 :(得分:2)

您需要致电statement.executeUpdate而不是statement.executeQuery