果然又报错了 2023-08-21 17:22 采纳率: 50%
浏览 10

用java链接MySQL插入变量时,网页报500错误

写web时用java链接MySQL插入数据服务器报500错误
java.sql.SQLIntegrityConstraintViolationException: Duplicate entry 'null' for key 'student.PRIMARY'

java代码


import java.sql.*;
import java.util.Objects;

public class database {
    public void insert(String username, String password1, String password2) throws SQLException{
        Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","Hwg2539785237");
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("驱动添加成功,可以进行数据库连接了");
        }catch (Exception e){
            System.out.println("驱动加载失败,错误原因是:"+e.getMessage());
        }

        String node="insert into student(id,name,age)values('"+username+"','"+password1+"','"+password2+"')";
        //String insert1="insert into student values('hahaha','wotama','jianzhitiancai')";执行注释代码插入常量时可以正确插入
        Statement stmt = conn.createStatement();
        int result=stmt.executeUpdate(node);
        //int result1=stmt.executeUpdate(insert1);
        System.out.println(result);
    }
}

传给java值的servlet


import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/servlet")
public class servlet extends HttpServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String name = request.getParameter("username");
        String password1 =request.getParameter("password1");
        String password2 =request.getParameter("password2");

        database data=new database();
        try {
            data.insert(name,password1,password2);//调试时这里是有正确参数的
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

报错截图

img

看到报错里Duplicate entry 'null'感觉是参数没传过来,但是调试的时候显示在database里的username变量是有输入的数据的,不知道是不是语法错误≧ ﹏ ≦

配置:tomcat,MySQL,mysql-connector-java-8.0.28.jar。编译器:idea2023

  • 写回答

3条回答 默认 最新

  • 一只小阿焜 2023-08-21 17:36
    关注

    个人理解:
    感觉是name类型的问题,student.primary应该是student表的主键,传入的name的类型与student表设置的id类型不一致。
    也有可能是你写错了,id为啥要与username对应,name为啥要与password1对应,age为啥跟password2对应。。。。。。如果没错的话,你这命名方式。。。说实话,有点离谱。

    评论

报告相同问题?

问题事件

  • 创建了问题 8月21日

悬赏问题

  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 关于#c语言#的问题:我在vscode和codeblocks中编写c语言时出现打不开源文件该怎么办
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥500 服务器搭建cisco AnyConnect vpn
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊
  • ¥15 快速扫描算法求解Eikonal方程咨询