1 2 Previous Next 15 Replies Latest reply: Apr 23, 2010 8:33 AM by masijade RSS

    Without Prepared statement -> With Prepared statement

    807580
      I have this code without prepared statement but I need it WITH prepared statement.



      Without : (works)
      public String[][]  zoekGebruikerViaNaam(String naam) throws SQLException {
                   openDatabank();
                   String[][] gevondenAbonnees = null;
                   String sql = "SELECT tblAbonnee.GebruikersID, tblAbonnee.Voornaam, tblAbonnee.Naam FROM tblAbonnee"
                    + " WHERE (((tblAbonnee.Voornaam)= '" + naam +"' )) OR (((tblAbonnee.Naam)= '" + naam +"'))";
                     stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                   
                   
                   try {
                        ResultSet rs = stmt.executeQuery(sql);
                        ResultSetMetaData rsMetaData = rs.getMetaData();
                        // Als er gevonden zijn, dus rs.first is true 
                        if (rs.first()) {
                             int kolom = rsMetaData.getColumnCount();
                             rs.last();
                             int rij = rs.getRow();       
                             rs.first();
                             
                             gevondenAbonnees = new String [rij][kolom];
                             for (int i=0; i<rij; i++){
                                  for (int j=0; j<kolom; j++) {
                                       gevondenAbonnees[i][j] = rs.getString(j+1);
                                  }
                                  rs.next();
                             }
                        }
                     // als er geen gevonden zijn, die string gewoon gelijkstellen aan ding null (dit is enkel voor binnenin progr van belang)
                        else {
                             gevondenAbonnees = new String[1][1];
                             gevondenAbonnees[0][0] = null;
                        }
                             
                   }
                   
                   catch (Exception e) {
                        System.out.println("Fout bij zoeken naar gebruiker met naam");
                        stmt.close();
                      System.exit(0);
                   }
                   return gevondenAbonnees;     
                
                   
                
                
           }
      With prepared statement : ( I have no idea what I'm doing wrong)
      public String[][]  zoekGebruikerViaNaam(String naam) throws SQLException {
                   openDatabank();
                   String[][] gevondenAbonnees = null;
                   String sql = "SELECT tblAbonnee.GebruikersID, tblAbonnee.Voornaam, tblAbonnee.Naam FROM tblAbonnee"
                         + " WHERE (((tblAbonnee.Voornaam)= ? )) OR (((tblAbonnee.Naam)= ?))"; 
                        PreparedStatement myStmt = con.prepareStatement(sql);
                        myStmt.setString(1, naam); // Naam = String declared in the begining
                        myStmt.setString(2,naam);
                        ResultSet rs;
                        
                        
                        try {
                             rs = myStmt.executeQuery();
                             // Als er gevonden zijn, dus rs.first is true 
                             if (rs.first() == false) {
                                  gevondenAbonnees = new String[1][1];
                                  gevondenAbonnees[0][0] = null;
                             }
                             else {
                                  rs.first();
                                  int kolom = rs.getMetaData().getColumnCount();
                                  rs.last();
                                  int rij = rs.getRow();       
                                  rs.first();
                                  
                                  gevondenAbonnees = new String [rij][kolom];
                                  for (int i=0; i<rij; i++){
                                       for (int j=0; j<kolom; j++) {
                                            gevondenAbonnees[i][j] = rs.getString(j+1);
                                       }
                                       rs.next();
                                  }
                                  
                             }
                                  
                        }
                   
                   catch (Exception e) {
                        System.out.println("Fout bij zoeken naar gebruiker met naam");
                        stmt.close();
                      System.exit(0);
                   }
                   return gevondenAbonnees;     
                
                   
                
                
           }
      It gives the error of the Exception e. Anyone can help me? I think there's something wrong with the setString or the executeQuery..
        1 2 Previous Next