Java 从 mysql 数据库错误填充 jcombobox

Java populating jcombobox from mysql database error

我在尝试从 sql 数据库检索数据到 j 组合框时遇到错误:

“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax error”

public class NewJFrame extends javax.swing.JFrame {
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
/** Creates new form NewJFrame */
public NewJFrame() {
    initComponents();
    fillcombo();

}
private void fillcombo(){
try{
con=Connect.ConnectDB();
String sql="select * from leave";
pst=con.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next())
 {
     String nm=rs.getString("combo");
jComboBox1.addItem(nm);
 }
  }
catch(Exception e)
{ JOptionPane.showMessageDialog(null, e);
 }

 }

请不要从 ConnectionStatement 中创建字段。 LEAVE is a mysql reserved word (so a table named leave must be escaped). Also, please don't select splat if you just want one column. Finally, you should close your database resources (assuming you're using Java 7 or newer, you might use a try-with-resources 否则你需要一个 finally 块)类似

String sql = "SELECT combo FROM `leave`";
try (Connection con = Connect.ConnectDB();
        PreparedStatement pst = con.prepareStatement(sql);
        ResultSet rs = pst.executeQuery()) {
    if (rs.next()) {
        String nm = rs.getString("combo");
        jComboBox1.addItem(nm);
    }
} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}