我有一个愚蠢的括号/括号错误,我无法理解; 我是新手程序员,请告诉我我缺少的标点符号;
我正在尝试根据Swing GUI
和INSERT
选项向MySQL
做一个JComboBox
到JTextArea
的简单陈述。
如果我有足够的声望点,我就可以进行屏幕截图; 这是更新的代码,具有更好的格式和指出的错误。
谢谢!
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();
}
}
});
}
}
答案 0 :(得分:0)
一个错误。
功能是getText()
错过括号
insertStmt.executeUpdate("INSERT INTO submit (submit_request,loc_string) VALUES ('"+request.getText()+"','"+schools.getSelectedItem() +"')");
// ^^ ^^
答案 1 :(得分:0)
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)
问题是你得到语法错误,因为你有很多语法错误
public void actionPerformed(ActionEvent e)
的大括号。你有一个左大括号,但没有大括号陷阱但不是语法错误
finally
内使用结束语句,因为你可以通过try-catch处理关闭时产生的异常,但最后将结束语句放在最后。PreparedStatement
的习惯。 Refer the advantages of PreparedStatement