我正在尝试编写将数据插入数据库(mysql)的servlet。问题是我需要在从servlet调用的其他类中编写它,并在没有getParameter()的情况下插入数据; UPD:这是一个完整的代码,我希望压缩成2个类,1个用于servlet,1个用于包含参数的类
UserController.java
protected void doPost(UserDao request, HttpServletResponse
response) throws ServletException, IOException {
UserDao.addUser();
}
UserDAO.java
public class UserDao {
private static Connection connection;
public UserDao() {
connection = (Connection) DBconnect.getConnection();
}
public static void addUser(String fname, String lname, String dob, String email, String uid) {
try {
PreparedStatement preparedStatement = ((java.sql.Connection) connection).prepareStatement("insert into users(firstname,lastname,dob,email) values (?, ?, ?, ? );");
preparedStatement.setString(1, fname);
preparedStatement.setString(2, lname);
preparedStatement.setString(3, dob);
preparedStatement.setString(4, email);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
从您的代码中我可以看到请求中的所有参数现在都在User对象中设置。
- >编写一个接受User对象的DAO图层类。然后编写查询以使用用户对象插入数据。
答案 1 :(得分:0)
我不确定你究竟在问什么,所以我用两种不同的方式做到了...... 希望这会给你一个足够好的例子来做你想做的事情。
---- WITH USER OBJECT ----
<强>的servlet:强>
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String fname = request.getParameter("firstName");
String lname = request.getParameter("lastName");
String dob = request.getParameter("dob");
String email = request.getParameter("email");
String uid = request.getParameter("userid");
dao.newUser(fname,lname,dob,email,uid);
}
<强> userDAO的强>
public class UserDao {
private Connection connection;
public UserDao() {
connection = DbConnect.getConnection();
}
public void newUser(String fname, String lname, String dob, String email, String uid){
User user = new User();
user.setFirstName(fname);
user.setLastName(lname);
user.setDob(dob);
user.setEmail(email);
addUser(user);}
}
public void addUser(User user) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("insert into
users(firstname,lastname,dob,email) values (?, ?, ?, ? )");
preparedStatement.setString(1, user.getFirstName());
preparedStatement.setString(2, user.getLastName());
preparedStatement.setString(3, user.getDob());
preparedStatement.setString(4, user.getEmail());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
----没有用户对象----
<强>的servlet:强>
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String fname = request.getParameter("firstName");
String lname = request.getParameter("lastName");
String dob = request.getParameter("dob");
String email = request.getParameter("email");
String uid = request.getParameter("userid");
dao.addUser(fname,lname,dob,email,uid);
}
<强> userDAO的强>
public class UserDao {
private Connection connection;
public UserDao() {
connection = DbConnect.getConnection();
}
public void addUser(String fname, String lname, String dob, String email, String uid) {
try {
PreparedStatement preparedStatement = connection
.prepareStatement("insert into users(firstname,lastname,dob,email) values (?, ?, ?, ? );");
preparedStatement.setString(1, fname);
preparedStatement.setString(2, lname);
preparedStatement.setString(3, dob);
preparedStatement.setString(4, email);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}