mysql:Column id cannot be null

点个赞,看一看,好习惯!本文 GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了3个月总结的一线大厂Java面试总结,本人已拿腾讯等大厂offer。
另外,原创文章首发在我的个人博客:blog.ouyangsihai.cn,欢迎访问。

一、问题

由于在项目中更改了数据库的字段,然后再连接测试的时候发现总是说:Column 'id' cannot be null 这个问题,也就是数据库的id不能为空,因为是主键。

二、问题解决

其实很简单,可能是自己改动了数据库,然后主键id不是自动增长了,只需要将数据库的字段id设置为自动增长,问题解决了。

最后,再分享我历时三个月总结的 Java 面试 + Java 后端技术学习指南,这是本人这几年及春招的总结,已经拿到了大厂offer,整理成了一本电子书,拿去不谢,目录如下:

现在免费分享大家,在下面我的公众号 程序员的技术圈子 回复 面试 即可获取。

有收获?希望老铁们来个三连击,给更多的人看到这篇文章

1、老铁们,关注我的原创微信公众号「程序员的技术圈子」,专注于Java、数据结构和算法、微服务、中间件等技术分享,保证你看完有所收获。

2、给俺点个赞呗,可以让更多的人看到这篇文章,顺便激励下我继续写作,嘻嘻。

3、另外,原创文章首发在我的个人博客:blog.ouyangsihai.cn,欢迎访问。

点赞是对我最大的鼓励
↓↓↓↓↓↓

