我写了get方法,但是我想编写set方法但是它给了我错误说 - PreparedStatement类型中的方法setString(int,String)不适用于参数(String,String,String) ,String,String,)我的代码如下 包com.glomindz.mercuri.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.glomindz.mercuri.pojo.User;
import com.glomindz.mercuri.util.MySingleTon;
public class UserServicesDAO {
private Connection connection;
public UserServicesDAO() {
// connection = new MySingleTon().getConnection();
connection = MySingleTon.getInstance().getConnection();
}
public List<User> get_all_data() {
List<User> usersList = new ArrayList<User>();
try {
PreparedStatement stmt = connection.prepareStatement("INSERT INTO spl_user_master(name,email,mobile,password,role,status,last_update)VALUES(?,?,?,?,?,?,?)");
stmt.setString("Charlie Sheen","help@glomindz.com","9654087107","cbf51a71a11d7ec348b0d7e9b2f0055f","admin","3","null");
boolean execute = stmt.execute();
System.out.println(execute);
ResultSet resultSet = stmt.getResultSet();
System.out.println(resultSet.getMetaData());
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setEmail(resultSet.getString("email"));
user.setMobile(resultSet.getString("mobile"));
user.setPassword(resultSet.getString("password"));
user.setRole(resultSet.getString("role"));
user.setStatus(resultSet.getString("status"));
user.setLast_udpate(resultSet.getString("last_update"));
usersList.add(user);
}
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return usersList;
}
public static void main(String[] args) {
UserServicesDAO userdao = new UserServicesDAO();
List<User> data = userdao.get_all_data();
System.out.println(data);
System.exit(0);
}
} 我的用户类是 包com.glomindz.mercuri.pojo;
public class User {
private int id;
private String Name;
private String email;
private String mobile;
private String password;
private String role;
private String status;
private String last_update;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getLast_update() {
return last_update;
}
public void setLast_udpate(String last_update) {
// TODO Auto-generated method stub
}
@Override
public String toString() {
return "User [id=" + id + "Name=" + Name + ", email=" + email + ", mobile=" + mobile
+ ", password=" + password + ", role=" + role + ", status="
+ status + ", last_update=" + last_update + "]";
}
}
答案 0 :(得分:2)
PreparedStatement.setString()不是变量arity方法;它不需要变量号。论点。每个占位符(?)必须使用
单独设置stmt.setString(1, "Charlie Sheen");
stmt.setString(2, "help@glomindz.com");
等等。
使用Spring的JdbcTemplate
jdbcTemplate.update("INSERT INTO spl_user_master(name,email,mobile,password,role,status,last_update)VALUES(?,?,?,?,?,?,?)", new Object[] {
"Charlie Sheen","help@glomindz.com","9654087107","cbf51a71a11d7ec348b0d7e9b2f0055f","admin","3","null"});
答案 1 :(得分:1)
stmt.setString("Charlie Sheen","help@glomindz.com","9654087107","cbf51a71a11d7ec348b0d7e9b2f0055f","admin","3","null");
这不起作用。您必须单独设置,例如:
stmt.setString(1, "Charlie Sheen);
其中1是变量的位置,“Charlie Sheen”是值。您需要为PreparedStatement中的每个占位符?
执行此操作。
答案 2 :(得分:0)
问题是,您不能使用setString一次设置所有参数。您需要逐个使用setString设置每个参数。:
stmt.setString("Charlie Sheen","help@glomindz.com","9654087107","cbf51a71a11d7ec348b0d7e9b2f0055f","admin","3","null");
来自javadocs:
void setString(int parameterIndex, 字符串x) 抛出SQLException
将指定参数设置为给定的Java String值。该 驱动程序将其转换为SQL VARCHAR或LONGVARCHAR值(取决于 关于参数的大小相对于驱动程序对VARCHAR的限制 当它将数据发送到数据库时。
参数:parameterIndex - 第一个参数是1,第二个参数是 2,... x - 参数值
抛出:SQLException - 如果parameterIndex不对应a SQL语句中的参数标记;如果数据库访问错误 发生或在关闭的PreparedStatement上调用此方法
答案 3 :(得分:0)
如this Oracle教程中所示,setString
方法接受一个整数,表示参数的编号和表示值的字符串。所以,在你的情况下,你需要做这样的事情:
PreparedStatement stmt = connection.prepareStatement("INSERT INTO spl_user_master(name,email,mobile,password,role,status,last_update)VALUES(?,?,?,?,?,?,?)");
stmt.setString(1, "Charlie Sheen");
stmt.setString(2, "help@glomindz.com");
...
答案 4 :(得分:0)
正如其他人发布的那样,您将错误的参数传递给setString()
方法。请记住,您必须为查询中单独定义的每个占位符?
设置值。
PreparedStatement stmt = connection.prepareStatement("INSERT INTO
spl_user_master(name,email,mobile,password,role,status,last_update)
VALUES(?,?,?,?,?,?,?)");
stmt.setString(1,"Charlie Sheen");
stmt.setString(2,"help@glomindz.com");
... so on ...
如果相应的列定义为setString()
,请记住SQL VARCHAR or LONGVARCHAR
用于设置SQL VARCHAR or LONGVARCHAR
值代替占位符。对于其他数据类型,API中有各种setXXX()
方法。