我想问问如何将多维数组存储到MySQL数据库。 例如我具有(X,Y)的阵列如何在java中将多维数组值存储到MySQL数据库中?
array[][] = {
{3, 10},
{2, 11},
{4, 9},
}
我试图使用preparedStatement时SETINT存储这些值来MySQL数据库但输出原来是这样的:
java.lang.ArrayIndexOutOfBoundsException: 3
这是代码:
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class InsertDB {
public static void main(String[] args) {
Connection conn = null;
Statement exe = null;
int[][] multiarray = {
{3, 10},
{2, 11},
{4, 9},
};
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to the database");
conn = DriverManager.getConnection("jdbc:mysql://localhost/finalproject","root","");
System.out.println("Database connected");
exe = conn.createStatement();
conn.setAutoCommit(false);
PreparedStatement stmt = null;
stmt = conn.prepareStatement("INSERT INTO SIGNATURE (angle, distance) VALUES (?,?)");
for (int i = 0; i < 10; i++) {
stmt.setInt(1, multiarray[i][0]);
stmt.setInt(2, multiarray[i][1]);
stmt.addBatch();
}
stmt.executeBatch();
conn.commit();
System.out.println("Data added ! ");
} catch (ClassNotFoundException ex) {
Logger.getLogger(InsertDB.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(InsertDB.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try{
if (exe != null)
conn.close();
}catch (SQLException se){
}try{
if(conn != null)
conn.close();
}catch(SQLException se){
}
}
System.out.println("Done !");
}
}
有人能解决这个问题,
谢谢:)
'有人可以解决这个问题'不除非你显示的代码失败... – marekful
**阵列指数出界:** *** 3 *** – nullpointer
你可以请分享你的代码? –