我想問問如何將多維數組存儲到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
你可以請分享你的代碼? –