problem on delete statement in JSP
org.apache.jasper.JasperException: An exception occurred processing JSP page /proj/AdminLog.jsp at line 130
127: Driver Driverrs3 = (Driver)Class.forName(MM_Conn_DRIVER).newInstance();
128: Connection Connrs3 = DriverManager.getConnection(MM_Conn_STRING,MM_Conn_USERNAME,MM_Conn_PASSWORD);
129: PreparedStatement Statementrs3 = Connrs3.prepareStatement("DELETE * FROM database.credentials WHERE UserId = 16");
130: ResultSet rs3 = Statementrs3.executeQuery();
root cause
javax.servlet.ServletException: java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
i also tried executeUpdate(); but it is still not working.
[723 byte] By [
jayson17a] at [2007-11-27 11:19:45]

# 3
> Eh, a delete query doesn't return a resultset.
i get it now, thanks .. but is it possible to reference a javascript variable
inside a JSP block? what i am trying to do is to delete all rows that are checked from the list on button click
<%
Statement stmnt;
//registering the driver
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//defining URL of database server
String url = "jdbc:mysql://localhost:3306/database";
String uname ="user";
String pswd = "pass";
//getting the connection
Connection connect = DriverManager.getConnection(url, uname, pswd);
%>
<script type="text/JavaScript">
function jsDelete() {
for (var i=0; i < document.form1.checkbox.length; i++)
{
if (document.form1.checkbox[i].checked)
{
<%
stmnt = connect.createStatement();
// i am getting the error here
String sq = "DELETE FROM credentials where UserId = " + document.form1.checkbox[i];
stmnt.executeUpdate(sq);
%>
}
}
}
</script>
# 5
>is it possible to reference a javascript variable
inside a JSP block?
>what i am trying to do is to delete all rows that are checked from the list on button click
No it is not possible.
The button has to submit the form, and then you use request.getParameter() to find out which checkboxes were checked.
The best approach is to give all your checkboxes the same name, and the value being the id you want to delete.
eg
<input type="checkbox" name="deluser" value="42">
<input type="checkbox" name="deluser" value="69">
<input type="checkbox" name="deluser" value="666">
then in your java/servlet code
// using a prepared statement in this case is more efficient.
// Can reuse the same statement multiple times.
PreparedStatement stmt = connect.prepareStatement("delete from credentials where UserId = ?";
// retrieve the selected checkboxes.
String[] selectedIds = request.getParameterValues("delUser");
// check to see if any were checked
if (selectedIds != null){
// iterate over all userids marked for deletion
for (int i=0; i<selectedIds.length; i++){
int idToDelete = Integer.parseInt(selectedIds[i]);
stmt.setInt(1, idToDelete);
stmt.execute();
}
}
Cheers,
evnafets>