Java 遇到错误:数据截断:第 1 行“StudentID”列的值超出范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9718043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
encounter ERROR: Data truncation: Out of range value for column 'StudentID' at row 1
提问by charcoalite
need your help on this error, I need to insert the value of an array into a row in MySQL except the primary key which is set to auto increment. however I encounter this error, below is my db structure and my code to insert:
需要您对此错误的帮助,我需要将数组的值插入到 MySQL 中的一行中,除了设置为自动递增的主键。但是我遇到了这个错误,下面是我的数据库结构和我要插入的代码:
CREATE TABLE `predicted`
(`StudentID` int(10) NOT NULL AUTO_INCREMENT,`Calc1` varchar(50) NOT NULL,
`ProgConcept` varchar(50) NOT NULL,
`English1` varchar(50) NOT NULL,
`Physics1` varchar(50) NOT NULL,
`IntrotoIT` varchar(50) NOT NULL,
PRIMARY KEY (`StudentID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2147483648 ;
and the code to insert was:
要插入的代码是:
/**
*
* @author fobia
*/
import java.sql.*;
import java.text.*;
public class arraytodb {
public static void sendtodb(String[]toarray, int lengthofarray){
String db="";
System.out.println("Length of array: "+lengthofarray);
for(int i=0;i<toarray.length;i++){
if(i==0){
db="'"+toarray[i]+"'";
}else{
db=db+"'"+toarray[i]+"'";
}
System.out.println(">>"+toarray[i]+"\t");
}
int counter=1;
for(int a=1;a<Integer.MAX_VALUE;a++){
counter++;
}
//==============================================================================
try{
Connection con = null;
String url = "jdbc:mysql://localhost:3306/thesis";
// String db = "";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
con = (Connection) DriverManager.getConnection(url,"root","");
try{
java.sql.Statement st = con.createStatement();
//int val = st.executeUpdate("INSERT dataset VALUES('019','"+db+")");
int val = st.executeUpdate("INSERT INTO predicted (Calc1, ProgConcept, English1, Physics1, IntrotoIT) VALUES ('"+toarray[0]+"','"+toarray[1]+"' ,'"+toarray[2]+"', '"+toarray[3]+"', '"+toarray[4]+"')");
System.out.println("1 row affected");
}
catch (SQLException s){
s.printStackTrace();
System.out.println("failed");
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
the input for StudentID is has to be auto increment from one, and in this way I could not get it and always encounter error
StudentID 的输入必须从 1 自动递增,这样我就无法获取它并且总是遇到错误
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value for column 'StudentID' at row 1
com.mysql.jdbc.MysqlDataTruncation:数据截断:第 1 行“StudentID”列的值超出范围
thank you very much.
非常感谢您。
回答by hoppa
You shouldn't try to insert an ID while creating. That is what the autoincrement column does for you ;)
您不应在创建时尝试插入 ID。这就是自动增量列为您所做的;)
Apart from that you are trying to insert a value that is larger than the maximum size of the column http://dev.mysql.com/doc/refman/5.5/en/out-of-range-and-overflow.html
除此之外,您试图插入一个大于列最大大小的值http://dev.mysql.com/doc/refman/5.5/en/out-of-range-and-overflow.html
回答by kandarp
You specify AUTO_INCREMENT=2147483648. Please remove this declaration, it will solve your issue.
您指定 AUTO_INCREMENT=2147483648。请删除此声明,它将解决您的问题。
回答by Anil Mathew
Either you are creating a table with a predefined auto increment field, or you seem to have reached the max range for the auto increment field.
您正在创建具有预定义自动增量字段的表,或者您似乎已达到自动增量字段的最大范围。
Use the following command to reset the auto increment value back to 1: ALTER TABLE predicted AUTO_INCREMENT = 1;
使用以下命令将自动增量值重置回 1: ALTER TABLE Predicted AUTO_INCREMENT = 1;
While resetting, ensure that the existing data (if any) are backed up and the table truncated. Else a primary key violation will be generated as there could be existing data with the same key.
重置时,请确保备份现有数据(如果有)并截断表。否则将生成主键违规,因为可能存在具有相同键的现有数据。
回答by Gautam
You must have to increase the size of the primary key value. On run time you are entering the value is not under the limitations of INT(10)
.
您必须增加主键值的大小。在运行时您输入的值不受INT(10)
.
You must increase the size of primary field that is intin your case.
在您的情况下,您必须增加int的主字段的大小。