java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).怎么办
packagecom.test.action;importjava.sql.*;importorg.apache.struts2.ServletActionContext...
package com.test.action;
import java.sql.*;
import org.apache.struts2.ServletActionContext;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.PreparedStatement;
public class RegistAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String id;
private String username;
private String password;
public String execute() throws Exception{
id=ServletActionContext.getRequest().getParameter("id");
username=ServletActionContext.getRequest().getParameter("username");
password= ServletActionContext.getRequest().getParameter("password");
Connection conn = null;
String URL = "jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, "root", "123456");
String sql = "insert into user(id,username,password) values('"+id+"','"+username+"','"+password+",'0')";
PreparedStatement ps;
try { ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.setString(1, id);
ps.setString(2, username);
ps.setString(3, password);
ps.executeUpdate();
return "no";
} catch (SQLException e)
{ e.printStackTrace();
return "yes";
}
}
} 展开
import java.sql.*;
import org.apache.struts2.ServletActionContext;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import com.opensymphony.xwork2.ActionSupport;
import java.sql.PreparedStatement;
public class RegistAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String id;
private String username;
private String password;
public String execute() throws Exception{
id=ServletActionContext.getRequest().getParameter("id");
username=ServletActionContext.getRequest().getParameter("username");
password= ServletActionContext.getRequest().getParameter("password");
Connection conn = null;
String URL = "jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, "root", "123456");
String sql = "insert into user(id,username,password) values('"+id+"','"+username+"','"+password+",'0')";
PreparedStatement ps;
try { ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
ps.setString(1, id);
ps.setString(2, username);
ps.setString(3, password);
ps.executeUpdate();
return "no";
} catch (SQLException e)
{ e.printStackTrace();
return "yes";
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询