对于mysql的warnings如何处理? Field 'userName' doesn't have a default value
我先创建一个表,然后分别用mysql客户端 和 一段java程序插入一条记录
1, 如果是mysql客户端, 有warnings产生,但成功插入了
2, 如果是java程序的话有异常抛出,不能插入, 是connector 的问题吗?
下面是建表的SQL:
CREATE TABLE `xudc`.`userCore` (
`userId` mediumint(9) unsigned NOT NULL,
`userName` varchar(14) NOT NULL,
`passwordHash` varchar(20) NOT NULL,
`email` text NOT NULL,
`emailVisible` tinyint(1) NOT NULL,
`safetyQuestion` varchar(50) NOT NULL,
`safetyAnswer` varchar(50) NOT NULL,
`registerDatetime` bigint(20) NOT NULL,
`loginCount` int(11) NOT NULL,
`lastLoginDatetime` bigint(20) NOT NULL,
`lastLoginIp` varchar(15) NOT NULL,
`isTerminaled` tinyint(1) NOT NULL,
`updateLogs` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='the table store user core data'
下面是mysql客户端的插入:
mysql> insert into userCore(userId)values(2342342);
Query OK, 1 row affected, 12 warnings (0.00 sec)
mysql> show warnings;
+---------+------+--------------------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------------------+
| Warning | 1364 | Field 'userName' doesn't have a default value |
| Warning | 1364 | Field 'passwordHash' doesn't have a default value |
| Warning | 1364 | Field 'email' doesn't have a default value |
| Warning | 1364 | Field 'emailVisible' doesn't have a default value |
| Warning | 1364 | Field 'safetyQuestion' doesn't have a default value |
| Warning | 1364 | Field 'safetyAnswer' doesn't have a default value |
| Warning | 1364 | Field 'registerDatetime' doesn't have a default value |
| Warning | 1364 | Field 'loginCount' doesn't have a default value |
| Warning | 1364 | Field 'lastLoginDatetime' doesn't have a default value |
| Warning | 1364 | Field 'lastLoginIp' doesn't have a default value |
| Warning | 1364 | Field 'isTerminaled' doesn't have a default value |
| Warning | 1364 | Field 'updateLogs' doesn't have a default value |
+---------+------+--------------------------------------------------------+
下面是java程序的插入和异常抛出:
Connection conn = ConnectionManager.getConnection(DatabaseConfig.DEFAULT_CONNECTION_PROVIDER_USER);
String sql = "INSERT INTO userCore (userId)VALUES(?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setLong(1, 2332423432l);
pstmt.executeUpdate();
java.sql.SQLException: Field 'userName' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3256)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1313)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1585)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1500)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1485)
at com.jeadu.platform.db.TestCP.main(TestCP.java:66)
求助大家,谢谢!!