这里不用try怎么捕获读取数据库的异常呢?

解决方案 »

  1.   

    像下面这么写显示是true或false,而不是钩package fee7;import java.awt.*;
    import javax.swing.JFrame;
    import com.borland.jbcl.layout.*;
    import java.awt.event.*;
    import opendb.opendb;
    import java.sql.*;
    import javax.swing.*;public class importData extends JFrame {
      private XYLayout xYLayout1 = new XYLayout();
      private Button button2 = new Button();
      private Button button1 = new Button();
      private Button button3 = new Button();
      private JTable jTable1 = new JTable();
      private JScrollPane jScrollPane1 = new JScrollPane(jTable1);  public importData(final JFrame mainFrame) {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }    mainFrame.setEnabled(false);
        button2.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            button2_actionPerformed(e);
          }
        void button2_actionPerformed(ActionEvent e) {
          mainFrame.setEnabled(true);
          importData.this.dispose();
        }});
      }
      private void jbInit() throws Exception {
        this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        this.setResizable(false);
        this.setTitle("导入数据");
        this.setSize(new Dimension(600, 400));
        java.awt.Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((d.width - this.getSize().width) / 2,
                           (d.height - this.getSize().height) / 2 );
          // the above is sure for middle showing
        button1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            button1_actionPerformed(e);
          }
        });
        button1.setLabel("导入");
        button1.setFont(new java.awt.Font("Dialog", 0, 14));
        button2.setFont(new java.awt.Font("Dialog", 0, 14));
        button2.setLabel("取消");    this.getContentPane().setLayout(xYLayout1);
        button3.setFont(new java.awt.Font("Dialog", 0, 14));
        button3.setLabel("显示未导数据");
        button3.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            button3_actionPerformed(e);
          }
        });
        this.getContentPane().add(button1, new XYConstraints(185, 315, 76, 32));
        this.getContentPane().add(button2,  new XYConstraints(338, 316, 76, 32));
        this.getContentPane().add(button3, new XYConstraints(242, 28, 122, 32));
      }
      //***********page变量**********
      opendb imData=new opendb();
      Object[][] data=new Object[20][8];
      //****************************
      void button1_actionPerformed(ActionEvent e) {  }
      void button3_actionPerformed(ActionEvent e) {
        String sqlblock="select * from block where blockname!='null'";
             try
              {
                ResultSet rs;
                rs=imData.dbstatement.executeQuery(sqlblock);
             }catch(Exception error){error.printStackTrace();}            //************显示表格**************
                       try
                         {
                       String block="select blockname,buildingNumber,buildingNumberReal,wholeArea,wholeAreaReal,wholePrice,wholePriceReal from block where blockname!='null'";
                       ResultSet rs1=imData.dbstatement.executeQuery(block);
                       String[] columnNames={"小区名称","应收楼数","实收楼数","应收总面积","实收总面积","应收总金额","实收总金额","状态"};
                       rs1.last();
                       int a=rs1.getRow();
                       data = new Object[a][8];
                       rs1.beforeFirst();
                       int i=0;
                       while(rs1.next())
                              {
                                      for(int col=1; col<=7; col++)//取列值
                                              data[i][col-1] = rs1.getString(col);
                                      data[i][7]=new Boolean(false);
                                      i++;
                                      data[0][7].getClass();
                              }
                       jTable1=new JTable(data,columnNames);
                       jTable1.setEnabled(false);
                       jTable1.getTableHeader( ).setReorderingAllowed( false );
                       this.getContentPane().add(jScrollPane1,  new XYConstraints(47, 88, 500, 200));
                       jScrollPane1.getViewport().add(jTable1, null);
                       this.show();
                       rs1.close();
                       }catch(Exception ar){ar.printStackTrace();}
                //*******************************
      }
      public Object getValueAt(int row, int col) {
           return data[row][col];
       }  public Class getColumnClass(int c) {
           return getValueAt(0, c).getClass();
      }}