日期:2014-05-20  浏览次数:20942 次

谁能帮我看下这个错误:在掉数据库数据时,才出错。
只有在触发事件的时候,才出错

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsExcept

图书表
CREATE TABLE [dbo].[BOOK](
[BNO] [int] NOT NULL,
[NAME] [char](20) NULL,
[TYPE] [char](10) NULL,
[ISBORROW] [char](2) NULL,
[RNO] [int] NULL,
 CONSTRAINT [PK_BOOK] PRIMARY KEY CLUSTERED 


Java code

package bookstore;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class bookDialog extends JDialog implements ActionListener{
    JLabel jLabel1;
    JButton jButton1;
    JTextField jTextField1;
    
    Box box1;
    JPanel jPanel1;
    
    JTable table;
    Object a[][];
    Object name[] = {"图书编号","书名","类别","是否借出","借阅人编号"};
    
    Connection con;
    Statement sql;
    ResultSet rs;
    
    bookDialog() {
        jLabel1 = new JLabel("图书编号(回车确认):");
        jTextField1 = new JTextField(10);
        jButton1 = new JButton("查询全部");
        a = new Object[1][5];
        table = new JTable(a,name);
        
        box1 = Box.createHorizontalBox();
        
        jPanel1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        
        box1.add(jLabel1);
        box1.add(Box.createHorizontalStrut(40));
        box1.add(jTextField1);
        box1.add(Box.createHorizontalStrut(40));
        box1.add(jButton1);
        
        jPanel1.add(box1);
        
        add(jPanel1,BorderLayout.SOUTH);
        add(new JScrollPane(table),BorderLayout.CENTER);
        
        jTextField1.addActionListener(this);
        jButton1.addActionListener(this);
        
        setBounds(200, 250, 435, 300);
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jTextField1) {
            try {
                String s = jTextField1.getText().trim();
                int num = Integer.parseInt(s);
                con = DriverManager.getConnection("jdbc:odbc:bookstore","","");
                sql = con.createStatement();
                rs  = sql.executeQuery("SELECT * FROM book WHERE bno = '"+num+"' ");
                if (!rs.next()) {
                    JOptionPane.showMessageDialog(this, "学号卜存在","提示",JOptionPane.WARNING_MESSAGE);
                }    
                else {
                   try {
                    a[0][0] = rs.getInt(1);
                    a[0][1] = rs.getString(2);
                    a[0][2] = rs.getString(3);
                    a[0][3] = rs.getString(4);
                    a[0][4] = rs.getInt(5);
                    table.repaint();
                   } 
                   catch (SQLException e2) {e2.printStackTrace();}
                   con.close();
                 }
            }
            catch (SQLException ee) {System.out.println(ee);}
            
            table = new JTable(a,name);
            getContentPane().removeAll();
            add(jPanel1,BorderLayout.SOUTH);
            add(new JScrollPane(table),BorderLayout.CENTER);
        }
        
        if (e.getSource() == jButton1) {
            try {
                con = DriverManager.getConnection("jdbc:odbc:bookstore","","");
                sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
                rs  = sql.executeQuery("SELECT * FROM book");
                int lastnum = rs.getRow();
                a= new Object[lastnum][5];
                int k = 0;
                rs.beforeFirst();
                while (rs.next()) {
                    try {
                        a[k][0] = rs.getInt(1);
                        a[k][1] = rs.getString(2);
                        a[k][2] = rs.getString(3);
                        a[k][3] = rs.getString(4);
                        a[k][4] = rs.getInt(5);        
                        k++;
                       } 
                       catch (SQLException e2) {e2.printStackTrace();}
                }
                con.close();
            } catch (SQLException ee) {
                System.out.println(ee);
            }
            
            table = new JTable(a,name);
            getContentPane().removeAll();
            add(jPanel1,BorderLayout.SOUTH);
            add(new JScrollPane(table),BorderLayout.CENTER);
        }
    }
     
}