当我在main函数中编写PreparedStatement时,它可以完美地工作并提交数据,但是当我在actionPerformed函数中编写时,它不起作用,为什么? 我认为我没有做错任何事情。
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class EnterData {
static void insertData(int rn, String fn, String ln) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo","root","12345")) {
String query = "insert into student (rollno, fname, lname)" + " values (?, ?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt (1, rn);
preparedStmt.setString (2, fn);
preparedStmt.setString (3, ln);
preparedStmt.execute();
}
}
public static void main(String[] args) {
JFrame mainFrame = new JFrame("Submit Information");
mainFrame.setSize(400, 300);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setLayout(null);
JLabel heading = new JLabel("Enter Data");
heading.setBounds(150, 10, 100, 20);
mainFrame.add(heading);
JLabel rollnoLabel = new JLabel("Roll Number");
rollnoLabel.setBounds(70, 60, 100, 20);
mainFrame.add(rollnoLabel);
JTextField rollno = new JTextField();
rollno.setBounds(220, 60, 100, 20);
mainFrame.add(rollno);
// Taking First Name
JLabel name1 = new JLabel("First Name");
name1.setBounds(70, 110, 100, 20);
mainFrame.add(name1);
JTextField fname = new JTextField();
fname.setBounds(220, 110, 100, 20);
mainFrame.add(fname);
// Taking Last Name
JLabel name2 = new JLabel("Last Name");
name2.setBounds(70, 160, 100, 20);
mainFrame.add(name2);
JTextField lname = new JTextField();
lname.setBounds(220, 160, 100, 20);
mainFrame.add(lname);
// Submit and Reset Button
JButton submit = new JButton("Submit");
submit.setBounds(70, 220, 100, 20);
mainFrame.add(submit);
JButton reset = new JButton("Reset");
reset.setBounds(220, 220, 100, 20);
mainFrame.add(reset);
// Frame Visibility
mainFrame.setVisible(true);
submit.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
try {
int rn = Integer.parseInt(rollno.getText());
String fn = fname.getText();
String ln = lname.getText();
insertData(rn, fn, ln);
JOptionPane.showMessageDialog(null, "Submitted Successfully...!!!", "Submission Prompt", JOptionPane.PLAIN_MESSAGE);
} catch (Exception ex) {
System.err.println("Got an exception!");
System.err.println(ex.getMessage());
}
}
});
}
}
我已经尝试过了,并且效果很好,但是当我将这些语句放入actionPerformed函数中时,它们将不起作用。
import java.sql.*;
public class DatabaseTest {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo","root","12345");
String query = "insert into student (rollno, fname, lname)" + " values (?, ?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt (1, 6505);
preparedStmt.setString (2, "Prince");
preparedStmt.setString (3, "Saini");
preparedStmt.execute();
conn.close();
} catch (Exception e) {
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
}
}