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)
7
  • 1
    Do you have a stack trace of the exception? Commented Nov 22, 2013 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? Commented Nov 22, 2013 at 20:19
  • @Rogue i put a stack trace of the exception. Commented Nov 22, 2013 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. Commented Nov 22, 2013 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. Commented Nov 22, 2013 at 20:31

4 Answers 4

22

Instead of:

stmt.executeQuery(queryAccount);

use:

stmt.executeUpdate(queryAccount);
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

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.

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.