Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [prepared statements] indice di colonna errato

    Salve a tutti, sto cercando di introdurre le prepared statement nella mia applicazione esercizio che sto preparando per scuola.
    Ecco il codice:
    codice:
        public int create(User u) {
            Connection c = DBDataSource.getJDBCConnection();
            try {
                String sql = "INSERT into users (id ,nick, pass) VALUES (seq_users.nextval, '?','?')";
                OraclePreparedStatement st = (OraclePreparedStatement) c.prepareStatement(sql);
                st.setString(1, u.getNick());
                st.setString(2, u.getPass());
                st.executeUpdate();
                c.commit();
                c.close();
                return u.getId();
            } catch (SQLException ex) {
                Logger.getLogger(UsersDAO.class.getName()).log(Level.SEVERE, null, ex);
                return -1;
            }
    
        }
    Ed e il log degli errori:

    codice:
    19-nov-2009 14:16:57 DAO.UsersDAO create
    GRAVE: null
    java.sql.SQLException: Indice di colonna non valido
            at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
            at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
            at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
            at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:5360)
            at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:5352)
            at DAO.UsersDAO.create(UsersDAO.java:27)
            at userdao.main(userdao.java:19)
    Qualcuno mi sa dire dove sbaglio? Io davvero non riesco a capire.
    Grazie del vostro prezioso aiuto
    If today was perfect, there would be no need for tomorrow...
    Live as you want, the wildest way you can, till you're alive!!

  2. #2
    Utente di HTML.it L'avatar di neroux
    Registrato dal
    Aug 2009
    Messaggi
    1,973
    Non so se cambi tanto ma prova senza ' per i wildcard.
    codice:
    public int create(User u)
    {
        Connection c = DBDataSource.getJDBCConnection();
        try
        {
            PreparedStatement st = c.prepareStatement("INSERT into users (id ,nick, pass) VALUES (seq_users.nextval, ?, ?)");
            st.setString(1, u.getNick());
            st.setString(2, u.getPass());
            st.executeUpdate();
            c.commit();
            c.close();
            return u.getId();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(UsersDAO.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        }
    }

    Ma perché fai un return di un valore che hai passato come parametro
    codice:
    public void create(User u) throws SQLException
    {
        Connection c = DBDataSource.getJDBCConnection();
        PreparedStatement st = c.prepareStatement("INSERT into users (id ,nick, pass) VALUES (seq_users.nextval, ?, ?)");
        st.setString(1, u.getNick());
        st.setString(2, u.getPass());
        st.executeUpdate();
        c.commit();
        c.close();
    }

    www.sitemeer.com » Quando un sito pare irraggiungibile

    Se ti piace ci puoi trovare anche su Facebook

  3. #3
    senza quote ho già provato... niente

    per il valore di ritorno effettivamente il metodo è poco ortodosso, lo risonosco.
    sarebbe stato meglio usare un boolean.

    altri suggerimenti?
    If today was perfect, there would be no need for tomorrow...
    Live as you want, the wildest way you can, till you're alive!!

  4. #4
    Utente di HTML.it
    Registrato dal
    Jun 2009
    Messaggi
    347
    ciao, nella tabella del db ci sono tutti i campi? (id ,nick, pass)

  5. #5
    certo
    If today was perfect, there would be no need for tomorrow...
    Live as you want, the wildest way you can, till you're alive!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.