gpt4 book ai didi

mysql - 用jsp向MySQL表中插入一行

转载 作者:行者123 更新时间:2023-11-29 02:30:55 27 4
gpt4 key购买 nike

我想用 jsp 代码向 mysql 数据库中插入行,但我做不到。这是我没有 html 形式的 jsp 代码:

<%
String login = request.getParameter("login");
String password = request.getParameter("password");
String full_name = request.getParameter("full_name");
String ulevel = request.getParameter("ulevel");
String team_id = request.getParameter("team_id");

Connection connection = null;
PreparedStatement pstatement = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
int updateQuery = 0;
if(login!=null && password!=null && full_name!=null && ulevel!=null && team_id!=null){
if(login!="" && password!="" && full_name!="" && ulevel!="" && team_id!="") {
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/android","root","root");
String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (null, ?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(2, login);
pstatement.setString(3, password);
pstatement.setString(4, full_name);
pstatement.setString(5, ulevel);
pstatement.setString(6, team_id);
updateQuery = pstatement.executeUpdate();
if (updateQuery != 0) { %>

当我按下 SUBMIT 时,网页显示如下:

type Status report message descriptionThe requested resource () is not available.

p.s.user_id在mysql表中设置为autoincrement,所以我使用null

但我不知道,那是正确的方法...

我从 netbeans 7.0.1 运行代码

最佳答案

因为 ID自动递增,从列表中删除它,preparedstatement 在其参数上寻找索引 1

String queryString = "INSERT INTO users(login,password,full_name,ulevel,team_id) VALUES (?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, login);
pstatement.setString(2, password);
pstatement.setString(3, full_name);
pstatement.setString(4, ulevel);
pstatement.setString(5, team_id);

下面这个没有测试

只需使用 1 开始参数,因为只有 5 个参数需要定义。

String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (null, ?, ?, ?, ?, ?)";
pstatement = connection.prepareStatement(queryString);
pstatement.setString(1, login);
pstatement.setString(2, password);
pstatement.setString(3, full_name);
pstatement.setString(4, ulevel);
pstatement.setString(5, team_id);

关于mysql - 用jsp向MySQL表中插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com