需要您對此錯誤的幫助,我需要將數組的值插入MySQL中的一行,但主鍵設置爲自動遞增。然而,我遇到這個錯誤,下面是我的數據庫結構和我的代碼中插入:遇到錯誤:數據截斷:行1列'StudentID'的範圍值超出
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 ;
,並插入代碼爲:
/**
*
* @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();
}
}
}
爲StudentID的輸入必須是自動增量從一個,並以這種方式我無法得到它,總會遇到錯誤
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Out of range value for column 'StudentID' at row 1
非常感謝你。
如果直接在MySQL中運行插入查詢會發生什麼?該行有哪些id? – 2012-03-15 10:45:24