1

This is my function in my database that insert one user.

public Utente aggiungiUtente(char[] tempo, char[] id_utente, int x, int y, int z, int id_stanza) throws SQLException {

        Connection c = null;
        PreparedStatement s = null;
        ResultSet r = null;
        Utente risultato = null;

        try {
            c = creaConnessione();
            s = c.prepareStatement("INSERT INTO UTENTE(time, id_utente, x, y, z, id_stanza) values (?,?,?,?,?,?) ", Statement.RETURN_GENERATED_KEYS);
            String tempoString = tempo.toString();
            String idUtenteString = id_utente.toString();

//          s.set..(1, tempo);
//          s.set..(2, id_utente);

            s.setInt(3,x);
            s.setInt(4,y);
            s.setInt(5,z);
            s.setInt(6,id_stanza);
            s.execute();
            r = s.getGeneratedKeys();
            r.next();
            risultato = factory.creaUtente(tempo, id_utente, x, y, z, id_stanza);

        } catch (SQLException e) {
            disconnetti(c,s,r);
            throw e;
        }
        disconnetti(c,s,r);
        return risultato;
    }

But I don't know how to set char array. At the creation of database I build them as VARCHAR and for my project I need to do this. I tried to set them as: s.setString(tempo.toString);but within the database will insert a strange thing like this: [C@76ccd017 Who can help me please??

Unknown
  • 2,037
  • 3
  • 30
  • 47
Sara Savio
  • 25
  • 1
  • 8
  • 2
    You can convert the char array to string (String str = String.valueOf(charArray);) and store it in DB – Unknown May 20 '16 at 14:47
  • Thank you! And if also i do a select like this: "SELECT * FROM UTENTE WHERE time >= ? AND time <= ? AND id_stanza = ?" where "time" is an array of char and i want to see if it is between a certain array of characters or another array of character. The comparison with operators is correct ? – Sara Savio May 20 '16 at 15:28

1 Answers1

0

String are nothing more than char arrays. If you convert this char array that you have to String and keep DB side as VARCHAR2 it'll solve your problem.

String class already have a method named 'valueOf' which you can use a char array as parameter and then it returns you a String:

String tempoString = String.valueOf(tempo);
String idUtenteString = String.valueOf(id_utente);
Eduardo Meneses
  • 504
  • 3
  • 18
  • Thank you! And if also i do a select like this: "SELECT * FROM UTENTE WHERE time >= ? AND time <= ? AND id_stanza = ?" where "time" is an array of char and i want to see if it is between a certain array of characters or another array of character. The comparison with operators is correct ? – Sara Savio May 20 '16 at 15:26
  • It'll depend the format you are using this 'time'. If it is a date, firstly, I would recommend you to use a date format in your DB side (date, timestamp and stuffs). You simply cannot check if a string is greater or lesser than another string. You must to convert it to a date using 'to_date' (oracle function, probably in other DB it has another name) function and make this time become a date. – Eduardo Meneses May 20 '16 at 15:39
  • So I have to convert ALL char array (that in my case represent an array of time) in dates so that they can be compared – Sara Savio May 20 '16 at 15:45
  • Basically yes. You can also treat them as Date in your code, then 'tempo' will be a Date or Calendar in java and DATE in oracle. – Eduardo Meneses May 20 '16 at 16:51