public class MyStorageSystem extends JFrame 
{public class RightPanel extends JPanel 
{

public RightPanel() 
{         
    JPanel downPanel = new JPanel();
    JLabel lable0 = new JLabel();
    JLabel lable1 = new JLabel();
    JLabel lable2 = new JLabel();
   
lable0.setPreferredSize(new Dimension(50, 22));
lable0.setText("学生ID:");

lable1.setPreferredSize(new Dimension(60, 20));
lable1.setText("学生姓名:");

lable2.setPreferredSize(new Dimension(60, 40));
lable2.setText("学生年龄:");

JTextField textID0 = new JTextField();
JTextField textID1 = new JTextField();
JTextField textID2 = new JTextField();

textID0.setPreferredSize(new Dimension(140, 22));
textID1.setPreferredSize(new Dimension(140, 22));
textID2.setPreferredSize(new Dimension(140, 22));
JButton bu = new JButton("确定");

bu.addActionListener(new ActionListener() 

public void actionPerformed(ActionEvent e) 

 SelectDB();//请问这里怎么做才能调用SelectDB() ??

}); 



downPanel.add(lable0);
downPanel.add(textID0);
downPanel.add(lable1);
downPanel.add(textID1);
downPanel.add(lable2);
downPanel.add(textID2);
downPanel.add(bu);
   
   FlowLayout flowLayout =  new FlowLayout(); 
       flowLayout.setAlignment(FlowLayout.LEFT);
   downPanel.setLayout(flowLayout);
   this.add(downPanel,BorderLayout.NORTH);
  
this.setBorder(new EtchedBorder(EtchedBorder.RAISED,Color.LIGHT_GRAY,Color.red));
      
} }
public void  SelectDB()
{
             try{
                        getConnection();
                    
                        Statement stmt  = this.conn.createStatement();
                        String SQL = "select stu_id ,stu_name ,stu_age ,stu_sex,stu_date,stu_add  from student";
                        ResultSet rs = stmt.executeQuery(SQL);
                        
                        Collection col = new ArrayList(); 
                        
                        ResultSetMetaData ss = rs.getMetaData();        
                        int count = ss.getColumnCount();
                        MyTable tmp_table = new MyTable();
                        int j = 0;
                        while(rs.next())
                        {        

                                for (int i = 1; i <= count;i++)
                                {
                                 tmp_table.setValueAt(rs.getString(i),j,i);
                                        System.out.print(rs.getString(i)+" ");              
                                }
                                j++;
                                System.out.println(" ");
                        }
                        
                        rs.close();
                        closeConnection();                       
                 }
                catch(Exception e)
                {
                        e.printStackTrace();
         }
      }
}

解决方案 »

  1.   

    final  public void  SelectDB()
    {
       ...
    }
      

  2.   

    一般来说你只要new SelectDB()就可以了
    给SelectDB()写一个构造,把try{}放进去
      

  3.   

    mq612(五斗米)
    -------------
    SelectDB()虽说第一个字母大写了,但还是一个方法,必不是类.
      

  4.   

    呵呵,上面代码明显是匿名内部类调用外部类方法啊,应该把该方法声明为 final 就可以了!
      

  5.   

    MyStorageSystem m=new MyStorageSystem ();
    m.SelectDB();可以调用
      

  6.   

    new SelectDB() 这样不行的,找不到
      

  7.   

    MyStorageSystem m=new MyStorageSystem ();
    m.SelectDB();可以调用
    这样调用的话 就出来两个窗口了~~这样不好~~
      

  8.   

    将SelectDB();声名为静态方法static,通过外部类的类名直接调用
      

  9.   

    再请教大家一个问题:
    这样为什么不能向表格中赋值,没有显示,还是本省就错了
    class MyTable extends AbstractTableModel 
    {

        Object[][] p = {};

        String[] n = {"ID","姓名","年龄","性别","出生日期","家庭地址"};

        public int getColumnCount() {
            return n.length;
        }

        public int getRowCount() {
            return p.length;
        }

        public String getColumnName(int col) {
            return n[col];
        }

        public Object getValueAt(int row, int col) {
            return p[row][col];
        }
        
            public Class getColumnClass(int c) {
                    return getValueAt(0, c).getClass();
        }
            
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return true;        
        }
        
            public void setValueAt(Object value, int row, int col) {
                 
        }
        
        public void mySetValueAt(Object value, int row, int col) {
                p[row][col] = value;
        }
    }
    public void  SelectDB()
        {
                 try{
                            getConnection();
                        
                            Statement stmt  = conn.createStatement();
                            String SQL = "select stu_id ,stu_name ,stu_age ,stu_sex,stu_date,stu_add  from student";
                            ResultSet rs = stmt.executeQuery(SQL);
                            
                            Collection col = new ArrayList(); 
                            
                            ResultSetMetaData ss = rs.getMetaData();        
                            int count = ss.getColumnCount();
                            MyTable tmp_table = new MyTable();
                            int j = 0;
                            while(rs.next())
                            {                                         for (int i = 1; i <= count;i++)
                                    {
                                     String ss1 =rs.getString(i);
                                         ss1 = new String(ss1.getBytes("ISO8859_1"),"GBK");
                                     tmp_table.setValueAt(ss1,j,i);
                                     System.out.print(ss1+" "); 
                                                      
                                    }
                                    j++;
                                    System.out.println(" ");
                            }
                             tmp_table.fireTableStructureChanged();
                            rs.close();
                            closeConnection();                       
                     }
                    catch(Exception e)
                    {
                            e.printStackTrace();
             }
           }