<!DOCTYPE html><html lang="en"><head> <meta chart="UTF-8"> <title>Title</title></head><body> <form action="" method="post"> 用户名:<input type="text" name="urname"> <br> 密码:<input type="password" name="password"><br> <input type="submit" value="登录"> </form></body></html>
CREATE TABLE USER(id INT PRIMARY KEY AUTO_INCREMENT,urname VARCHAR(32) UNIQUE NOT NULL,PASSWORD VARCHAR(32) NOT NULL);
public class Ur { private int id; private String urname; private String password; public int getId() { return id; } public void tId(int id) { this.id = id; } public String getUrname() { return urname; } public void tUrname(String urname) { this.urname = urname; } public String getPassword() { return password; } public void tPassword(String password) { this.password = password; } @Override public String toString() { return "Ur{" + "id=" + id国家励志奖学金申请书范文 + ", urname='" + urname + '\'' + ", password='" + password + '\'' + '}'; }}
这里使用的是c3p0 / druid 两种数据库连接池技术 分别需要在项目导入相应的jar包
public class JDBCUtils { private static DataSource ds ;static { try { //1.加载配置文件Properties pro = new Properties();//使用ClassLoader加载配置文件,获取字节输入流InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");pro.load(is);//2.初始化连接池对象ds = DruidDataSourceFactory.createDataSource(pro);} catch (IOException e) { e.printStackTrace();} catch (Exception e) { e.printStackTrace();}}/** * 获取连接池对象 */public static DataSource getDataSource(){ return ds;}/** * 获取连接Connection对象 */public static Connection getConnection() throws SQLException { return ds.getConnection();}system.out.println("=============================================================")public class JDBCButil { final static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();// 获取连接方法// 返回一个连接对象public static Connection getCon() { // 连接使用c3p0进行获取// 使用c3p0数据库连接池获取连接Connection connection = null;try { connection = comboPooledDataSource.getConnection();} catch (SQLException e) { System.err.println("获取连接失败");return null;}return connection;}// DML方法// 不支持事务 单条sql语句执行public static boolean DML(String sql, Object... o) { // 获取连接Connection con = getCon();// 创建预编译对象try { PreparedStatement ps = con.prepareStatement(sql);for (int i = 0; i < o.length; i++) { ps.tObject((i + 1), o[i]);}ps.executeUpdate();} catch (SQLException e) { System.out.println("查询执行失败:" + sql);return fal;}return true;}// DML方法// 支持事务 多条sql语句执行public static boolean DML(Connection con, String sql, Object... o) { // 创建预编译对象try { PreparedStatement ps = con.prepareStatement(sql);for (int i = 0; i < o.length; i++) { ps.tObject((i + 1), o[i]);}ps.executeUpdate();} catch (SQLException e) { System.out.println("查询执行失败:" + sql);return fal;}return true;}// 查询dql语句方法public static <E> ArrayList<E> DQL(String sql, Class<E> c, Object... o) { ArrayList<E> list = new ArrayList<>();try { // 获取连接Connection con = getCon();// 准备预编译对象PreparedStatement ps = con.prepareStatement(sql);// 获取元数据 准备存储所有列名的数组ResultSetMetaData metaData = ps.getMetaData();// 创建指定长度用于存储列名的数组String[] names = new String[metaData.getColumnCount()];// 循环为names数组进行赋值for (int i = 0; i < names.length; i++) { names[i] = metaData.getColumnLabel(i + 1);// 获取指定列名}for (int i = 0; i < o.length; i++) { ps.tObject(i+1, o[i]);}// 执行sql返回结果集ResultSet rs = ps.executeQuery();while (rs.next()) { // 每一行数据就是一个对象// 使用反射创建对象E obj = c.newInstance();// 当前行所有列名 在names数组中存储// 循环names数组取出当前行对应数据for (String colname : names) { Object value = rs.getObject(colname);// 获取列名对应值// 将值存入相应对象// 使用反射获取类中同名的属性对象Field field = c.getDeclaredField(colname);// 私有属性使用前必须赋权field.tAccessible(true);// 调用属性对象的t方法为指定对象进行赋值field.t(obj, value);}// 列名循环结束后对应对象属性已经全部进行赋值// 将对象存储至集合中list.add(obj);}} catch数三角形个数的规律 (Exception e) { e.printStackTrace();return null;}return list;}}ublic class JDBCUtils { private static DataSource ds ;static { try { //1.加载配置文件Properties pro = new Properties();//使用ClassLoader加载配置文件,获取字节输入流InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");pro.load(is);//2.初始化连接池对象ds = DruidDataSourceFactory.createDataSource(pro);} catch (IOException e) { e.printStackT祝福的英语race();} catch (Exception e) { e.printStackTrace();}}/** * 获取连接池对象 */public static DataSource getDataSource(){ return ds;}
public class UrDao { //声明JDBCTemplate对象共用private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());/** * 登录方法 * @param loginUr 只有用户名和密码 * @return ur包含用户全部数据,没有查询到,返回null */public Ur login(Ur loginUr){ try { //1.编写sqlString sql = "lect * from ur where urna2015新年me = ? and password = ?";//2.调用query方法Ur ur = template.queryForObject(sql,new BeanPropertyRowMapper<Ur>(Ur.class),loginUr.getUrname(), loginUr.getPassword());return ur;} catch (DataAccessException e) { e.printStackTrace();return null;}}}
@WebServlet("/loginServlet")public class LoginServlet extends HttpServlet { @育儿教育Overrideprotected void doGet(HttpServletRequest req, HttpServletRespon resp) throws ServletException, IOException { //1.设置编码req.tCharacterEncoding("utf-8");//2.获取请求参数String urname = req.getParameter("urname");String password = req.getParameter("password");//3.封装ur对象Ur loginUr = new Ur();loginUr.tUrname(urname);loginUr.tPassword(password);//4.调用UrDao的login方法UrDao dao = new UrDao();Ur ur = dao.login(loginUr);//5.判断urif(ur == null){ //登录失败req.getRequestDispatcher("/failServlet").forward(req,resp);}el{ //登录成功//存储数据req.tAttribute("ur",ur);//转发req.getRequestDispatcher("/successServlet").forward(req,resp);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletRespon resp) throws ServletException, IOException { this.doGet(req,resp);}}
public class SuccessServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletRespon respon) throws ServletException, IOException { //获取request域中共享的ur对象Ur ur = (Ur) request.getAttribute("ur");if(ur != null){ //给页面写一句话//设置编码respon.tContentType("text/html;chart=utf-8");//输出respon.getWriter().write("登录成功!"+ur.getUrname()+",欢迎您");}}@WebServlet("/failServlet")public class FailServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletRespon respon) throws ServletException, IOException { //给页面写一句话//设置编码respon.tContentType("text/html;chart=utf-8");//输出respon.getWriter().write("登录失败,用户名或密码错误");}protected void doGet(HttpServletRequest request, HttpServletRespon respon) throws ServletException, IOException { this.doPost(request,respon);}}
本文地址:https://blog.csdn.net/weixin_45289374/article/details/110498412
本文发布于:2023-04-07 15:45:58,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/b3cd472972915c7d3a7594b349b7b6b3.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Servlet实现简单的用户登录功能.doc
本文 PDF 下载地址:Servlet实现简单的用户登录功能.pdf
留言与评论(共有 0 条评论) |