8
<%@ 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!".

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)
sleske
  • 81,358
  • 34
  • 189
  • 227
jkl
  • 675
  • 2
  • 8
  • 23
  • 1
    Do you have a stack trace of the exception? – Rogue Nov 22 '13 at 20:17
  • you should print e rather than 'not inserted', might shed more light on the error. Show us the schema of member too, does the ID column accept a string? – OGHaza Nov 22 '13 at 20:19
  • @Rogue i put a stack trace of the exception. – jkl Nov 22 '13 at 20:21
  • 1
    Sometimes it helps to actually read error messages before posting them on StackExchange. I think _Can not issue data manipulation statements with executeQuery()_ sounds pretty self-explanatory. – mustaccio Nov 22 '13 at 20:26
  • Just as a note: Please never, *ever* handle exceptions like this. For fatal exceptions like this, just let them bubble up, and optionally catch them higher up to print a proper error message. *Never* just catch and continue like this. – sleske Nov 22 '13 at 20:31
  • @sleske Thanks for an advise! But what do you mean by "just let them bubble up, and optionally catch them higher up to print a proper error message." Thanks! – jkl Nov 23 '13 at 15:44
  • @prographer: I meant that you should not catch the exception; then it will bubble up automatically. You may or may not want to have a generic exception handler higher up, like in your `main()` function. – sleske Nov 23 '13 at 17:53

4 Answers4

22

Instead of:

stmt.executeQuery(queryAccount);

use:

stmt.executeUpdate(queryAccount);
Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
3

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

Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
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();
}

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

S M
  • 57
  • 6
2

You need to call statement.executeUpdate instead of statement.executeQuery

Andrei Nicusan
  • 4,555
  • 1
  • 23
  • 36