enter image description here

On this form, words separated by spaces are entered into the Authors and Keywords fields. How to immediately parse 2 arrays and transfer them to the database with one request, if possible?

tried like this:

connection = connector.getConnection(); query = connection.prepareStatement(INSERT INTO document_s (document_id, document_name, document_type, creation_date, parent_id) VALUES(?,?,?,?,?), Statement.RETURN_GENERATED_KEYS); query.setNull(1, Types.INTEGER); query.setString(2, docid.getDoc_name()); query.setString(3, docid.getDoc_type()); query.setString(4, docid.getCreation_date()); query.setInt(5, docid.getParent_id()); query.executeUpdate(); resultSet = query.getGeneratedKeys(); resultSet.next(); int key = resultSet.getInt(1); query1 = connection.prepareStatement(INSERT INTO document_r(index, document_id, authors, keywords) VALUES(?,?,?,?)); query1.setNull(1, Types.INTEGER); query1.setInt(2, key); query1.setString(3, docid.getAuthors()); query1.setString(4, docid.getKeywords()); query1.executeUpdate(); return 0; } catch (SQLException e) { e.printStackTrace(); } finally { connector.close1(connection, query, resultSet); } return 0; 

I mean what needs to be done through a for loop like this:

  for (String retval: s.split(" ")) { query1.setString(1, retval); query1.executeUpdate(); } 

it needs to be done for two lines at once.

The result should be like this

enter image description here

PS Help

  • You got three authors and four keywords. What will you do with them? - Anton Shchyrov
  • @Anton Shchyrov enter into the database in such a format 1 1 evg bas || 2 1 ref nas || 3 1 gef mas || 4 1 ras || - Eugene
  • Those. will 3 * 4 = 12 records be added to the database? - Anton Shchyrov
  • @a 4 entries I formatted the top comment - Eugene
  • @AntonShchyrov edited at the end of the screen how it should look At the end - Eugene

2 answers 2

 //int key = ?; String[] autors = docid.getAuthors().split( " " ); String[] keywords = docid.getKeywords().split( " " ); for ( int i = 0; i < Math.max( autors.length, keywords.lenght ); i++ ) { query.setInt( 1, i+1 ); query.setInt( 2, key ); if ( i >= autors.length ) { query.setNull( 3, Types./*N*/VARCHAR ); } else { query.setString( 3, autors[i] ); } if ( i >= keywords.length ) { query.setNull( 4, Types./*N*/VARCHAR ); } else { query.setString( 4, keywords[i] ); } query.executeUpdate(); } 
  • in this situation, it gives an error No value specified for Parameter 3 in the debug, it just flies by - Eugene
  • @ Eugene It is difficult to say what the matter is - what input data, on which i occurs, are there any typos, does the driver support all data types? - Andrey M
  • there was a typo, only now I have sql. Says that an error in the request, although I register the same thing in Mysql itself and passes and JDBC says Achtung index, document_id, authors, keywords) VALUES (1,54, 'evg', 'sda') - Yevgeny
 String[] authors = docid.getAuthors().split(" "); String[] keywords = docid.getKeywords().split(" "); int minLen = Math.min(authors.length, keywords.length); for (int i = 0; i < minLen; i++) { query1.setString(3, authors[i]); query1.setString(4, keyword[i]); query1.executeUpdate(); } query1.setNull(3, Types.STRING); for (int i = minLen; i < keywords.length; i++) { query1.setString(4, keyword[i]); query1.executeUpdate(); } query1.setNull(4, Types.STRING); for (int i = minLen; i < authors.length; i++) { query1.setString(3, authors[i]); query1.executeUpdate(); } 
  • But is it necessary to do an update after each setString? After all, I need to get the insert lines with the first values, then the insert with the second, and so on - Eugene
  • @ Yevgeny Well, that's how it will turn out. setString sets the value of the executeUpdate parameter executes the query with the specified parameters on the server - Anton Shchyrov