无法在鼠标事件上执行查询

时间:2015-08-19 10:38:11

标签: java mysql swing nullpointerexception

我有以下代码,

 private void trans_tabMouseClicked(java.awt.event.MouseEvent evt) {                                       
        try{

         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/online_store","poornima","mit1234");
          if(con != null){

                String query = "SELECT * FROM bill"; 

                rs = stmt.executeQuery(query);
                System.out.println("fffgg");
                ResultSetMetaData rsmt = rs.getMetaData();
                int c = rsmt.getColumnCount(); 

                Vector row = new Vector();
                DefaultTableModel model = (DefaultTableModel)expense_table.getModel();
                while(rs.next())
                { 
                    row = new Vector(c); 
                    for(int i = 1; i <= c; i++)
                    { 
                        row.add(rs.getString(i));

                    }
                 model.addRow( row );
                }

          }

}catch(Exception ex){
     System.out.println(ex);
}
    }  

在上面的代码查询中没有执行。在null pointer exception行触发rs = stmt.executeQuery(query);。我试了几个小时,但我无法弄清楚问题出在哪里。请帮忙。

1 个答案:

答案 0 :(得分:2)

因为stmt为空。

使用此

String query = "SELECT * FROM bill"; 
stmt = con.createStatement();
rs = stmt.executeQuery(query);

而不是

String query = "SELECT * FROM bill"; 
rs = stmt.executeQuery(query);

参考this