Hi,

I am writing a code to dynamically create a table and then add delete and modify records. The table is created from an existing database table. How ever the add and the deleting of records is not wrking. I think there is some error in the query statements. Please help. Here is my code:

#
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>BP Admin</title>
</head>
<script type="text/javascript" language="javascript">
function slideit(tid)
{
if (document.getElementById(tid).style.display=="none")
{
document.getElementById(tid).style.display="";
}
else
{
document.getElementById(tid).style.display="none";
}
}
</script>


<body>


<H3>Business Processes</H3>
<table width="100%" height="100%" >
<tr>
<td height="33%"><%
Connection conn = null;
ResultSet rs1 = null;
Statement stmt = null;
ResultSetMetaData rsmd = null;
int columns=0;
String query="select bpname from businessprocessinfo;";



Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.128.150:5432/thirdeye", "postgres", "postgres");
stmt = conn.createStatement();
rs1 = stmt.executeQuery(query);


while (rs1.next())
{%>


<table  width="100%">
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<td width="33%"><a href="#" id="bpname"><%= rs1.getString(1) %></a> </td>
<td width="33%"><form name="form1" >
<input type="button" name="button1" value="Modify">
</form>             </td>
<td width="33%"><form name="form2" method="post" action="../src/com/wss/DeleteBP.java">
<label>
<input type="button" name="button2" value="Delete">
</label>
</form>               </td>
</tr>
</table>
<BR>
<%
}
%></td>
</tr>
<tr>
<td height="23"><div align="center">
<form id="form3" name="form3" >
<label>
<input type="button" name="button3" value="Add New  Business Process" onclick="javascript:slideit('collapsiblediv')"/>
</label>
</form>
</div></td>
</tr>
<tr>
<td height="33%"><div id="collapsiblediv" style="display:none"><table width="100%" border="1">
<tr><th width="33%">Name</th>
<td width="33%"><form id="form4" name="form4" method="post" action="">
<label>
<input type="text" name="textfield" />
</label>
</form>
</td>
<td width="33%"><form name="form10"><input type="button" name="button10" value="Check" onclick="" /></form>
</tr>
<tr>
<th>Description</th>
<td><form id="form5" name="form5" method="post" action="">
<label>
<input type="text" name="textfield2" />
</label>
</form>
</td>
</tr>
<tr>
<th>Add Jobs</th>
<td colspan="2"><form id="form7" name="form7" >
<input type="button" name="button12" value="Add a new Job" onclick="javascript:slideit('collapsiblediv1')" />
</form>
</td>
</tr>
<tr>
<td colspan="3"></td>
</tr>


<tr><td>
<div align="center">
<form id="form6" name="form6" method="post" action="addbp.jsp">
<label>
<div align="center">
<input type="submit" name="Submit" value="Add the new BP" onclick="alert('Do you want to save changes?')"/>
</div>
</label>
</form>
</div></td></tr>
</table></div>
</tr>
<hr />
<tr>
<td height="33%">
<div id="collapsiblediv1" style="display:none">
<table width="100%" border="1">
<tr><th width="33%">Name</th>
<td width="33%"><form id="form14" name="form14" method="post" action="">
<label>
<input name="textfield1" type="text" size="100%" />
</label>
</form>
</td>
<td width="33%"><form name="form100"><input type="button" name="button100" value="Check" onclick="" /></form>
</tr>
<tr>
<th>Description</th>
<td><form id="form15" name="form15" method="post" action="">
<label>
<input name="textfield12" type="text" size="100%" />
</label>
</form>
</td>
</tr>
<tr>
<th>Add Steps</th>
<td colspan="2"><form id="form17" name="form17" >
<div align="center">
<input type="button" name="button112" value="Add a new Step" onclick="window.open('AddnewStep.jsp','window1','width=800, height=250', resizable=1)"/>
</div>
</form>
</td>
</tr>
<tr>
<td colspan="3"><form name="form50">
<div align="center">
<input type="submit" name="button24" value="Add step to job" />
</div>
</form></td>
</tr>
</table>
</div>
</td>
</tr>
</table>


</body>
</html>

The code above creates dynamic table.

#<%@ page import="javax.servlet.http.*" %>
<%@ page import= "java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>addbptodatabase</title>
</head>
<body>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String query="insert into businessprocessinfo values(/"" <br>
+ "request.getParameter("textfield")");";
try
{
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql://192.168.128.150:5432/thirdeye","postgres","postgres");
stmt=con.createStatement();
rs=stmt.executeQuery(query);
}catch (Exception e){ }
%>
</body>
</html>

This code is to add a row

#package com.wss;
import java.sql.*;
public class DeleteBP {
public static void main(String[] args) {
System.out.println("Deleting a Row from the Database!");
Connection con = null;
Statement stmt=null;

try

{
Class.forName("org.postgresql.Driver");
con=DriverManager.getConnection("jdbc:postgresql://192.168.128.150:5432/thirdeye","postgres","postgres");
stmt=con.createStatement();
String query="Delete from businessprocessinfo where bpname=("request.getParameter('bpname')";)";
int delete=stmt.executeUpdate(query);



}catch(Exception e){ }
}
}

This code is to delete a row.

Please help.
Thanks a lot in advance.

Saswati

Dani AI

Generated

Quick diagnosis and an actionable fix summary (ties to earlier replies from and ):

Several problems prevent add/delete from working. HTML controls use type="button" so the form never posts; row forms do not include the bp name as a parameter; the delete class is a plain Java main() program (not a servlet) and is being referenced as a .java path; SQL strings are concatenated incorrectly; and INSERT/DELETE are being run with executeQuery() or with malformed text. As noted, the correct flow is JSP -> servlet -> DB -> redirect back to the JSP. As noted, string concatenation here is broken; the safe fix is a parameterized query.

Practical steps and a minimal delete servlet example (use POST and a hidden form field for bpname, and use PreparedStatement + executeUpdate):

import java.io.IOException;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/deleteBP")
public class DeleteBP extends HttpServlet {
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String bp = req.getParameter("bpname");
    if (bp == null || bp.trim().isEmpty()) { resp.sendRedirect("list.jsp"); return; }
    String url="jdbc:postgresql://HOST:5432/DB"; String user="USER"; String pass="PASS";
    String sql="DELETE FROM businessprocessinfo WHERE bpname = ?";
    try (Connection c = DriverManager.getConnection(url,user,pass);
         PreparedStatement ps = c.prepareStatement(sql)) {
      ps.setString(1, bp);
      ps.executeUpdate();
    } catch (SQLException ex) { throw new ServletException(ex); }
    resp.sendRedirect("list.jsp");
  }
}

Troubleshooting and best practices: use input type="submit" or links that include the bp name, include a hidden field with the exact bp value, always use PreparedStatement (prevents SQL injection), use executeUpdate() for INSERT/UPDATE/DELETE, do not swallow exceptions — log or rethrow, and redirect after POST to avoid duplicate submissions. request.getParameterValues() (as @Avinash_7 mentioned) is only for multi-value inputs (checkbox arrays), not needed for a single bpname.

Recommended Answers

All 4 Replies

1. Please use the hash sign "#" to insert any code in your post. The function is there for a reason.
2. Ofcourse the thing you try to do it doesn't work. You did not finishe it. From your JSP page you call servlet which is supposed to delete some data and then nothing...
What you want to do is from your JSP call servlet. Pass a request for processing, process the request, update your session bean or what ever you use store data from database. After these you call back your JSP page and show updated view.

Hi,

String query="Delete from businessprocessinfo where bpname=("request.getParameter('bpname')":P";

Saswati

Have you tried this in SQL, because you are trying to access a variable within a string without catenation (spelling?).

Member Avatar for Member #1131052

why dont you try using request.getparametervalues() instead of request.getparamete().

The get parametervalues gets the output of name in an array that is if the buttons have same name they form an array starting from index 0 .

@Avinash_7 - This thread is 8 years old and the OP hasn't been here since, so don't resurrect old threads from the death!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.