INSERTAR una nueva fila en la base de datos java?

I have the simple following code:

try {   
    Main.db.getCon().createStatement().executeUpdate("INSERT INTO qos_qoe (channel_id) VALUES ('vtv1') ");
    Main.db.getCon().createStatement().executeUpdate("UPDATE qos_qoe SET channel_id = 'vtv1' WHERE id = 3 ");

} catch (SQLException e) {
// TODO Auto-generated catch block

The UPDATE command runs well but INSERT command does not. Runs that INSERT command directly in phpmyadmin shows no problem. Where does the problem lie in? Thanks for any reply!

preguntado el 08 de noviembre de 11 a las 10:11

"Does not run" is not enough information. Do you get an exception? If yes which one. -

Sorry! I'm still new to Java so i only know the code can't run if i don't comment the INSERT line. -

Then show us the stacktrace you get if you un-comment the line -

Is there any exceptions thrown in the console? -

Also, I believe you should care sharing the table structure (incl. primary key & unique indexes at least). -

1 Respuestas

Probablemente rated or vote_count is not of char type in the db. Anyway, you should use Declaración preparada I'll assume all your variables are Strings

String statement = "UPDATE iptv_media_source SET rated = ?, vote_count = ? WHERE channel_id= ? ";
PreparedStatement st = Main.db.getCon().prepareStatement(statement);
st.setString(1, rated);
st.setString(2, vote_count);
st.setString(3, channelId);    

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.