java编程:用object数组从mysql数据库中读取数据,data11[i][0] = rs.getInt(1);报错了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java编程:用object数组从mysql数据库中读取数据,data11[i][0] = rs.getInt(1);报错了相关的知识,希望对你有一定的参考价值。
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)
这个rsnum是否取到了值,是多少,还有rs.next的时候赋值超没超过数组界值,错误发生在你这个文件的第41行,走个断点看看吧 参考技术A 用什么二维数组啊?直接一个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;
参考技术B 用一维数据就可以了,没必要用二维数据,http://www.rjbus.com/zx.asp 参考技术C 楼我是一个叫晶晶的女孩,前几天,我晚自习回家,被一辆大卡车撞死了,司机将我的尸体抛入了路径边的小河里,然后逃走了,你看见了这条信息后,请将她发给4个论坛,如果没有发,你的妈妈会在1个月后撞死,你的爸爸会得绝症,如果你照着做了,在5天后,你喜欢的也会喜欢你,
Java-数组
Java-数组
1、什么是数组?
数组是存储同一类型的数据的集合。
②引用数据类型(所有用class定义类都是引用类型)
如果数组的类型是Object,则所有数据类型的数据都可以存储。(Object是Java中所有类的父类。)
2、数组的特点:
③是一种线性的连续存储空间的数据结构
⑧但是有从Object父类继承过来的属性和方法。
获取或者设置数组的某一个位置的值:数组名[下标]
数组的length是什么时候开始计数的?从1开始
数组的下标是从什么时候开始计数的?从0开始
获取二维数组中某一行有多少列:数组名[行下标].length
获取二维数组中能存储的元素总数:
new 数据类型[][]{{值,...},...}:数组名[行下标].length+...
new 数据类型[行][列]:数组名.length*数组名[行下标].length
获取或者设置数组的某一个位置的值:数组名[行下标][列下标]
public class Students { private String name; private int credit; private int age; public Students(String name,int credit,int age){ this.name = name; this.credit = credit; this.age =age; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setCredit(int credit){ this.credit = credit; } public int getCredit(){ return credit; } public void setaAge(int age){ this.age = age; } public int getAge(){ return age; } public void show(){ System.out.println("Name "+ name+" Credit " + credit+ " Age "+ age+" "); } }
然后在主方法里面建立一个学生数组,并且通过循环创建学生对象并且往里面输入信息
public static void main(String[] args){ String name; int age; int credit; System.out.println("请输入学生个数"); Scanner sc = new Scanner(System.in); int size = sc.nextInt(); Students[] stuarray = new Students[size]; for(int i = 0; i < stuarray.length;i++){ stuarray[i]=new Students(); System.out.println("请输入学生姓名"); name = sc.next(); stuarray[i].setName(name); // System.out.println(stuarray[i].getName()); System.out.println("请输入学生学分"); credit = sc.nextInt(); stuarray[i].setCredit(credit); System.out.println("请输入学生年龄"); age = sc.nextInt(); stuarray[i].setaAge(age); }
最后通过三大排序方法(选择排序、冒泡排序、插入排序)分别对其姓名、学分、年龄排序
public void sort1(Students[] stus){ for(int i = 0; i < stus.length - 1;i++) { int min_index = i; for(int j = i + 1; j < stus.length;j++){ if((stus[i].getName()).compareTo((stus[j].getName())) >0 ){ min_index = j; } } //这个地方能够成功转换吗?能! if(min_index != i){ Students temp = stus[min_index]; stus[min_index] = stus[i]; stus[i] = temp; } } } public void sort2(Students[] stus){ boolean a = true; while(a){ a = false; for(int i = 0; i < stus.length - 1;i++){ if(stus[i].getCredit() > stus[i+1].getCredit()){ Students temp = stus[i]; stus[i] = stus[i+1]; stus[i+1] = temp; a = true; } } } } public void sort3(Students[] stus){ for(int i = 1; i < stus.length;i++){ Students temp = stus[i]; int j = i - 1; for(;j >=0 && stus[j].getAge() > stus[i].getAge();j--){ stus[j+1] = stus[j]; } stus[j+1] = temp; } }
SortOfinformation a = new SortOfinformation(); a.sort1(stuarray); System.out.println("名字排序以后的输出"); a.showName(stuarray); a.sort2(stuarray); System.out.println("学分排序后输出"); a.showCredit(stuarray); a.sort3(stuarray); System.out.println("年龄排序后输出"); a.showAge(stuarray);
这样就完成了这个小练习了!
以上是关于java编程:用object数组从mysql数据库中读取数据,data11[i][0] = rs.getInt(1);报错了的主要内容,如果未能解决你的问题,请参考以下文章