赞
踩
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import entity.User;
public class UserDao {
public boolean login(String name,String password){
String sql = “select * from users where name = ? and password = ?”;
Connection con = JDBCUtils.getConn();
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,name);
pst.setString(2,password);
if(pst.executeQuery().next()){
return true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(con);
}
return false;
}
public boolean register(User user){
String sql = “insert into users(name,username,password,age,phone) values (?,?,?,?,?)”;
Connection con = JDBCUtils.getConn();
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,user.getName());
pst.setString(2,user.getUsername());
pst.setString(3,user.getPassword());
pst.setInt(4,user.getAge());
pst.setString(5,user.getPhone());
int value = pst.executeUpdate();
if(value>0){
return true;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(con);
}
return false;
}
public User findUser(String name){
String sql = “select * from users where name = ?”;
Connection con = JDBCUtils.getConn();
User user = null;
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1,name);
ResultSet rs = pst.executeQuery();
while (rs.next()){
int id = rs.getInt(1);
String namedb = rs.getString(2);
String username = rs.getString(3);
String passworddb = rs.getString(4);
int age = rs.getInt(5);
String phone = rs.getString(6);
user = new User(id,namedb,username,passworddb,age,phone);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUtils.close(con);
}
return user;
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。