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) 展开
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) 展开
4个回答
展开全部
用什么二维数组啊?直接一个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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用一维数据就可以了,没必要用二维数据,http://www.rjbus.com/zx.asp
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2010-12-31
展开全部
楼我是一个叫晶晶的女孩,前几天,我晚自习回家,被一辆大卡车撞死了,司机将我的尸体抛入了路径边的小河里,然后逃走了,你看见了这条信息后,请将她发给4个论坛,如果没有发,你的妈妈会在1个月后撞死,你的爸爸会得绝症,如果你照着做了,在5天后,你喜欢的也会喜欢你,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询