java编程:用object数组从mysql数据库中读取数据,data11[i][0] = rs.getInt(1);报错了

packagechuangti1;importjava.awt.Dimension;importjavax.swing.JFrame;importjavax.swing.... package chuangti1;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.Color;
import java.awt.GridLayout;
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.Statement ;
import java.sql.ResultSet ;

public class sp

{

public static void main (String[] args)throws Exception

{
String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
String DBURL = "jdbc:mysql://localhost:3306/wsgw" ;
String DBUSER = "root" ;
String DBPASS = "admin" ;
ResultSet rs = null ;
Connection conn = null ;
Statement stmt = null ;
String sql = "select spid,lid,spm,spj,spl,spt,spjj from sp;";
Object[][] data11;
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
stmt = conn.createStatement() ;
rs = stmt.executeQuery(sql) ;
int rsnum = rs.getRow();
int i=0;
data11=new Object[rsnum][7] ;
rs.beforeFirst(); //记录指针移到首记录之前
final Object[] columnName = {"商品序列号", "商品类序列号", "商品名",
"商品价格", "商品数量", "入库时间", "商品简介"};
while(rs.next()){
data11[i][0] = rs.getInt(1);
data11[i][1] = rs.getString(2);
data11[i][2] = String.valueOf(rs.getInt(3));
data11[i][3] = String.valueOf(rs.getInt(4));
data11[i][4] =String.valueOf( rs.getInt(5));
data11[i][5] = rs.getDate(6);
data11[i][6] = rs.getString(7);
i++;
}
rs.close() ;
stmt.close() ;
conn.close() ;
JTable friends = new JTable (data11, columnName);

friends.setPreferredScrollableViewportSize(new Dimension(600, 100));

friends.setRowHeight (30);

friends.setRowHeight (0, 20);

friends.setRowMargin (5);

friends.setRowSelectionAllowed (true);

friends.setSelectionBackground (Color.white);

friends.setSelectionForeground (Color.red);

friends.setGridColor (Color.black);

friends.selectAll ();

friends.setRowSelectionInterval (0,2);

friends.clearSelection ();

friends.setDragEnabled (false);

friends.setShowGrid (false);

friends.setShowHorizontalLines (false);

friends.setShowVerticalLines (true);

friends.setValueAt ("tt", 0, 0);

friends.doLayout ();

friends.setBackground (Color.lightGray);

JScrollPane pane3 = new JScrollPane (friends);

JPanel panel = new JPanel (new GridLayout (0, 1));

panel.setPreferredSize (new Dimension (600,400));

panel.setBackground (Color.black);

panel.add (pane3);

JFrame frame = new JFrame ("JTableDemo");

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

frame.setContentPane (panel);

frame.pack();

frame.show();

}
}
出错
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at chuangti1.sp.main(sp.java:41)
展开
 我来答
xhxyes111
2010-12-31 · TA获得超过2900个赞
知道小有建树答主
回答量:278
采纳率:100%
帮助的人:386万
展开全部
楼主这是数组越界啊~看看你那2维数组有没有边界值设的比较小,还有确定下 data11=new Object[rsnum][7] ;
这个rsnum是否取到了值,是多少,还有rs.next的时候赋值超没超过数组界值,错误发生在你这个文件的第41行,走个断点看看吧
crazylaa
2010-12-31 · TA获得超过234个赞
知道小有建树答主
回答量:287
采纳率:0%
帮助的人:295万
展开全部
用什么二维数组啊?直接一个ArrayList!给你参考:
public ArrayList<CallLogInfo> getCallLogInfo() {
ArrayList<CallLogInfo> cliList = new ArrayList<CallLogInfo>();
Log.d(tag, "getCallLogInfo()");
Cursor cliCursor = mContext.getContentResolver().query(
Calls.CONTENT_URI, null, null, null, Calls.DEFAULT_SORT_ORDER);
if (cliCursor != null) {
int dataCount = 0;
while (cliCursor.moveToNext()) {
CallLogInfo cli = new CallLogInfo();
dataCount++;
Log.e(tag + dataCount, "-----------------------------");

cli.setId(cliCursor.getString(cliCursor
.getColumnIndex(Calls._ID)));
cli.setCachedName(cliCursor.getString(cliCursor
.getColumnIndex(Calls.CACHED_NAME)));
cli.setCachedNumberLabel(cliCursor.getString(cliCursor
.getColumnIndex(Calls.CACHED_NUMBER_LABEL)));
cli.setCachedNumberType(cliCursor.getString(cliCursor
.getColumnIndex(Calls.CACHED_NUMBER_TYPE)));
cli.setDate(cliCursor.getString(cliCursor
.getColumnIndex(Calls.DATE)));
cli.setDuration(cliCursor.getString(cliCursor
.getColumnIndex(Calls.DURATION)));
cli.setIsNew(cliCursor.getString(cliCursor
.getColumnIndex(Calls.NEW)));
cli.setNumber(cliCursor.getString(cliCursor
.getColumnIndex(Calls.NUMBER)));
cli.setType(cliCursor.getString(cliCursor
.getColumnIndex(Calls.TYPE)));

Log.e("[CallLogList]", cli.toString());

cliList.add(cli);
}
cliCursor.close();
}
return cliList;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
rjbus
2010-12-31
知道答主
回答量:13
采纳率:0%
帮助的人:0
展开全部
用一维数据就可以了,没必要用二维数据,http://www.rjbus.com/zx.asp
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2010-12-31
展开全部
楼我是一个叫晶晶的女孩,前几天,我晚自习回家,被一辆大卡车撞死了,司机将我的尸体抛入了路径边的小河里,然后逃走了,你看见了这条信息后,请将她发给4个论坛,如果没有发,你的妈妈会在1个月后撞死,你的爸爸会得绝症,如果你照着做了,在5天后,你喜欢的也会喜欢你,
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式