数据截断:列数据太长,电话'在第1行

时间:2014-07-11 13:39:12

标签: java mysql sql

我将数据插入到MYSQL数据库的临时表中。我将'Phone'列的大小指定为VARCHAR(20)并插入了正好10个字符的数据并且它是常量。

这是我的代码:

java.sql.PreparedStatement insertStmt = null;

                 String insertQuery = "insert into "+tableName+"(";
                 for(String col : columnList){
                             insertQuery+=col+",";
                         }
                insertQuery = insertQuery.substring(0,insertQuery.lastIndexOf(","));
                         insertQuery+=") values (";
                         for(String col : columnList){
                             insertQuery+="?,";
                         }
                insertQuery = insertQuery.substring(0,insertQuery.lastIndexOf(","));
                            insertQuery+=")";
                         insertStmt = mysqlConn.prepareStatement(insertQuery);

                while(rs.next())
                     {
                    for(int j=1;j<=columnList.size();j++)
                    {
                        insertStmt.setString(j,rs.getString(j).trim());
                        System.out.println(rs.getString(j).trim());
                    }
                    insertStmt.addBatch();
                    i++;

                    }
                insertStmt.executeBatch();  

                mysqlConn.setAutoCommit(true);
                rs = null;

我收到以下错误:

java.sql.BatchUpdateException: Data truncation: Data too long for column 'Phone' at row 1
Completed Temp tables 1&2
    at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)
    at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955)
    at MultipleDataExtraction.addDataToTempTable(MultipleDataExtraction.java:132)
    at MultipleDataExtraction.addDataToFinalTempTable(MultipleDataExtraction.java:94)

表格定义:

EmpNo varchar(15) YES NULL,   Name varchar(18) YES NULL,   Address varchar(40) YES NULL,   Phone varchar(20) YES NULL,   Age varchar(4) YES NULL,  DeptNo varchar(20) YES NULL

插入声明:

insert into temp__final_table(Name,Address,Phone,EmpNo,Age,DeptNo) values ('Clerk#000000959','Skye Norling','2112 Ravine Drive','7091581728','Clerk#000000959','26') 

请帮助..

1 个答案:

答案 0 :(得分:0)

这是你的问题:

&#39; 2112 Ravine Drive&#39;

插入查询中的

insert into temp__final_table(Name,Address,Phone,EmpNo,Age,DeptNo) values ('Clerk#000000959','Skye Norling','2112 Ravine Drive','7091581728','Clerk#000000959','26')

您正在电话栏中插入地址。

此外,'Clerk#000000959'似乎是EmpNo。您似乎将它插入同一行中的多个列中。

此外,建议在必要时使用numeric字段。您的DeptNo看起来像整数类型的完美候选者。您不需要为所有列使用varchar。

希望这有帮助!!!