I've a simple JSP which does a select on a table of my simple database and shows the results:
<%-- 
<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"
        import="model.User"%>
<%!  public void deleteUser(int id) {
        Connection c1 = null;
        PreparedStatement ps = null;
        try {
            Class.forName("org.postgresql.Driver");
            c1 = DriverManager
                    .getConnection("jdbc:postgresql://localhost:5432/postgres",
                            "postgres", "admin");
            c1.setAutoCommit(false);
            System.out.println("Opened database successfully" + "\n");
            System.out.println("PROVA PROVA PROVA" + "\n");
            ps = c1.prepareStatement("DELETE FROM users WHERE user_id = ? ");
            ps.setInt(1, id);
            ResultSet rs1 = ps.executeQuery();
            rs1.close();
            ps.close();
            c1.close();
            JOptionPane.showInputDialog("Player Deleted");
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }
    }
%>
<!DOCTYPE html>
<head>
    <link rel="stylesheet" type="text/css" href="layout.css">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Registered users</title>
<input Type="BUTTON" Value="Home Page" onclick="location.href = 'index.html'"> 
</head>
<body>
    <h1>Registered users</h1>
    <p>
        <%
            Connection c = null;
            Statement stmt = null;
            try {
                Class.forName("org.postgresql.Driver");
                c = DriverManager
                        .getConnection("jdbc:postgresql://localhost:5432/firstapp",
                                "postgres", "admin");
                c.setAutoCommit(false);
                System.out.println("Opened database successfully" + "\n");
                stmt = c.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM users;");
                while (rs.next()) {
                    int id = rs.getInt("user_id");
                    String firstname = rs.getString("firstname");
                    String lastname = rs.getString("lastname");
                    String country = rs.getString("country");
                    User u = (User) request.getAttribute("user"); %>
        First Name: <% out.print(firstname); %> <br/>
        Last Name: <% out.print(lastname); %> <br/>
        Country: <% out.print(country); %> <br/>
        <input Type="BUTTON" Value="Delete" onclick=<%deleteUser(id);%>>
    <p/>
    <%
            }
            rs.close();
            stmt.close();
            c.close();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }%>
</p>
</body>
Each record has a "delete" button underneath which should allow to delete the corresponding record.
However, when the method deleteUser is called, the prepared statement fails and I get the following message in the console: table "users" does not ecists.
Why is that? It's because I call the deleteUser method and open another connection to the DB while another connection to the same DB is already opened (hence it fails to find the table)?
How can I fix this?
Also I'd like to return to my simple web application main menu (index.html) after the delete is done. Can you help me also with this?
Many thanks!
 
     
     
     
    