简体   繁体   English

插入数据库表Java

[英]insert into DB table java

I want to fill a table using insert statement. 我想使用插入语句填充表格。 To get the data I used arrays that contain the data I need. 为了获得数据,我使用了包含所需数据的数组。

I want to insert the id and some other values in this format. 我想以这种格式插入ID和其他一些值。

id w0   w1   w2    w3
1  0.0  1.0  0.54 0.0 
2  1.0  0.5  0.0  0.8 
and others 

id ---> read from a normal array id --->从普通数组读取

the other numbers ---> read from a 2d array 其他数字--->从二维数组中读取

and I want for each new row a new id. 我想要为每个新行添加一个新ID。

The problem I have is the rows are moving and incrementing while the id is stuck in 0, so each new row is added with the same id which is wrong since the id is a primary key. 我的问题是ID固定为0时,行正在移动和递增,因此每个新行都添加了相同的ID,这是错误的,因为ID是主键。

String sqlselect=new String("select k_id from keywordsTable");
        ResultSet rss = stmt1.executeQuery(sqlselect);
        kId=new String[numOfFields];
        String f="";
        String ff="";
        String ffff="";
            for (int i=0;i<kId.length;i++) {
                while(rss.next())   
                {
                kId[i]=rss.getString(1);
                f="w"+kId[i]+ " varchar(20)";
                ff+="w"+kId[i]+", ";
                ffff+="w"+kId[i]+", ";
                //fff+="w"+kId[i]+ " varchar(20), ";
                String sqlalter=new String("ALTER TABLE "+ tableName +" add "+f+"");
                //System.out.println(sqlalter);
                stmt1.executeUpdate(sqlalter);
            }

        }
            ff = ff.replaceAll(", $","");
            ffff = ffff.replaceAll(", $","");


String sqlselectF=new String("select f_id from filesTable");
            ResultSet rssF = stmt1.executeQuery(sqlselectF);
            FId=new String[numOfFiles];

            String matInsert = null;
            String g="";
            String seperator = "";

                for (int s=0;s<FId.length;s++) {
                    while(rssF.next())  
                    {
                    FId[s]=rssF.getString(1);
                    g=seperator+FId[s];

            for (int k=0;k<di.mat.length;k++) {  //row
                String m="";
                String sep = "";
            for (int j=0;j<di.mat[k].length;j++) { //col

                m+= (sep+di.mat[k][j]);
                sep = " ,";
                matInsert=new String("INSERT INTO "+ tableName  +"(id,"+ffff+")"+"values" +"("+g+","+m+")");

        } //col 
            System.out.println("ff="+g);
            System.out.println(matInsert);
            stmt1.executeUpdate(matInsert);

                    } //row


                    } //while

        } //for 1


    seperator=" ,";

You shouldn't insert IDs, configure the column as AUTO_INCREMENT and let MySQL assign the IDs upon each INSERT. 您不应该插入ID,将列配置为AUTO_INCREMENT,然后让MySQL在每个INSERT上分配ID。

Second, the nesting of while loop inside a for loop is probably a logical mistake. 其次,在for循环中嵌套while循环可能是一个逻辑错误。
Third, ffff is not declared anywhere in the code that you posted. 第三,在您发布的代码中的任何地方都没有声明ffff
Forth, the INSERT query is missing whitespaces in a few places, it will come out (if all the input is correct) as something like: 第四,INSERT查询在某些地方缺少空格,它将以如下形式出现(如果所有输入正确):

INSERT INTO tableName(id,ffff)values(x,y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM