首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

java-mySQL图书管理系统出现这种异常: Can't call commit when autocommit=true

2012-12-25 
java-mySQL图书管理系统出现这种错误: Can't call commit when autocommittrue运行程序时,执行添加,

java-mySQL图书管理系统出现这种错误: Can't call commit when autocommit=true
运行程序时,执行添加,修改,删除时出现同种错误:
java.sql.SQLException: Can't call commit when autocommit=truemySQL中表结构如下:
java-mySQL图书管理系统出现这种异常: Can't call commit when autocommit=true
添加用户的代码如下:
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
意思是你已经设置自动提交事务了,但是你又手动提交事务了。所以冲突。
[解决办法]

引用:
Can't call commit when autocommit=true
意思是你已经设置自动提交事务了,但是你又手动提交事务了。所以冲突。


+1


 不知道你如何设置的自动提交了, lz把这句话给翻译下应该也知道答案了吧?
[解决办法]
这个功能好像性能不怎么样

楼主可以通过设置数据库来设置

1、通过set来设置autocommit

mysql> set global init_connect="set autocommit=0";  //提示你用权限更高的用户来设置  
02.ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation  
mysql> set autocommit=0;  
Query OK, 0 rows affected (0.00 sec)  
mysql> select @@autocommit;   //查看一下autocommit的设置  
+--------------+  

[解决办法]
 @@autocommit 
[解决办法]
  
+--------------+  

[解决办法]
            0 
[解决办法]
  
+--------------+  
1 row in set (0.00 sec)

2、
可以修改mysql的配置文件my.cnf来关闭autocommit

[mysqld]  
init_connect='SET autocommit=0'  //在mysqld里面加上这些内容

  
用第二种方法关,有一点要注意,连接mysql用户的权限不能大于启动mysql的用户的权限,不然init_connect='SET autocommit=0'根本不会启作用,也不会报任何错误,

热点排行