美食侵略者
2015-12-06 12:58
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
额,已找到原因,connector和MySQL版本不匹配
JDBC之 “ 对岸的女孩看过来”
99334 学习 · 916 问题
相似问题