lucy nana 0 Newbie Poster

I facing a problem with checkbox value that already exists inside the database. so how can I retrieve those value into another database table? The table subject and service below is what I want. But based on the code below the SERVICE TABLE on SUBJECTID COLUMN producing null value. How to remove those null value with subjectid value? I don't understand that well because I am a newbie.

SUBJECT TABLE (Values inside subject table already exists)

SUBJECTID       SUBJECTNAME
-----------     ----------------
PT3_ENGLISH      ENGLISH
PT3_BM           BAHASA MELAYU
PT3_SC           SCIENCE
PT3_MATH         MATHEMATICS

CURRENT SERVICE TABLE IN DB

 SERVICE TABLE 
        SERVICEID       SERVICECATEGORY    SERVICETYPE     STUDENTID    SUBJECTID
        ---------       ---------------    -----------     ---------    ----------
        123             PT3                 ORDINARY       20345         null

TABLE THAT I WANT.

SERVICE TABLE 
    SERVICEID       SERVICECATEGORY    SERVICETYPE     STUDENTID    SUBJECTID
    ---------       ---------------    -----------     ---------    ----------
    123             PT3                 ORDINARY       20345         PT3_ENGLISH
                                                                     PT3_BM
                                                                     PT3_SC
                                                                     PT3_MATH

here my registration.jsp

<u><center><h2>Services Information </h2></center></u>
                                    <hr>
                                    </div>
                                    <br>

                                     <div class="one_third first">Service Type:
                                        <select name="servicetype" id="servicetype" required>
                                                <option value="">- Service Type -</option>
                                                <option value="ORDINARY">ORDINARY</option>
                                                <option value="PERMATA">PERMATA</option>
                                                <option value="PRIVATE">PRIVATE</option>
                                                <option value="PMC">PMC</option>
                                            </select>
                                        </div>

                                    <div class="one_half">Service Category:
                                        <select name="servicecategory" id="servicecategory" required>
                                                <option value="">-Service Category -</option>
                                                <option value="UPSR">UPSR</option>
                                                <option value="PT3">PT3</option>
                                                <option value="SPM">SPM</option>
                                            </select>
                                        </div>

                            <BR><BR><BR><h2>Subject</h2>
                             <div class="three_quarter first">  
                                        <h3>UPSR</h3>
                                         <input type="checkbox" class="dissable" onclick="check();" name="UPSR" value="UPSR_ENGLISH" id="subjectId">English </td>
                                </div>              
                            <br><br><hr><br><br>        
                             <div class="three_quarter first">  
                                  <h3>PT3</h3>
                                    <input type="checkbox" class="dissable" onclick="check();" name="PT3" value="PT3_ENGLISH" id="subjectId">English </td>
                                    <input type="checkbox" class="dissable" onclick="check();" name="PT3" value="PT3_BM" id="subjectId">Bahasa Melayu</td>
                                    <input type="checkbox" class="dissable" onclick="check();" name="PT3" value="PT3_SC" id="subjectId">Science</td>
                                    <input type="checkbox" class="dissable" onclick="check();" name="PT3" value="PT3_MATH" id="subjectId">Mathematics</td>
                            </div-->
                            <br><br><hr><br><br>
                             <div class="block clear">  
                                  <h3>SPM</h3>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_ENGLISH" id="subjectId">English </td> 
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_BM" id="subjectId">Bahasa Melayu</td>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_SC" id="subjectId">Science</td>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_MATH" id="subjectId">Mathematics</td>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_ADDMATH" id="subjectId">Additional Mathematics </td>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_HISTORY" id="subjectId">History</td>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_ECONOMY" id="subjectId">Economy</td>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_PHYSIC" id="subjectId">Physics</td><br>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_CHEMISTRY" id="subjectId">Chemistry</td>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_BIOLOGY" id="subjectId">Biology</td>
                                     <input type="checkbox" class="dissable" onclick="check();" name="SPM" value="SPM_ACCOUNT" id="subjectId">Account</td>
                                </div>

here is my Servlet registerServlet.java

                      String[] sv;
              String[] sv2;
              String[] sv3;

              sv = request.getParameterValues("UPSR");
              sv2 = request.getParameterValues("PT3");
              sv3 = request.getParameterValues("SPM");
              String subjectId= "";                 //first declare the variable to store the value to store loop value
              String service = request.getParameter("servicecategory"); // retrieve parameter value
                                                                        // int i=0; declare variable for loop
               if(service.equalsIgnoreCase("UPSR")){

                   for(int i = 0; i <sv.length; i++){       // for loop to read one by one all the checkbox checked status
                       if(i<sv.length-1){
                           subjectId = subjectId +sv[i]+ ","; //store value of checked checkbox
                       }
                       else{
                           subjectId = subjectId + sv[i];
                       }
                   }

               }
               else if(service.equalsIgnoreCase("PT3")){

                   for(int i = 0; i <sv2.length; i++){
                       if(i<sv2.length-1){
                           subjectId = subjectId +sv2[i]+ ",";
                       }
                       else{
                           subjectId = subjectId + sv2[i];
                       }
                   }

               }
               else if(service.equalsIgnoreCase("SPM")){

                   for(int i = 0; i <sv3.length; i++){
                       if(i<sv3.length-1){
                           subjectId = subjectId +sv3[i]+ ",";
                       }
                       else{
                           subjectId = subjectId + sv3[i];
                       }
                   }

               }

               svb.setServiceid(request.getParameter("serviceid"));
               svb.setStudentid(request.getParameter("studentid"));
               svb.setServicecategory(service);
               svb.setServicetype(request.getParameter("servicetype"));

               sub.setSubjectId(subjectId);
               sub.setSubjectName(request.getParameter("subjectName"));

               System.out.println(service);

               sb = RegisterDAO.registerStudent(sb);
               svb = RegisterDAO.registerService(svb);
               sub = RegisterDAO.registerSubject(sub);

