简单登入模块
2015-11-28 17:40
302 查看
主类
package Servlet; import java.sql.*; public class Dao { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getconn() throws SQLException { Connection conn = null; conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "root"); return conn; } public static void closeconn(Connection conn) throws SQLException { if (conn != null && !conn.isClosed()) { conn.close(); } } }数据插入
package Servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class Input extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("gb2312"); String userName = request.getParameter("userName"); String userPassword = request.getParameter("userPassword"); String sql = "insert into user values(?,?,?)"; PrintWriter out = response.getWriter(); // HttpSession session = request.getSession(); Connection conn = null; try { conn = Dao.getconn(); } catch (SQLException e1) { e1.printStackTrace(); } try { if (conn != null) { PreparedStatement ps; ps =conn.prepareStatement(sql); ps.setInt(1, 0); ps.setString(2, userName); ps.setString(3, userPassword); int rs = ps.executeUpdate(); if (rs != 0) { response.sendRedirect("Students.jsp"); } else { out.println("添加失败!"); } Dao.closeconn(conn); } } catch (SQLException e) { e.printStackTrace(); } } }数据导出
package Servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class Output extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("gb2312"); String UserName = request.getParameter("UserName"); String UserPassword = request.getParameter("UserPassword"); String sql = "select * from user where UserName=? and UserPassword=?"; PrintWriter out = response.getWriter(); //HttpSession session = request.getSession(); Connection conn = null; try { conn = Dao.getconn(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (conn != null) { PreparedStatement ps; try { ps =conn.prepareStatement(sql); ps.setString(1, UserName); ps.setString(2, UserPassword); ResultSet rs = ps.executeQuery(); //如果有数据,则跳转,如果没有下一条数据(空结果集)则 向网页输出登入失败 if(rs.next()){ response.sendRedirect("Students.jsp"); }else out.println("登入失败!"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Dao.closeconn(conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }登入界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登入</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <center> <h1>系统登入</h1> <form action="Output" method="post"> 用户名:<input type="text" name="UserName"> <p> 密 码:<input type="password" name="UserPassword"> <p> <input type="submit" value="提交"> <input type="reset" value="取消"> <p> 還沒註冊?<a href="index.jsp">點擊</a>註冊 </form> </center> </body> </html>注册界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登入</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <center> <form action="Input" method="post"> <h1>用户注册</h1> 用户名:<input type="text" name="userName"> <p> 密 码:<input type="password" name="userPassword"> <p> <input type="submit" value="注册"> <input type="reset" value="取消"><br> </form> </center> </body> </html>
相关文章推荐
- Android 屏幕水平、垂直使用不同的布局xml文件
- I2C总线简介
- Android dialog使用总结
- MBProgressHUD上传照片进度提示
- 树边,前向边,后向边,横叉边
- [ecshop 资料]商品显示 选择属性名称然后标题也加上属性的名称
- 类的继承
- 在flex&bison的基础上利用符号表进行语义分析
- 【JavaScript】(1)——JavaScript概述
- 前端5
- Product of Array Except Self
- unity3d 代码设置贴图Read/Write Enable属性
- Delphi 7下使用VT实现树型列表结合控件
- [磁盘数据分析] 实现解析特定分区体系(DOS分区体系)的主引导记录扇区
- <iOS> 谈谈iOS Animation -- calculationMode属性
- Android学习笔记-ScollView实现QQ消息界面滑动效果
- UITabBarController
- 使用git对unity3d项目进行版本控制
- 20135327郭皓——信息安全系统设计基础第十二周学习总结
- MBProgressHUD+FastCall