我的标点符号出错了什么?

时间:2014-08-17 11:01:48

标签: java mysql swing jdbc

我有一个愚蠢的括号/括号错误,我无法理解; 我是新手程序员,请告诉我我缺少的标点符号;

我正在尝试根据Swing GUIINSERT选项向MySQL做一个JComboBoxJTextArea的简单陈述。

如果我有足够的声望点,我就可以进行屏幕截图; 这是更新的代码,具有更好的格式和指出的错误。

谢谢!

public class Submit extends JFrame {

DateFormat df = new SimpleDateFormat ("MM/dd/yyyy HH:mm");
Date today = Calendar.getInstance().getTime();
String reqDate = df.format(today);
String[] locations = {"Select School","Union Hills","40th St and Cactus","Chandler","Arrowhead","El Mirage","Surprise","Cactus 1","Cactus 3"};
public JPanel contentPane;





public Submit() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    setTitle("Maintenance Request");
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JComboBox schools = new JComboBox(locations);
    schools.setBounds(10, 11, 150, 20);
    contentPane.add(schools);

    JLabel dater = new JLabel(reqDate);
    dater.setBounds(304, 14, 120, 14);
    contentPane.add(dater);

    JTextArea request = new JTextArea("Type Maintenance Request Here");
    request.setLineWrap(true);
    request.setBounds(10, 70, 414, 123);
    contentPane.add(request);

    JButton submitter = new JButton("Submit Request");
    submitter.setBounds(145, 204, 144, 23);
    contentPane.add(submitter);

    submitter.addActionListener(new ActionListener(){


        public void actionPerformed(ActionEvent e) {

            Connection connection = null;
            Statement insertStmt = null;


    {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/maintenance","root","password");
        insertStmt = connection.createStatement();
        insertStmt.executeUpdate("INSERT INTO submit (submit_request,loc_string) VALUES ('"+request.getText()+"','"+schools.getSelectedItem+"')");
    }



            }); // ERROR here "Syntax error, insert "}" to complete ClassBody"

        try
        {
                insertStmt.close();
                connection.close();
        }
        catch (Exception e)         
        {
                e.printStackTrace();

        }   


}






    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Submit frame = new Submit();
                frame.setVisible(true);
                        } catch (Exception e) {
                        e.printStackTrace();
                }
            }
        });
    }
}

3 个答案:

答案 0 :(得分:0)

一个错误。

功能是getText()错过括号

insertStmt.executeUpdate("INSERT INTO submit (submit_request,loc_string) VALUES ('"+request.getText()+"','"+schools.getSelectedItem() +"')");
//                                                                                                 ^^                      ^^             

答案 1 :(得分:0)

恕我直言,你已经错过了几个关闭括号。我认为你必须更加关注它。我现在正在学习Java,所以不确定它应该是什么样子但是我觉得这样会好一些:

JButton submitter = new JButton("Submit Request");
submitter.setBounds(145, 204, 144, 23);
contentPane.add(submitter);
submitter.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
        Connection connection = null;
        Statement insertStmt = null;

        {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/maintenance","root","password");
            insertStmt = connection.createStatement();
            insertStmt.executeUpdate("INSERT INTO submit (submit_request,loc_string) VALUES ('" + request.getText() + "','" + schools.getText() + "')");
        }
    }
});

try
    {
        insertStmt.close();
        connection.close();
    } 
catch (Exception e)
    {
        e.printStackTrace();
    }


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Submit frame = new Submit();
                frame.setVisible(true);
            } catch (Exception e) {
            e.printStackTrace();
            }
        }
    }
};

答案 2 :(得分:0)

问题是你得到语法错误,因为你有很多语法错误

  1. 用getSelectedText()
  2. 替换getSelectedText
  3. 你错过了这个public void actionPerformed(ActionEvent e)的大括号。你有一个左大括号,但没有大括号
  4. 陷阱但不是语法错误

    1. 为什么需要数据库样板代码的匿名块。我的意思是为什么数据库代码在{}。
    2. 始终在finally内使用结束语句,因为你可以通过try-catch处理关闭时产生的异常,但最后将结束语句放在最后。
    3. 最重要的是养成使用PreparedStatement的习惯。 Refer the advantages of PreparedStatement