Reputation: 43
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:
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
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value for column 'StudentID' at row 1
thank you very much.
Upvotes: 1
Views: 12699
Reputation: 11
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)
.
You must increase the size of primary field that is int in your case.
Upvotes: 1
Reputation: 2696
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;
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.
Upvotes: 0
Reputation: 5047
You specify AUTO_INCREMENT=2147483648. Please remove this declaration, it will solve your issue.
Upvotes: 0
Reputation: 3041
You shouldn't try to insert an ID while creating. That is what the autoincrement column does for you ;)
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
Upvotes: 0