已标记关键词 清除标记
我用的是mysql数据库 程序提示Column '列名' cannot be null错误 请问这个问题怎么解决我用的是mysql数据库 程序提示Column '列名' cannot be null错误 请问这个问题怎么解决 [b]问题补充:[/b] public void add(Connection con, DepartVO dep) throws SQLException { // TODO Auto-generated method stub PreparedStatement stmt = null; ResultSet rs = null; try { StringBuffer sql = new StringBuffer(); sql.append(" INSERT INTO departdb("); sql.append(" d_name,sis)"); sql.append(" VALUES(?,?) "); stmt = con.prepareStatement(sql.toString()); stmt.setString(1, dep.getDName()); stmt.setDate(2, dep.getSis()); stmt.execute(); } finally { // 关闭连接; stmt.close(); } } 错误提示: java.sql.SQLException: Column 'd_name' cannot be null at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1085) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:770) at com.lgjt.dao.DepartDAO.add(DepartDAO.java:27) at com.lgjt.service.BusinessService.saveDepart(BusinessService.java:47) at com.lgjt.struts.action.DepartAddAction.execute(DepartAddAction.java:51) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) at javax.servlet.http.HttpServlet.service(HttpServlet.java:710) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at com.lgjt.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:34) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689) at java.lang.Thread.run(Thread.java:595) [b]问题补充:[/b] 我在文本框中输入 内容了啊 我尝试过在mysql中d_name字段中设置可以为空 但是什么东西都录入不进去
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'userName' cannot be null ### The error may exist in com/demo/dao/UserMapper.xml ### The error may involve com.demo.dao.UserMapper.insertUser-Inline ### The error occurred while setting parameters ### SQL: insert into user (userName,password,id) values (?,?,?) ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'userName' cannot be null ; SQL []; Column 'userName' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'userName' cannot be null org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811) javax.servlet.http.HttpServlet.service(HttpServlet.java:624) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107 一直提示字段不能为空,这是因为没有接收到页面的请求参数吗?所以#{}接收不到值?怎样解决,求好心人搭救,感谢!
一直出现以下错误:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'EmployeeId' cannot be null ![图片说明](https://img-ask.csdn.net/upload/201801/04/1515057353_712815.png) 因为题目要求主键自增,类型是varchar,所以我用了触发器进行自增 begin set new.employeeid=concat('E',lpad(((select substring(employeeid,2,3) from epoint_employee where employeeid=(select employeeid from epoint_employee order by employeeid desc limit 1))+1),3,0)); end 以下是java代码: Test Employee EmployeeImpl import java.sql.SQLException; public class Test { public static void main(String[] args) throws SQLException { // TODO Auto-generated method stub EmployeeImpl ei=new EmployeeImpl(); System.out.println(ei.save("E001", "123", "SWEET", 22, 4325)); // System.out.println(ei.save("E002", "321", "TINA", 21, 4000)); // System.out.println(ei.save("E003", "111", "JACK", 21, 4000)); // System.out.println(ei.save("E004", "222", "MARK", 21, 4000)); // System.out.println(ei.save("E005", "123", "NICK", 22, 4325)); // System.out.println(ei.delete("004")); // System.out.println(ei.update("001", "22222", "ZS", 22, 4325)); // ei.find("001"); } } import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class EmployeeImpl implements Employee { public static String driver = "com.mysql.jdbc.Driver"; public static String url = "jdbc:mysql://localhost:3306/epoint?characterEncoding=utf8&useSSL=true"; public static String user = "sa"; public static String pwd = "11111"; public String EmployeeId;// 员工账号 public String Password; // 员工密码 public String Name; // 姓名 public int Age; // 年龄 public double Salary; // 工资 public Connection getConnection() { Connection con = null; try { Class.forName(driver); con = DriverManager.getConnection(url, user, pwd); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public boolean save(String employeeid, String password, String name, int age, double salary){ Connection con = new EmployeeImpl().getConnection(); PreparedStatement pst=null; // boolean b=true; try { pst=con.prepareStatement("insert into Epoint_EMPLOYEE(EmployeeId,Password,Name,Age,Salary) values(?,?,?,?,?)");//?: 占位符 //占位符有几个就写几个,否则会报错 pst.setString(1, employeeid); pst.setString(2, password); pst.setString(3, name); pst.setInt(4, age); pst.setDouble(5, salary); // b=pst.execute();//返回Boolean类型,false表示执行CUD语句,true为R int i=pst.executeUpdate();//返回受影响的行数 System.out.println(i); // System.out.println(pst.getUpdateCount());//打印了几行 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; } public boolean update(String employeeid, String password, String name, int age, double salary){ Connection con = new EmployeeImpl().getConnection(); PreparedStatement pst=null; boolean b=true; try { pst=con.prepareStatement("update Epoint_EMPLOYEE set password=? where employeeid=?");//?: 占位符 //占位符有几个就写几个,否则会报错 pst.setString(1, password); pst.setString(2, employeeid); b=pst.execute();//返回Boolean类型,false表示执行CUD语句,true为R // int i=pst.executeUpdate();//返回受影响的行数 // System.out.println(i); // System.out.println(pst.getUpdateCount());//打印了几行 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return b; } public boolean delete(String employeeid) { Connection con = new EmployeeImpl().getConnection(); PreparedStatement pst=null; boolean b=true; try { pst=con.prepareStatement("delete from Epoint_EMPLOYEE where employeeid=?"); //占位符有几个就写几个,否则会报错 pst.setString(1, employeeid); b=pst.execute();//返回Boolean类型,false表示执行CUD语句,true为R // int i=pst.executeUpdate();//返回受影响的行数 // System.out.println(i); // System.out.println(pst.getUpdateCount());//打印了几行 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return b; } public void find(String employeeid){ Connection con = new EmployeeImpl().getConnection(); ResultSet rs = null; PreparedStatement pst=null; try { pst=con.prepareStatement("select * from Epoint_EMPLOYEE where employeeid=?"); //占位符有几个就写几个,否则会报错 pst.setString(1, employeeid); rs = pst.executeQuery(); while (rs.next()) { System.out.println(rs.getString(1)); System.out.println(rs.getString(2)); System.out.println(rs.getString(3)); System.out.println(rs.getInt(4)); System.out.println(rs.getDouble(5)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (rs != null && !rs.isClosed()) { rs.close(); } if (pst != null && !pst.isClosed()) { pst.close(); } if (con != null && !con.isClosed()) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } import java.sql.SQLException; public interface Employee { public boolean save(String employeeid, String password,String name, int age, double salary); public boolean update(String employeeid, String password,String name, int age, double salary); public boolean delete(String employeeid); public void find (String employeeid); }
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值