Java 如何插入列表<;字符串[]>;使用JDBC将数据导入数据库?

Java 如何插入列表<;字符串[]>;使用JDBC将数据导入数据库?,java,for-loop,jdbc,insert,prepared-statement,Java,For Loop,Jdbc,Insert,Prepared Statement,我的列表的当前格式为: 60 52 0 0 1512230400 76 52 1 1 1514044800 42 52 4 1 1516464000 其中每个按空格分隔的值都是我的数据库表中的一行,例如:60 52 0 0 1512230400。我想在每个循环中插入5个单独的值。我想将所有这些行插入我的数据库,但不确定具体如何插入。到目前为止,这也是到我的数据库的工作连接 这是我的大致想法: String query = "INSERT INTO games (team1_id, team2

我的
列表的当前格式为:

60 52 0 0 1512230400
76 52 1 1 1514044800 
42 52 4 1 1516464000
其中每个按空格分隔的值都是我的数据库表中的一行,例如:
60 52 0 0 1512230400
。我想在每个循环中插入5个单独的值。我想将所有这些行插入我的数据库,但不确定具体如何插入。到目前为止,这也是到我的数据库的工作连接

这是我的大致想法:

String query = "INSERT INTO games (team1_id, team2_id, score1, score2, created_at) VALUES (? ,?, ?, ?, ? )";
Connection con = DBConnector.connect();
PreparedStatement stmt = con.prepareStatement(query);//prepare the SQL Query

for (String[] s : fixtures) {

}
任何帮助都是惊人的


非常感谢

在for循环中,您可以执行以下操作:

stmt.setString(1, s[0]); //team1_id if it's of string type in db
stmt.setInt(2, Integer.parseInt(s[1])); //team2_id if it's of type integer in db
stmt.setInt(3, Integer.parseInt(s[2])); //score1
stmt.setInt(4, Integer.parseInt(s[3])); //score2
stmt.setLong(5, Long.parseLong(s[4])); //created_at
stmt.executeUpdate();
上面的代码向您展示了如何处理字符串、Long和Integer,您可以类似地使用其他类型。

List fixtures=new ArrayList();
List<String[]> fixtures = new ArrayList<>();
fixtures.add(new String [] {"60","52","0","0","1512230400"});
fixtures.add(new String [] {"76","52","1","1","1514044800"});
fixtures.add(new String [] {"42","52","4","1","1516464000"});

String query = 
  "INSERT INTO games (team1_id, team2_id, score1, score2, created_at)\n"
  + " VALUES (? ,?, ?, ?, ? )";
try(
  Connection con = DBConnector.connect();
  PreparedStatement stmt = con.prepareStatement(query);
) {
  for (String[] s : fixtures) {
    stmt.setString(1,s[0]);
    stmt.setString(2,s[1]);
    stmt.setString(3,s[2]);
    stmt.setString(4,s[3]);
    stmt.setString(5,s[4]);
    stmt.execute();
  }
  con.commit();
}
add(新字符串[]{“60”、“52”、“0”、“0”、“1512230400”}); add(新字符串[]{“76”、“52”、“1”、“1”、“1514044800”}); 添加(新字符串[]{“42”、“52”、“4”、“1”、“1516464000”}); 字符串查询= “插入到游戏中(team1\u id、team2\u id、score1、score2、created\u at)\n” +“值(?,?,?,?)”; 试一试( Connection con=DBConnector.connect(); PreparedStatement stmt=con.prepareStatement(查询); ) { 对于(字符串[]s:装置){ stmt.setString(1,s[0]); stmt.setString(2,s[1]); stmt.setString(3,s[2]); stmt.setString(4,s[3]); stmt.setString(5,s[4]); stmt.execute(); } con.commit(); }
使用这种方法,我们将绑定变量作为字符串传递。如果需要,根据插入到的列的实际类型,数据库将进行从字符串(VARCHAR)到数字(NUMBER)的转换


您基本上都做对了,但实际上没有采取下一步…

如果已创建输入
列表
,则这可以工作:

List<String[]> fixtures = ...; // assuming this data is already created
String query = "INSERT INTO games (team1_id, team2_id, score1, score2, created_at) VALUES (? ,?, ?, ?, ? )";

try (Connection con = DBConnector.connect();
      PreparedStatement stmt = con.prepareStatement(query)) {

    for (String [] row : fixtures) {

        // This gets executed for each row insert
        for (int i = 0; i < row.length; i++) {
            stmt.setInt(i+1, Integer.parseInt(row[i]);
        }

        stmt.executeUpdate();
    }
}
catch(SQLException ex) {
    ex.printStackTrace();
    // code that handles exception...
}
列出装置=…;//假设此数据已创建
String query=“插入到游戏(team1_id、team2_id、score1、score2、created_at)值(?,,,?,?)”;
try(Connection con=DBConnector.connect();
PreparedStatement stmt=con.prepareStatement(查询)){
对于(字符串[]行:装置){
//这将针对每一行插入执行
for(int i=0;i
您可以使用JDBC批插入: