我正在开发一个Java Swing
应用程序来select data
从database
。但我的Action Listener
行不通。请帮我。执行程序时没有显示错误。请检查我的代码。
public class Availability implements ActionListener {
JPanel panelForMedicine,inputPanel;
JLabel mediLabel;
JComboBox listCombo;
JButton dbButton;
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/paliative_care_unit";
final String USER = "root";
final String PASS = "root";
Connection conn = null;
Statement stmt=null;
static void guiBuilder(){
JFrame availabilityFrame=new JFrame("Check Stock");
availabilityFrame.setLocation(0, 0);
availabilityFrame.setSize(390,150);
availabilityFrame.setVisible(true);
availabilityFrame.setDefaultCloseOperation(availabilityFrame.DISPOSE_ON_CLOSE);
Availability availability=new Availability();
availabilityFrame.setContentPane(availability.mainPanel());
availability.mainPanel();
}
public JPanel mainPanel(){
JPanel availabilityPanel=new JPanel();
availabilityPanel.setLayout(null);
panelForMedicine=new JPanel();
//panelForMedicine.setLayout(null);
panelForMedicine.setSize(70, 40);
panelForMedicine.setLocation(30, 35);
availabilityPanel.add(panelForMedicine);
mediLabel=new JLabel("Medicine");
mediLabel.setLocation(0, 0);
mediLabel.setSize(70, 40);
panelForMedicine.add(mediLabel);
inputPanel=new JPanel();
inputPanel.setLayout(null);
inputPanel.setSize(150, 40);
inputPanel.setLocation(120,35);
availabilityPanel.add(inputPanel);
listCombo=new JComboBox();
listCombo.setSize(150, 30);
listCombo.setLocation(0, 0);
inputPanel.add(listCombo);
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql="SELECT DISTINCT medi FROM paliative";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
listCombo.addItem(rs.getString("medi"));
//medicineField.setSelectedItem("attempt11");
}
}
catch(Exception e){System.out.println(e);}
dbButton=new JButton("GO");
dbButton.setSize(70, 30);
dbButton.setLocation(300, 35);
dbButton.addActionListener(this);
availabilityPanel.add(dbButton);
availabilityPanel.setOpaque(true);
return availabilityPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==dbButton){
System.out.println("Checking");
try{
System.out.println("Checking");
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql="SELECT * FROM paliative WHERE medi='"+listCombo.getSelectedItem().toString()+"'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}
catch (Exception ex) {
JFrame dialogFrame=new JFrame();
JOptionPane.showMessageDialog(dialogFrame, ex);
}
}
}
}