我的 JTable 有问题,我不知道如何将 String 类型的提取记录从我的数据库存储到多维数组中,比如 Object[][] 数据。我想要做的是向 JTable 显示我的数据库记录,我已经在数据库中获取记录并将其存储在我的 String 变量中,问题是如何将获取记录存储到 Object 的多维数组并在我的表。
这是我获取记录的代码:
static class TableData{
Object[][] data;
int count = 0;
Statement sql = null;
String query, user = "JEROME", pass = "Perbert101", driver = "oracle.jdbc.OracleDriver", conString = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
Connection con = null;
ResultSet rs = null;
TableData(){
try{
Class.forName(driver);
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "Problem Loading Driver");
}
try{
con = DriverManager.getConnection(conString, user, pass);
sql = con.createStatement();
sql.executeQuery("SELECT * FROM INVENTORY");
rs = sql.getResultSet();
int key = 0;
String val = null, val1 = null, val2 = null, val3 = null, val4 = null, val5 = null;
System.out.println("Results: ");
while(rs.next()){
key = rs.getInt(1);
if(rs.wasNull()){
key = -1;
}
val = rs.getString(2);
if(rs.wasNull()){
val = null;
}
val1 = rs.getString(3);
if(rs.wasNull()){
val = null;
}
val2 = rs.getString(4);
if(rs.wasNull()){
val = null;
}
val3 = rs.getString(5);
if(rs.wasNull()){
val = null;
}
val4 = rs.getString(6);
if(rs.wasNull()){
val = null;
}
val5 = rs.getString(7);
if(rs.wasNull()){
val = null;
}
System.out.println("Key = " + key);
System.out.println("value = " + val);
System.out.println("value = " + val1);
System.out.println("value = " + val2);
System.out.println("value = " + val3);
System.out.println("value = " + val4);
System.out.println("value = " + val5);
}
sql.close();
con.close();
}
catch(SQLException e){
JOptionPane.showMessageDialog(null, "Error Loading Database Data");
}
}
}
//----------END------------
public static void main(String[] args){
POSModel.TableData data = new POSModel.TableData();
}