从java创建并填充数据库

时间:2017-01-20 13:56:45

标签: java mysql

编辑:我现在正在使用prepareStatement,代码如下:

            Statement st = con.createStatement();


        st.executeUpdate("CREATE TABLE IF NOT EXISTS Xbox_One (id INT AUTO_INCREMENT, "
                + "PRIMARY KEY(id), Juego VARCHAR(500), URL VARCHAR (200), Lanzamiento VARCHAR (50), Descripcion TEXT(2000),"
                + "Pegi VARCHAR(10), Descripcion_Pegi VARCHAR (200), nota FLOAT(10,1),"
                + "USA VARCHAR (100), USA_Gold VARCHAR (100), USA_sin_Gold VARCHAR (100), USA_EA VARCHAR (100),"
                + "ARG VARCHAR (100), ARG_Gold VARCHAR (100), ARG_sin_Gold VARCHAR (100), ARG_EA VARCHAR (100),"
                + "AUS VARCHAR (100), AUS_Gold VARCHAR (100), AUS_sin_Gold VARCHAR (100), AUS_EA VARCHAR (100),"
                + "BRA VARCHAR (100), BRA_Gold VARCHAR (100), BRA_sin_Gold VARCHAR (100), BRA_EA VARCHAR (100),"
                + "CAN VARCHAR (100), CAN_Gold VARCHAR (100), CAN_sin_Gold VARCHAR (100), CAN_EA VARCHAR (100),"
                + "SIN VARCHAR (100), SIN_Gold VARCHAR (100), SIN_sin_Gold VARCHAR (100), SIN_EA VARCHAR (100),"
                + "ESP VARCHAR (100), ESP_Gold VARCHAR (100), ESP_sin_Gold VARCHAR (100), ESP_EA VARCHAR (100),"
                + "HK VARCHAR (100), HK_Gold VARCHAR (100), HK_sin_Gold VARCHAR (100), HK_EA VARCHAR (100),"
                + "MEX VARCHAR (100), MEX_Gold VARCHAR (100), MEX_sin_Gold VARCHAR (100), MEX_EA VARCHAR (100),"
                + "COL VARCHAR (100), COL_Gold VARCHAR (100), COL_sin_Gold VARCHAR (100), COL_EA VARCHAR (100),"
                + "HUN VARCHAR (100), HUN_Gold VARCHAR (100), HUN_sin_Gold VARCHAR (100), HUN_EA VARCHAR (100),"
                + "SUD VARCHAR (100), SUD_Gold VARCHAR (100), SUD_sin_Gold VARCHAR (100), SUD_EA VARCHAR (100),"
                + "RUS VARCHAR (100), RUS_Gold VARCHAR (100), RUS_sin_Gold VARCHAR (100), RUS_EA VARCHAR (100),"
                + "CHI VARCHAR (100), CHI_Gold VARCHAR (100), CHI_sin_Gold VARCHAR (100), CHI_EA VARCHAR (100),"
                + "IND VARCHAR (100), IND_Gold VARCHAR (100), IND_sin_Gold VARCHAR (100), IND_EA VARCHAR (100),"
                + "JP VARCHAR (100), JP_Gold VARCHAR (100), JP_sin_Gold VARCHAR (100), JP_EA VARCHAR (100),"
                + "KOR VARCHAR (100), KOR_Gold VARCHAR (100), KOR_sin_Gold VARCHAR (100), KOR_EA VARCHAR (100),"
                + "TAI VARCHAR (100), TAI_Gold VARCHAR (100), TAI_sin_Gold VARCHAR (100), TAI_EA VARCHAR (100),"
                + "UK VARCHAR (100), UK_Gold VARCHAR (100), UK_sin_Gold VARCHAR (100), UK_EA VARCHAR (100))"
                );

我收到以下错误:

  

