執行execute語句報錯(sex字段是枚舉型的)
StudentDao里的插入方法
public?void?addStudent(Student?student)?throws?SQLException{
Connection?conn=DBUtil.getConnection();
String?sql="insert?student"+
"(number,name,sex,age,birthday)"+
"values("+
"?,?,?,?,?)";
PreparedStatement?ps=conn.prepareStatement(sql);
ps.setString(1,?student.getNumber());
ps.setString(2,?student.getName());
ps.setString(3,?student.getSex());
ps.setInt(4,?student.getAge());
ps.setDate(5,?new?Date(student.getBirthday().getTime()));
ps.execute();
conn.close();
}action里的
public?class?StudentAction?{
public?static?void?main(String[]?args)?throws?Exception?{
StudentDao?sd=new?StudentDao();
Student?stu=new?Student();
stu.setNumber("140521203");
stu.setName("張三");
stu.setAge(19);
stu.setSex("男");
stu.setBirthday(new?SimpleDateFormat("yyyy-MM-dd").parse("1996-03-20"));
sd.addStudent(stu);
List<Student>?sl=sd.query();
for?(Student?student?:?sl)?{
System.out.println(student.toString());
}
}
}控制臺報錯:
Exception?in?thread?"main"?java.sql.SQLException:?Could?not?retrieve?transation?read-only?status?server at?com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) at?com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) at?com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975) at?com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920) at?com.mysql.jdbc.SQLError.createSQLException(SQLError.java:951) at?com.mysql.jdbc.SQLError.createSQLException(SQLError.java:941) at?com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3936) at?com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3907) at?com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2408) at?com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375) at?com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) at?china_mozhu.dao.StudentDao.addStudent(StudentDao.java:28) at?china_mozhu.action.StudentAction.main(StudentAction.java:21) Caused?by:?java.sql.SQLException:?Unknown?system?variable?'tx_read_only' at?com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) at?com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187) at?com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119) at?com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) at?com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) at?com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809) at?com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2758) at?com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612) at?com.mysql.jdbc.ConnectionImpl.isReadOnly(ConnectionImpl.java:3930) ...?6?more
2015-12-06
額,已找到原因,connector和MySQL版本不匹配