Java takes the value entered from the input form

String s = new String(request.getParameter("authorName")); Пример : authorName = "user1 user2 user3" 

Then makes a split and gets

  user1 user2 user3 

how to transfer all these values ​​in one PreparedStatement

 query1 = connection.prepareStatement("INSERT INTO authors (author_name) VALUES(?)"); query1.setString(1, AuthorName); 

if done in this form

  for (String retval: s.split(" ")) { document.setAuthorName(retval); } 

then it only transmits the last value that is

 user3 

but it is necessary to transfer everything + the number of split lines dynamically.

  • And what does the setAuthorName function do? In a good way, the loop should execute: query1.setString(1, retval); query1.executeUpdate(); query1.setString(1, retval); query1.executeUpdate(); (based on examples from docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html ) - Mike
  • these are getters and setters. Get allows you to get values ​​(read values), and Set - write values ​​to a variable. - Eugene
  • Those. it simply sets the class property to the specified value. Changing one variable to the information in the database usually does not. You need to do what I wrote above in a cycle - Mike

2 answers 2

If I understand correctly, you want to break the line into parts and write to the database as separate records. At the same time, to speed up the work, you want to use one pre-prepared request. It will look like this:

  String s = "user1 user2 user3"; PreparedStatement query1 = conn.prepareStatement("INSERT INTO authors (author_name) VALUES(?)"); for (String retval: s.split(" ")) { query1.setString(1, retval); query1.executeUpdate(); } 

Example at www.tutorialspoint.com . Click execute, then to check, at the bottom of the console type echo "use test; select * from authors" | mysql -u root -proot echo "use test; select * from authors" | mysql -u root -proot

    As an option, send several requests to add values ​​to a table in a loop, where the cycle goes от i = 0 до i < количество строк полученных сплитом 6889065 / inserting-multiple-rows-in-mysql