2

我想用 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 时,网页会显示:

类型状态报告消息描述请求的资源()不可用。

ps: mysql 表中的列user_id设置为,所以我使用.autoincrementnull

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

我从 netbeans 7.0.1 运行代码

4

3 回答 3

2

因为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);
于 2012-11-11T09:45:10.740 回答
0

我发现了问题。错误是 html 形式的,在属性action处,我有一个源文件,这是我以前从未有过的。:/

于 2012-11-13T21:08:09.590 回答
0
String queryString = "INSERT INTO users(user_id,login,password,full_name,ulevel,team_id) VALUES (?, ?, ?, ?, ?)";

你不应该使用任何东西,甚至null查询!

于 2018-10-05T10:11:18.553 回答