我已经编写了 get 方法,但我想编写 set 方法,但它给了我错误提示 - PreparedStatement 类型中的方法 setString(int, 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 + "]";
}
}