Skip to content
Advertisement

Universal solution to insert Userinput values in MySQL database with a priori unknown columns with Java and for example PreparedStatements

I want to insert a whole set of values in my table in MySQL (e.g.

JavaScript

This works in MySQL Workbench 8.0 but not in my Java Code. I read the JavaDoc about prepared Statements but I didn’t find any clues on inserting values if I don’t know the type a priori. My Java code looks like this:

JavaScript

I tried many things like resultSet = statement.executeQuery("INSERT INTO " + Tabelle + " (" + Insertabfrage + ") VALUES (" + SQLInsert + " );"); So my Question is, is there a possibility to get the Datatype of the columns and insert it into my database wwfsaew? Something like this:

JavaScript

Advertisement

Answer

I found the problems:

  1. String sql was flawed –> double values().
  2. There wasn’t an execute command for preparedStatement
  3. for loop from XtremeBaumer in the comments
  4. ExceptionHandling for Foreign Keys I hope this helps somebody in the future 🙂
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement