目录
使用JDBC实现javaweb登录界面
题目要求
1.表的创建
use soso; create table user( id int primary key auto_increment, username varchar(32), password varchar(32) ); insert into user values(null,'zhangsan','123'); insert into user values(null,'lisi','234'); 2.使用jdbc
/** * jdbc的工具类 */ public class JDBCUtil { static Properties props = new Properties(); static{ InputStream in = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties"); try { props.load(in); Class.forName(props.getProperty("driverClass")); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //得到连接的方法 public static Connection getConnection() throws SQLException { return DriverManager.getConnection(props.getProperty("url"),props.getProperty("userName"),props.getProperty("password")); } //关闭资源的方法 public static void close(ResultSet rs, PreparedStatement pstmt,Connection conn ){ try { if(rs != null) rs.close(); if(pstmt != null) pstmt.close(); if(conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } /** * 执行增删改的sql */ public static int executeUpdate(String sql,Object...params){ Connection conn = null; PreparedStatement pstmt = null; try { conn = JDBCUtil.getConnection(); pstmt = createPreparedStatement(conn,sql,params); return pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally { JDBCUtil.close(null,pstmt,conn); } return 0; } private static PreparedStatement createPreparedStatement(Connection conn,String sql,Object...params) throws SQLException { conn = JDBCUtil.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql); //给?赋值 params本质数组 if( params != null && params.length > 0 ){ for (int i = 0; i < params.length; i++) { pstmt.setObject(i+1,params[i]); } } return pstmt; } /** * 执行查询的sql * 可变参数约束: * 1. 位于所有参数后面 * 2. 一个方法只能有一个可变参数 * 所有可变参数都可以使用数组替换 */ public static <T> List<T> executeQuery(String sql,Class<T> clazz, Object... params){ //使用jdbc 查询tb_user表 Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = JDBCUtil.getConnection(); //创建PreparedStatement pstmt = createPreparedStatement(conn,sql,params); //执行 rs = pstmt.executeQuery(); //解决ResultSet 反射 List<T> list = new ArrayList<>(); while(rs.next()){ //rs.getXxx() //newInstance() 调用无参构造方法 T t = clazz.newInstance(); //给t的属性赋值 //获取T的属性 Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { //设置允许访问 field.setAccessible(true); //给属性赋值 底层调用 对象.属性 = 值 //getObject(列索引) //getObject(列名) 约定: 属性名与列名一样,赋值 // 如果属性名与列名不一样: 列名:user_name username 属性名:username // 给列名取一个属性名的别名 field.set(t,rs.getObject(field.getName())); } list.add(t); } return list; }catch (Exception e) { e.printStackTrace(); }finally{ JDBCUtil.close(rs,pstmt,conn); } return null; }jdbc的配置文件
#驱动类 值的后面不能敲空格 driverClass = com.mysql.jdbc.Driver #数据库url url = jdbc:mysql://localhost:3306/soso?useUnicode=true&characterEncoding=utf8&useSSL=false #用户名 userName = root #密码 password = 123456文件层级展示
3.登录界面
denglu.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户登录界面</title> <link rel="stylesheet" href="style.css"> </head> <body class="container"> <form action="/LoginServlet" method="post" autocomplete="on"> <table> <tr> <td class="login-wrapper"><h1>登录</h1></td> </tr> <tr> <td><label> <input type="text" name="username" placeholder="用户名" required/> </label></td> </tr> <tr> <td><label> <input type="password" name="password" placeholder="密码" required/> </label></td> </tr> <!-- <tr>--> <!-- <td><label>--> <!-- <input type="password" placeholder="确认密码" required/>--> <!-- </label></td>--> <!-- </tr>--> <!-- <tr>--> <!-- <td><label>--> <!-- <input type="email" placeholder="Email"/>--> <!-- </label></td>--> <!-- </tr>--> <!-- <tr>--> <!-- <td><label>--> <!-- <input type="tel" placeholder="手机号码" required/>--> <!-- </label></td>--> <!-- </tr>--> <tr> <td><span class="login"><input type="submit" value="登录"/></span></td> </tr> <tr> <td><span id="post">没有账号?<a href="#">注册</a></span></td> </tr> </table> </form> </body> </html>
style.css
* { margin: 0; padding: 0; } /*去掉整个网页空白*/ .container { height: 100%; background-image: linear-gradient(to right, #fbc2eb, #a6c1ee); } /*设置背景颜色*/ body { text-align: center; } h1 { font-size: 45px; } /*设置标题字体大小*/ a:link, a:visited { color: rgba(230, 0, 255, 0.35); text-decoration: none; /*超链接去下划线*/ } a:hover { color: cyan; text-decoration: underline; /*鼠标放置超链接显示下划线*/ } table { background-color: white; /*表格背景颜色*/ margin-top: 160px; margin-left: 620px; /*修改整个表格位置大概居于中间位置*/ width: 480px; height: 600px; /*修改整个表格的大小*/ border-radius: 20px; /*整个表格添加边框圆角效果*/ } .login-wrapper { text-align: center; } /*文本居中显示*/ input { outline: none; /*去掉输入框点击后的边框聚焦效果*/ width: 350px; border-bottom: 1px solid #dbdbdb; border-top: 0; border-left: 0; border-right: 0; /*让输入框有下划线效果*/ } .login input { font-size: 20px; width: 200px; height: 40px; background-image: linear-gradient(to right, #a6c1ee, #fbc2eb); /*“登录”按钮背景色*/ color: #fff; /*“登录”两字颜色*/ border-radius: 20px; /*边框圆角*/ } ::placeholder { font-size: 14px; }
4.user的实体类
//用户实体类 public class User { private Integer id; private String username; private String password; public User(Integer id, String username, String password) { this.id = id; this.username = username; this.password = password; } public User() { } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }5.servlet的实现(我使用的是实现Servlet接口的方法实现servlet)
public class LoginServlet implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { //获得到请求参数 String username1 = servletRequest.getParameter("username"); String password1 = servletRequest.getParameter("password"); String sql = "select * from user where username = ? and password = ?"; List<User> users = JDBCUtil.executeQuery(sql,User.class,username1,password1); if(users !=null || users.size()==0){ servletResponse.getWriter().print("<script>alert('fail');location.href='/denglu.html';</script>"); }else { servletResponse.getWriter().print("<script>location.href='/index.html'</script>"); } } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }