samedi 30 avril 2016

Checkbox select and delete from database

I have multiple checkbox and button in my form. When I select one or more checkboxes and press button I saved all values in the database.

Now I need select that values from database and checkbox must be checked only for values that are in database, and on unchecked i must delete data from database and after page reload checkbox must be unchecked.. how can i simple write this code?

I have in java servlet class input in database and its works.

Thank you.

//checkbox from form 

     "<td><span>checkbox:</span><input class='chxActivate' type='checkbox' id='ID_"+p.getID()+"' name='ID_"+p.getID()+"' value='1'></td>"



//servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Connection c = null;
    Statement stmt = null;

    Set<String> parameterNames = request.getParameterMap().keySet();
    for (String string : parameterNames) {
        System.out.println(string);
    }

    Enumeration<String> en = request.getParameterNames(); 
    Integer tmpsnlid = null;
    String auxStr = "";

    while (en.hasMoreElements()) {
        auxStr = (String) en.nextElement();
        if (auxStr.startsWith("ID_")){
                tmpsnlid = Integer.parseInt(auxStr.substring(6));   
                System.out.println("V2) Checkbox postavljen: "+tmpsnlid);
                try {
                      Class.forName("org.postgresql.Driver");
                      c = DriverManager.getConnection("jdbc:postgresql://database", "user","pass");
                      c.setAutoCommit(false);
                      System.out.println("Opened database successfully");
                      stmt = c.createStatement();
                      String sql = "INSERT INTO database(id, active, username)" +
                                   "VALUES ('"+tmpsnlid+"', 't','kavusladnin');"; 
                      stmt.executeUpdate(sql);
                      stmt.close();
                      c.commit();
                      c.close();
                    } catch ( Exception e ) {
                      System.err.println( e.getClass().getName()+ ": " + e.getMessage() );
                      System.exit(0);
                    }
                    System.out.println("INSERT - Records created successfully");

        }
    }




Aucun commentaire:

Enregistrer un commentaire