Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL error

 
Donna Harrington
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everybody.
hope somebody can help me. I have this error that says ' javax.servlet.ServletException: Can not issue data manipulation statements with executeQuery() '.
Its something to do with my rs = p.executeQuery();
Thanx for ur time!



<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import = "java.sql.*"%>
<%@page import = "java.io.*"%>
<%@page import = "java.util.Properties"%>
<%@page import = "java.io.InputStream"%>
<%@page session = "true"%>


<html>
<head>
<title> Checkout</title>
</head>
<body bgcolor="#228B22">
<font face="Times New Roman,Times" size="+3"> Checkout</font>
<hr>

<% String notEntered = "";
String connectionURL ="jdbc:mysql://localhost:3306/petshopwebsite";
String driver= "com.mysql.jdbc.Driver";
Connection c = null;
ResultSet rs = null;
Statement st = null;
String password = "";
String username ="root";


try{
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection(connectionURL,username, password);
st = c.createStatement();



String productId = request.getParameter("productId");
//String PName = request.getParameter("PName");
//String price = request.getParameter("Price");
String Quantity = request.getParameter("Quantity");
//Properties product = new Properties();

if(rs == null)
{
PreparedStatement p = c.prepareStatement("INSERT INTO order (username,productId,Quantity)"+" VALUES('"+username+"','"+productId+"','"+Quantity+"')");
rs = p.executeQuery();
%>
<jsp:include page = "signinsuccessful.jsp"/> <%--display this page on the current page--%>
<%
}
else{
%>
<jsp:include page = "Err.jsp"/> <%--display this page on the current page--%>
<%}



%>




<a href="http://localhost:3306/categoryList.jsp">Shop some more!</a>
<%

}catch (Exception e) {
throw (new ServletException(e));
}
finally
{
try { if( rs != null ) rs.close() ; } catch( SQLException ex ) { }
try { if( st != null ) st.close() ; } catch( SQLException ex ) { }
try { if( c != null ) c.close() ; } catch( SQLException ex ) { }
}
%>
</center>
</body>
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are having a problem with JDBC, it's best to ask in the JDBC forum, don't you think? Just because you are doing this within a JSP doesn't make it a JSP question.

Moving to the JDBC forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can not issue data manipulation statements with executeQuery() '.


This is a pretty straight-forward message; you are trying to execute a non-query with executeQuery. Look at the javadoc for the PreparedStatement class to find the appropriate method to use when executing non-query statements.
 
Donna Harrington
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanx very much
 
raj edla
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

My answer is use executeUpdate() instead of executeQuery() as Sql statement is Insering data into the table.

Raj
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic