为什么我只从数据库中获取最后一行?
Why does i get only the last row from the database?
我使用 Netbeans 8.2。我试图从数据库中获取数据并将其显示在 jTable 中。
但是,当我 运行 程序时,我只得到最后一行。
谁能帮我解决一下?
public class MultiSpanCellTableExample extends JFrame {
Statement st;
ResultSet rs;
Object [][]row;
Object[] column;
MultiSpanCellTableExample() {
column = new Object[]{"","","","",""};
String g="",h="",j="",z="",n="",hg="",oo="",zz="";
Double l=null;
Date i=null;
String sql="SELECT * from personn";
con=Connections.getConnection();
try{
st=con.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
g=rs.getString("NAMME");
l=rs.getDouble("TOTAL");
i=rs.getDate("DATEE");
z=rs.getString("WY");
n=rs.getString("BAA");
row=new Object[][]{{g,l,z,i,n}};
}
}
catch(Exception e){
System.out.print( e);
}
AttributiveCellTableModel ml = new AttributiveCellTableModel(row,column);
final CellSpan cellAtt =(CellSpan)ml.getCellAttribute();
final MultiSpanCellTable table = new MultiSpanCellTable(ml);
JScrollPane scroll = new JScrollPane( table );
Box box = new Box(BoxLayout.X_AXIS);
box.add(scroll);
box.add(new JSeparator(SwingConstants.HORIZONTAL));
box.add(p_buttons);
getContentPane().add( box );
setSize( 400, 200 );
setVisible(true);
}
public static void main(String[] args) {
MultiSpanCellTableExample frame = new MultiSpanCellTableExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}
问题
您尚未使用所需的行数初始化 row[][]
。此外,您没有将记录存储到 row[i]
中,其中 i
是索引。请注意,二维数组是数组的数组,因此数据库中的每条记录都代表一个需要存储到 row[i]
.
中的一维数组
解决方案
try{
// Get the number of rows
Statment stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rsCount = stmt.executeQuery("SELECT count(*) from personn");
if(rsCount.next()) {
// Initialize row[][]
row = new Object[rsCount.getInt(1)][];
st = con.createStatement();
rs = st.executeQuery(sql);
for(int i = 0; rs.next(); i++) {
g = rs.getString("NAMME");
l = rs.getDouble("TOTAL");
i = rs.getDate("DATEE");
z = rs.getString("WY");
n = rs.getString("BAA");
// Store the record into row[i]
row[i] = new Object[]{g,l,z,i,n};
}
}
}
我使用 Netbeans 8.2。我试图从数据库中获取数据并将其显示在 jTable 中。 但是,当我 运行 程序时,我只得到最后一行。
谁能帮我解决一下?
public class MultiSpanCellTableExample extends JFrame {
Statement st;
ResultSet rs;
Object [][]row;
Object[] column;
MultiSpanCellTableExample() {
column = new Object[]{"","","","",""};
String g="",h="",j="",z="",n="",hg="",oo="",zz="";
Double l=null;
Date i=null;
String sql="SELECT * from personn";
con=Connections.getConnection();
try{
st=con.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
g=rs.getString("NAMME");
l=rs.getDouble("TOTAL");
i=rs.getDate("DATEE");
z=rs.getString("WY");
n=rs.getString("BAA");
row=new Object[][]{{g,l,z,i,n}};
}
}
catch(Exception e){
System.out.print( e);
}
AttributiveCellTableModel ml = new AttributiveCellTableModel(row,column);
final CellSpan cellAtt =(CellSpan)ml.getCellAttribute();
final MultiSpanCellTable table = new MultiSpanCellTable(ml);
JScrollPane scroll = new JScrollPane( table );
Box box = new Box(BoxLayout.X_AXIS);
box.add(scroll);
box.add(new JSeparator(SwingConstants.HORIZONTAL));
box.add(p_buttons);
getContentPane().add( box );
setSize( 400, 200 );
setVisible(true);
}
public static void main(String[] args) {
MultiSpanCellTableExample frame = new MultiSpanCellTableExample();
frame.addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent e ) {
System.exit(0);
}
});
}
}
问题
您尚未使用所需的行数初始化 row[][]
。此外,您没有将记录存储到 row[i]
中,其中 i
是索引。请注意,二维数组是数组的数组,因此数据库中的每条记录都代表一个需要存储到 row[i]
.
解决方案
try{
// Get the number of rows
Statment stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rsCount = stmt.executeQuery("SELECT count(*) from personn");
if(rsCount.next()) {
// Initialize row[][]
row = new Object[rsCount.getInt(1)][];
st = con.createStatement();
rs = st.executeQuery(sql);
for(int i = 0; rs.next(); i++) {
g = rs.getString("NAMME");
l = rs.getDouble("TOTAL");
i = rs.getDate("DATEE");
z = rs.getString("WY");
n = rs.getString("BAA");
// Store the record into row[i]
row[i] = new Object[]{g,l,z,i,n};
}
}
}