您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以获得正确的语法,以便使用近似值('火箭联盟''火箭联盟/ c125w9bg2k0v',' ; 2016-02-17',' Psyonix,In'第1行"

如果我尝试手动插入数据,我也有问题。如您所见,类型为VARCHAR。

编辑完成。

我遇到了将代码从java引入Mysql中的表的问题。

首先我创建表格,如果它不存在:

for (int i = 1; i<listaEmpresaA.length; i++) { //la fila 1 es la del header que no nos interesa
                    for (int j=0; j<1; j++) {
                        st.executeUpdate("INSERT INTO Xbox_One (Juego, URL, Publicado, Lanzamiento, Descripcion, Pegi, Descripcion_Pegi, nota, "
                                + "USA, USA_Gold, USA_sin_Gold, USA_EA,"
                                + "ARG, ARG_Gold, ARG_sin_Gold, ARG_EA,"
                                + "AUS, AUS_Gold, AUS_sin_Gold, AUS_EA,"
                                + "BRA, BRA_Gold, BRA_sin_Gold, BRA_EA,"
                                + "CAN, CAN_Gold, CAN_sin_Gold, CAN_EA,"
                                + "SIN, SIN_Gold, SIN_sin_Gold, SIN_EA,"
                                + "ESP, ESP_Gold, ESP_sin_Gold, ESP_EA,"
                                + "HK, HK_Gold, HK_sin_Gold, HK_EA,"
                                + "MEX, MEX_Gold, MEX_sin_Gold, MEX_EA,"
                                + "COL, COL_Gold, COL_sin_Gold, COL_EA,"
                                + "HUN, HUN_Gold, HUN_sin_Gold, HUN_EA,"
                                + "SUD, SUD_Gold, SUD_sin_Gold, SUD_EA,"
                                + "RUS, RUS_Gold, RUS_sin_Gold, RUS_EA,"
                                + "CHI, CHI_Gold, CHI_sin_Gold, CHI_EA,"
                                + "CHINA, CHINA_Gold, CHINA_sin_Gold, CHINA_EA,"
                                + "IND, IND_Gold, IND_sin_Gold, IND_EA,"
                                + "JP, JP_Gold, JP_sin_Gold, JP_EA,"
                                + "KOR, KOR_Gold, KOR_sin_Gold, KOR_EA,"
                                + "TAI, TAI_Gold, TAI_sin_Gold, TAI_EA,"
                                + "UK, UK_Gold, UK_sin_Gold, UK_EA)"
                                + ") VALUES ('"
                                +listaEmpresaA[i][j]+"','"+listaEmpresaA[i][j+1]+"','"+listaEmpresaA[i][j+2]+"', '"+listaEmpresaA[i][j+3]+"', '"+listaEmpresaA[i][j+4]+"', '"+listaEmpresaA[i][j+5]+"'"
                                +listaEmpresaA[i][j+6]+"','"+listaEmpresaA[i][j+7]+"',"
                                + "'"+listaEmpresaA[i][j+8]+"', '"+listaEmpresaA[i][j+9]+"'"                                        
                                +listaEmpresaA[i][j+10]+"','"+listaEmpresaA[i][j+11]+"','"+listaEmpresaA[i][j+12]+"', '"+listaEmpresaA[i][j+13]+"'"
                                +listaEmpresaA[i][j+14]+"','"+listaEmpresaA[i][j+15]+"','"+listaEmpresaA[i][j+16]+"', '"+listaEmpresaA[i][j+17]+"'"
                                +listaEmpresaA[i][j+18]+"','"+listaEmpresaA[i][j+19]+"','"+listaEmpresaA[i][j+20]+"', '"+listaEmpresaA[i][j+21]+"'"
                                +listaEmpresaA[i][j+22]+"','"+listaEmpresaA[i][j+23]+"','"+listaEmpresaA[i][j+24]+"', '"+listaEmpresaA[i][j+25]+"'"
                                +listaEmpresaA[i][j+26]+"','"+listaEmpresaA[i][j+27]+"','"+listaEmpresaA[i][j+28]+"', '"+listaEmpresaA[i][j+29]+"'"
                                +listaEmpresaA[i][j+30]+"','"+listaEmpresaA[i][j+31]+"','"+listaEmpresaA[i][j+32]+"', '"+listaEmpresaA[i][j+33]+"'"                                     
                                +listaEmpresaA[i][j+34]+"','"+listaEmpresaA[i][j+35]+"','"+listaEmpresaA[i][j+36]+"', '"+listaEmpresaA[i][j+37]+"'"
                                +listaEmpresaA[i][j+38]+"','"+listaEmpresaA[i][j+39]+"','"+listaEmpresaA[i][j+40]+"', '"+listaEmpresaA[i][j+41]+"'"
                                +listaEmpresaA[i][j+42]+"','"+listaEmpresaA[i][j+43]+"','"+listaEmpresaA[i][j+44]+"', '"+listaEmpresaA[i][j+45]+"'"
                                +listaEmpresaA[i][j+46]+"','"+listaEmpresaA[i][j+47]+"','"+listaEmpresaA[i][j+48]+"', '"+listaEmpresaA[i][j+49]+"'"
                                +listaEmpresaA[i][j+50]+"','"+listaEmpresaA[i][j+51]+"','"+listaEmpresaA[i][j+52]+"', '"+listaEmpresaA[i][j+53]+"'"
                                +listaEmpresaA[i][j+54]+"','"+listaEmpresaA[i][j+55]+"','"+listaEmpresaA[i][j+56]+"', '"+listaEmpresaA[i][j+57]+"'"
                                +listaEmpresaA[i][j+58]+"','"+listaEmpresaA[i][j+59]+"','"+listaEmpresaA[i][j+60]+"', '"+listaEmpresaA[i][j+61]+"'"
                                +listaEmpresaA[i][j+62]+"','"+listaEmpresaA[i][j+63]+"','"+listaEmpresaA[i][j+64]+"', '"+listaEmpresaA[i][j+65]+"'"
                                +listaEmpresaA[i][j+66]+"','"+listaEmpresaA[i][j+67]+"','"+listaEmpresaA[i][j+68]+"', '"+listaEmpresaA[i][j+69]+"'"
                                +listaEmpresaA[i][j+70]+"','"+listaEmpresaA[i][j+71]+"','"+listaEmpresaA[i][j+72]+"', '"+listaEmpresaA[i][j+73]+"'"                                     
                                +listaEmpresaA[i][j+74]+"','"+listaEmpresaA[i][j+75]+"','"+listaEmpresaA[i][j+76]+"', '"+listaEmpresaA[i][j+77]+"'"
                                +listaEmpresaA[i][j+78]+"','"+listaEmpresaA[i][j+79]+"','"+listaEmpresaA[i][j+80]+"', '"+listaEmpresaA[i][j+81]+"'"
                                +listaEmpresaA[i][j+82]+"','"+listaEmpresaA[i][j+83]+"','"+listaEmpresaA[i][j+84]+"', '"+listaEmpresaA[i][j+85]+"'"
                                +listaEmpresaA[i][j+86]+"','"+listaEmpresaA[i][j+87]+"','"+listaEmpresaA[i][j+88]+"', '"+listaEmpresaA[i][j+89]+"'"
                                +listaEmpresaA[i][j+90]+"','"+listaEmpresaA[i][j+91]+"','"+listaEmpresaA[i][j+92]+"', '"+listaEmpresaA[i][j+93]+"'"
                                +listaEmpresaA[i][j+94]+"','"+listaEmpresaA[i][j+95]+"'"        
                                + ")");
                    }
                }

此步骤正确,表已创建。问题是我找不到引入数据的方法,数据存储在名为listaEmpresaA的二维数组中。

以下是我正在使用的代码:

{{1}}

可能有点乱,但我还没有找到任何其他办法。

如果您能给我一些提示或指出错误,我将不胜感激。执行中没有错误。

感谢您的建议。

1 个答案:

答案 0 :(得分:1)

这将通过PreparedStatement完成。

这对于防止SQL注入很有用,如果执行相同的查询,它将更有效。

首先,使用?准备语句,以指定循环外参数的位置。

PreparedStatement ps = conn.preparedStatement(
      "insert into MY_TABLE (A,B,C,D) values (?,?,?,?)"
):

这将预编译查询。然后,您只需使用循环中为每个实例提供的setter(取决于类型)设置值

ps.setString(1, instance.getA());
ps.setString(2, instance.getB());
ps.setString(3, instance.getC());
ps.setString(4, instance.getD());

<强> !!该指数是基于1的!!!

当然,在这里你应该在数据数组上使用循环来设置值

您只需要使用PreparedStatement.execute()执行此语句。您可以反复重复使用相同的语句(在循环中),只需清除参数即可。

这看起来像是:

PreparedStatement ps = conn.preparedStatement(
  "INSERT INTO Xbox_One (Juego, URL, Publicado, Lanzamiento, Descripcion, "
            ..... + ") VALUES (?,?,?,?,? .... )";
);

for(String[] array : listaEmpresaA){
    for(int i = 0; i < 95; ++i){ //Careful to an arrayoutofbounds exception, but every parameter MUST BE SET
        ps.setString(i+1, array[i]); //Can't be null, if so, must use setNull(i+1);
    }
    ps.execute();
}

编辑:

您得到的错误来自您编写的查询。通过打印您使用的String,我得到了这个

INSERT INTO Xbox_One (
    Juego, URL, Lanzamiento, ... , UK_sin_Gold, UK_EA)) 
VALUES (
    ?,?,?, ... ,?,?)

然后你会看到在VALUES之前有两个括号,而不是一个