last is my registerDAO.java containing a certain part of registration subject only

 //registersubject
                public static SubjectBean registerSubject(SubjectBean bean) { // preparing some objects for connection 
                    System.out.println("JIJIJI SUBJECT"); 
                    Statement stmt = null; 

                    String subjectId = bean.getSubjectId(); 
                    String searchQuery = "select * from subject where subjectId='" + subjectId +"'";

                    //String query = "select * from SUBJECT where ROWNUM <=1 ORDER BY SUBJECTID DESC";

                    // "System.out.println" prints in the console; Normally used to trace the process 
                    System.out.println("Your subject ID is " + subjectId); 
                    //System.out.println("Your subject name is " + subjectName); 
                    System.out.println("Query: " + searchQuery); 

                    try { // connect to DB 
                        currentCon = connectionManager.getConnection(); 
                        stmt = currentCon.createStatement(); 
                        rs = stmt.executeQuery(searchQuery);
                        boolean more = rs.next(); 

                        subjectId=rs2.getString("subjectId");

                        System.out.println("\n the subject are : "+subjectId);

                        bean.setValid(true);
                        bean.setSubjectId(subjectId);
                        bean.setSubjectName(rs2.getString("subjectName"));

                        // if subject does not exist set the isValid variable to false 
                        if (!more) { 
                            System.out.println("Sorry, no subject registered! Please ask the clerk first"); 
                            bean.setValid(false); 
                            } 
                        // if user exists set the isValid variable to true 
                        //else if (more) { 

                            //System.out.println("Added Subject!"); 

                            //System.out.print("3");
                            //stmt.executeUpdate(searchQuery);
                            //System.out.print("4");
                            //System.out.print("1 new row inserted in table subject");

                            //bean.setValid(true); 
                            //} 
                        } 
                    catch (Exception ex) { 
                        System.out.println("Log In failed: An Exception has occurred! " + ex); 
                        } 
                    // some exception handling 
                    finally { 
                        if (rs != null) { 
                            try { rs.close(); 
                            } 
                                catch (Exception e) {   
                                } 
                                rs = null; 
                            } 
                        if (stmt != null) { 
                            try { stmt.close(); 
                            } 
                                catch (Exception e) { 

                                } 
                                stmt = null; 
                                } 

                        if (currentCon != null) {
                            try { currentCon.close();
                            } 
                            catch (Exception e) { 

                            } 
                            currentCon = null; 
                            } 
                        } 
                    return bean;

Please help me.
thanks in advance.

Dani AI

Generated

Quick diagnosis (why SERVICE.SUBJECTID is ending up null)

  • The code builds a comma list of selected subject IDs but then treats that list as if it were a single subject row. A WHERE subjectId = 'PT3_ENGLISH,PT3_BM' will not match any single row, so lookups fail.
  • The DAO logic is broken: a wrong ResultSet variable is referenced and values are read before checking rs.next(), which can produce exceptions or no data.
  • It’s also common to insert the SERVICE row without ever passing the subject data into the service INSERT (or to call registerService before persisting the subject mapping). Any of those will leave the SUBJECTID column null.

Recommended fix — normalize and insert link rows (preferred)
Store subjects in a many‑to‑many relation (service_subject with columns serviceid, subjectid). Flow: insert SERVICE once, get its generated key, then insert one service_subject row per selected checkbox inside a transaction. Example JDBC pattern:

String[] chosen = request.getParameterValues("subject"); // checkboxes named "subject"
try (Connection c = ds.getConnection()) {
  c.setAutoCommit(false);
  try (PreparedStatement psService = c.prepareStatement(
         "INSERT INTO service(serviceid,servicecategory,servicetype,studentid) VALUES (?,?,?,?)")) {
    // set params and psService.executeUpdate()
  }
  try (PreparedStatement psLink = c.prepareStatement(
         "INSERT INTO service_subject(serviceid,subjectid) VALUES (?,?)")) {
    for (String s : chosen) {
      psLink.setString(1, serviceId);
      psLink.setString(2, s);
      psLink.addBatch();
    }
    psLink.executeBatch();
  }
  c.commit();
}

Short alternative (store CSV in the SERVICE row)
If you must keep a CSV string, build it safely: check for null, then use String.join(",", chosen) and persist that single string with a PreparedStatement. Avoid hand‑rolled loops that cause NPEs.

Practical troubleshooting notes for

  • Give all checkboxes the same name (e.g. name="subject") and remove duplicate id attributes.
  • Null‑check request.getParameterValues(...) before iterating.
  • Fix registerSubject: use the correct ResultSet variable, call rs.next() before getString(...), and switch SELECT vs INSERT logic as needed.
  • Use PreparedStatement, explicit transactions when inserting multiple rows, and print the generated SQL / exceptions while debugging.
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.