Monday 15 April 2013

Java: Insert values into table using prepared statement Derby


Following method adds a record to the database table:
  
public void addPerson(Person p) {
        if (p == null || p.getName().equals("")) {
            return;
        }

        String name = p.getName();
        String address = p.getAddress();
        String phone = p.getPhone();

        Connection con = null;
        try {
            con = getConnection();
            if (con != null) {

                String sql = "INSERT INTO APP.ADDRESS_BOOK("
                        + "NAME,"
                        + "ADDRESS,"
                        + "PHONE) "
                        + "VALUES(?,?,?)";

                PreparedStatement pStmt = con.prepareStatement(sql);

                // Set the values
                pStmt.setString(1, name);
                pStmt.setString(2, address);
                pStmt.setString(3, phone);

                // Insert
                pStmt.executeUpdate();
            }
        } catch (SQLException ex) {
            Logger.getLogger(PersonDAO.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println(ex.getMessage());
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    Logger.getLogger(PersonDAO.class.getName()).log(Level.SEVERE, null, ex);
                    System.out.println(ex.getMessage());
                }
            }
        }
    }

Java: how to check in servlet which html button was pressed


We can define two buttons in the html page:

<input type="submit" value="Add" name="addbutton">
<input type="submit" value="Search" name="addbutton">



Then in the servlet in the get or post method get the values for the buttons. Button pressed will have a value and the one not pressed will be null:

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String add = request.getParameter("addbutton");
        String search = request.getParameter("searchbutton");

        if (add != null) {
            response.sendRedirect("addperson.html");
        } else if (search != null) {
            response.sendRedirect("searchperson.html");
        }
    }

Java: Connect to Database (Derby)


public class PersonDAO {

//AddressBookDB is the database name to which we want to connect
    private final String url = "jdbc:derby://localhost/C:/Users/Administrator/.netbeans-derby/AddressBookDB";

    PersonDAO() {
    }

    private Connection getConnection() {
        Connection con = null;
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            con = DriverManager.getConnection(url, "test", "test"); // test, test are the //user name and password for the database.
        } catch (Exception e) {
            System.out.println("Exception occured while connecting to the database: " + e.getMessage());
            e.printStackTrace();
        }
        return con;
    }
}