Salve,
ho sviluppato questo metodo dato che ho una serie di query non riesco a sistemare il PreparedStatement cioè non so come fare se metto i punti di domanda come indicarli per poterli assegnare alla variabile(String) non sono sicuro di esser stato chiaro!!!!!!!!
Non riesco anche a far funzionare LIKE!codice:public Libro[] cerca_libro(String titolo, String autore, String editore, String annopub, int quantiLibri) { Libro libro[] = new Libro[quantiLibri]; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost/biblioteca"; String usr = "root"; String pass = "root"; Connection con = (Connection) DriverManager.getConnection(url, usr, pass); String sql="SELECT titolo,autore,editore,anno_publicazione FROM Libro"; sql+=" WHERE "; if(titolo != null && autore.equals("") && editore.equals("") && annopub.equals("")) { sql+=" titolo like ?"; //inserisco ? } else if (titolo.equals("") && autore != null && editore.equals("") && annopub.equals("")) { sql+=" autore='" + autore + "'"; } else if (titolo.equals("") && autore.equals("") && editore != null && annopub.equals("")) { sql+=" editore='" + editore + "'"; } else if (titolo.equals("") && autore.equals("") && editore.equals("") && annopub != null) { sql+=" anno_publicazione='" + annopub + "'"; } else if (titolo != null && autore != null && editore.equals("") && annopub.equals("")) { sql+= " titolo= '" + titolo + "' and autore = '" + autore + "'"; } else if (titolo != null && autore.equals("") && editore != null && annopub.equals("")) { sql+= " titolo= '" + titolo + "' and editore = '" + editore + "'"; } else if (titolo != null && autore.equals("") && editore.equals("") && annopub != null) { sql+= " titolo= '" + titolo + "' and anno_publicazione = '" + annopub + "'"; } else if (titolo.equals("") && autore != null && editore != null && annopub.equals("")) { sql+= " autore= '" + autore + "' and editore = '" + editore + "'"; } else if (titolo.equals("") && autore != null && editore.equals("") && annopub != null) { sql+= " WHERE autore = '" + autore + "' and anno_publicazione = '" + annopub + "'"; } else if (titolo.equals("") && autore.equals("") && editore != null && annopub != null) { sql+= " editore = '" + editore + "' and anno_publicazione = '" + annopub + "'"; } else if (titolo != null && autore != null && editore != null && annopub.equals("")) { sql+= " titolo= '" + titolo + "' and autore = '" + autore + "' and editore = '" + editore + "'"; } else if (titolo != null && autore != null && editore.equals("") && annopub != null) { sql+= " titolo= '" + titolo + "' and autore = '" + autore + "' and anno_publicazione = '" + annopub + "'"; } else if (titolo != null && autore.equals("") && editore != null && annopub != null) { sql+= " titolo= '" + titolo + "' and editore = '" + editore + "' and anno_publicazione = '" + annopub + "'"; } else if (titolo.equals("") && autore != null && editore != null && annopub != null) { sql+= " autore = '" + autore + "' and editore = '" + editore + "' and anno_publicazione = '" + annopub + "'"; } else if (titolo != null && autore != null && editore != null && annopub != null) { sql+= " WHERE titolo= '" + titolo + "' and autore = '" + autore + "' and editore = '" + editore + "' and anno_publicazione = '" + annopub + "'"; } PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sql); pstmt.setString(1, titolo+"%"); // dove ho il primo ? inserisco la viaribile titolo /*pstmt.setString(2, autore); pstmt.setString(3, editore); pstmt.setString(4, annopub);*/ ResultSet risultato = pstmt.executeQuery(); int i=0; while (risultato.next() && i < libro.length) { libro[i] = new Libro(risultato.getString("titolo"), risultato.getString("autore"), risultato.getString("editore"), risultato.getString("anno_publicazione")); i++; } con.close(); pstmt.close(); risultato.close(); } catch (Exception exception) { System.out.println("erroreeeeeeeee cerca_libro " + exception.getMessage()); } return libro; }
Grazie

Rispondi quotando