-3

I've a stupid bracket/parenthesis error that I cannot get right; I am a rookie programmer, Please tell me what punctuation I'm missing;

I am trying to make a Swing GUI to INSERT a simple statement to MySQL based on JComboBox and JTextArea selections.

If I had enough reputation points I would be able to screen shot; Here is the updated code with better formatting and the error pointed out.

Thanks!

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();
                }
            }
        });
    }
}
Swaylay
  • 19
  • 1
  • 8

3 Answers3

0

One error.

Function is getText() missed parenthesis

insertStmt.executeUpdate("INSERT INTO submit (submit_request,loc_string) VALUES ('"+request.getText()+"','"+schools.getSelectedItem() +"')");
//                                                                                                 ^^                      ^^             
sujithvm
  • 2,351
  • 3
  • 15
  • 16
  • IMO, you should also point out that queries built this way are vulnerable to SQL injections. OP should either use Prepared Statements or escape parameters before inserting them in the query instead – BackSlash Aug 17 '14 at 11:11
  • I'll research SQL injections as I've no idea what they are as well as prepared statements. Thanks – Swaylay Aug 17 '14 at 11:28
0

IMHO, You have missed couple closing braces. I think you have to pay more attention to it. I'm learning Java now, so not really sure how it should looks like but I think this is a bit better:

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();
            }
        }
    }
};
Mike
  • 1,979
  • 2
  • 16
  • 29
  • 1
    What's wrong with `String[] args`? It's perfectly fine. Actually, it's recommended to use the square brackets that way instead of the one you provided. – BackSlash Aug 17 '14 at 11:19
  • @BackSlash Yeah, my bad. I've just found an article here at stackoverflow http://stackoverflow.com/questions/301563/difference-fnstring-args-vs-fnstring-args you are totally right, as I said I'm entry level, but I will take note of this. Thank you :) – Mike Aug 17 '14 at 11:25
  • The String[] args was the Eclipse generated default. Thank you for cleaning up the code. I've been in a frenzy of placing curly brackets, parenthesis, and semicolons trying to crapshoot the problem. – Swaylay Aug 17 '14 at 11:27
  • And the word is "brace", not "bracer". – laune Aug 17 '14 at 11:29
0

The thing is you are getting the syntax error because you have lot of syntax error

  1. replace getSelectedText with getSelectedText()
  2. You missed the braces for this public void actionPerformed(ActionEvent e).You are having a opening brace for this but no closing brace

Pitfalls but not syntax errors

  1. Why do you need the anonymous block for database boiler-plate codes. I mean why the database codes inside { }.
  2. Always use closing statements inside finally, ofcoures you can handle the exception generated while closing by having try-catch but first put the closing statement inside finally.
  3. Most important of all make a habit of using PreparedStatement. Refer the advantages of PreparedStatement
Community
  • 1
  • 1
SparkOn
  • 8,806
  • 4
  • 29
  • 34