我怎样才能得到变量" a"在try和catch之外使用它?

时间:2015-12-17 00:57:37

标签: java scope

我试图在下面的查询中检索总数,我该怎么做,因为我想把结果放在jTextField中,try / catch在执行的buton动作中

try {
  Connection conn = new Operaciones().conectarLogin();

  Statement st = conn.createStatement();
  String sql = "RETURN total;"+
                "CREATE OR REPLACE FUNCTION cantidad_productos()"+
                "RETURNS integer AS $total$"+
                "declare total integer;"+
                "BEGIN SELECT count( *) into total FROM producto; END;"+
                "$total$ LANGUAGE plpgsql;";

      ResultSet rs = st.executeQuery(sql); 
      int total = rs.getInt("total");
      String a = Integer.toString(total);
      jTextField1.setText(a);

}catch (SQLException e) {JOptionPane.showMessageDialog(null, "Excepción de SQL: " + e);}

0 个答案:

没有答案