java-mySQL图书管理系统出现这种错误: Can't call commit when autocommit=true
运行程序时,执行添加,修改,删除时出现同种错误:
java.sql.SQLException: Can't call commit when autocommit=truemySQL中表结构如下:
添加用户的代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class UserAdd
extends JFrame
implements ActionListener {
DataBaseManager db = new DataBaseManager();
ResultSet rs;
Container c;
JPanel panel1, panel2;
JLabel userLabel, passwordLabel, passwordConfirmLabel, loginPrivelegeLabel;
JTextField userTextField;
JPasswordField passwordTextField, passwordConfirmTextField;
JComboBox loginPrivelegeComboBox;
JButton addBtn, cancelBtn;
public UserAdd() {
super("添加用户");
c = getContentPane();
c.setLayout(new BorderLayout());
userLabel = new JLabel("用户名", JLabel.CENTER);
passwordLabel = new JLabel("密码", JLabel.CENTER);
passwordConfirmLabel = new JLabel("确认密码", JLabel.CENTER);
loginPrivelegeLabel = new JLabel("登录权限", JLabel.CENTER);
userTextField = new JTextField(10);
passwordTextField = new JPasswordField(10);
passwordConfirmTextField = new JPasswordField(10);
loginPrivelegeComboBox = new JComboBox();
loginPrivelegeComboBox.addItem("系统管理员");
loginPrivelegeComboBox.addItem("书籍管理员");
loginPrivelegeComboBox.addItem("借阅管理员");
loginPrivelegeComboBox.addItem("读者");
addBtn = new JButton("添加");
cancelBtn = new JButton("取消");
addBtn.addActionListener(this);
cancelBtn.addActionListener(this);
panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 2));
panel1.add(userLabel);
panel1.add(userTextField);
panel1.add(passwordLabel);
panel1.add(passwordTextField);
panel1.add(passwordConfirmLabel);
panel1.add(passwordConfirmTextField);
panel1.add(loginPrivelegeLabel);
panel1.add(loginPrivelegeComboBox);
c.add(panel1, BorderLayout.CENTER);
panel2 = new JPanel();
panel2.add(addBtn);
panel2.add(cancelBtn);
c.add(panel2, BorderLayout.SOUTH);
setSize(300, 300);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cancelBtn) {
db.closeConnection();
this.dispose();
}
else if (e.getSource() == addBtn) {
try {
String strSQL = "select * from userTable where userName='" +
userTextField.getText().trim() + "'";
if (userTextField.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
}
else if (new String(passwordTextField.getPassword()).trim().equals("")) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
}
else if (!new String(passwordTextField.getPassword()).trim().equals(
new String(passwordConfirmTextField.getPassword()).trim())) {
JOptionPane.showMessageDialog(null, "两次输入的密码不一致!");
}
else {
if (db.getResult(strSQL).first()) {
JOptionPane.showMessageDialog(null, "此用户已经存在,请重新输入用户名!");
}
else {
strSQL = "insert into userTable values('" +
userTextField.getText().trim() + "','" +
new String(passwordTextField.getPassword()).
trim() + "','" + loginPrivelegeComboBox.getSelectedItem() +
"')";
if (db.updateSql(strSQL)>0) {
this.dispose();
JOptionPane.showMessageDialog(null, "添加用户成功!");
}
else {
JOptionPane.showMessageDialog(null, "添加用户失败!");
}
}
}
}
catch (SQLException sqle) {
System.out.println(sqle.toString());
}
catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
}
请各位帮忙解决一下,谢谢!
------解决方案--------------------
Can't call commit when autocommit=true
意思是你已经设置自动提交事务了,但是你又手动提交事务了。所以冲突。
[解决办法]