jsp与数据库
来源:互联网 发布:ubuntu 安装terminal 编辑:程序博客网 时间:2024/06/05 06:51
<%@ page language="java" pageEncoding="UTF-8" %><html> <head> <title>用户注册</title> <script type="text/javascript" src="js/news.js"></script> </head> <% //获取验证消息 request.setCharacterEncoding("UTF-8"); String message = (String) request.getAttribute("message"); %> <body> <div id="mess" style="color:red;"> <%=message != null && !message.equals("") ? message : "" %> </div> <form name="form1" method="post" action="control.jsp" onsubmit="return checkAll()"> <table> <tr> <td>用户名:</td> <td><input type="text" name="userName" id="userName" value='<%=request.getParameter("userName") == null ? "" : request.getParameter("userName") %>'> </td> </tr> <tr> <td>输入登录密码:</td> <td><input type="password" name="pwd" id="pwd" value='<%=request.getParameter("pwd") == null ? "" : request.getParameter("pwd") %>'> </td> </tr> <tr> <td>再次输入密码:</td> <td><input type="password" name="validatepwd" id="validatepwd" value='<%=request.getParameter("validatepwd") == null ? "" : request.getParameter("validatepwd") %>'> </td> </tr> <tr> <td></td> <td><input type="submit" value="注册"></td> </tr> </table> </form> <body></html>
control.jsp
<%@page import="com.sunny.dao.UserDao"%><%@page import="com.sunny.daoimpl.UserDaoImpl"%><%@page import="com.sunny.entity.User"%><%@ page language="java" pageEncoding="UTF-8" %><html> <head> <title>注册处理页面</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("userName"); String pwd = request.getParameter("pwd"); String validatepwd = request.getParameter("validatepwd"); if (name == null || name.equals("")) { request.setAttribute("message", "用户名不能为空"); request.getRequestDispatcher("register.jsp") .forward(request, response); } else if (pwd == null || pwd.equals("")) { request.setAttribute("message", "密码不能为空"); request.getRequestDispatcher("register.jsp") .forward(request, response); }else if(validatepwd==null || validatepwd.equals("")){ request.setAttribute("message","确认密码不能为空"); } else if (!validatepwd.equals(pwd)) { request.setAttribute("message", "两次输入的密码不一致"); request.getRequestDispatcher("register.jsp") .forward(request, response); } else { User user=new User(); user.setUname(name); user.setUpwd(pwd); UserDao userDao=new UserDaoImpl(); int result=userDao.add(user); if (result > 0) { out.println("<script type='text/javascript'>" +"alert('注册成功!马上登录。');location.href='login.jsp';" +"</script>"); } else { request.setAttribute("message", "注册失败,请重新注册。"); request.getRequestDispatcher("register.jsp") .forward(request, response); } } %> </body></html>
package com.sunny.dao;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;/** * 数据库连接与关闭工具类。 */public class BaseDao { private static String driver; // 数据库驱动字符串 private static String url; // 连接URL字符串 private static String user; // 数据库用户名 private static String password; // 用户密码 Connection conn = null;// 数据连接对象 static{//静态代码块,在类加载的时候执行 init(); } /** * 初始化连接参数,从配置文件里获得 */ public static void init(){ Properties params=new Properties(); String configFile = "database.properties";//配置文件路径 //加载配置文件到输入流中 InputStream is=BaseDao.class.getClassLoader().getResourceAsStream(configFile); try { //从输入流中读取属性列表 params.load(is); } catch (IOException e) { e.printStackTrace(); } //根据指定的获取对应的值 driver=params.getProperty("driver"); url=params.getProperty("url"); user=params.getProperty("user"); password=params.getProperty("password"); } /** * 获取数据库连接对象。 */ public Connection getConnection() { if(conn==null){ // 获取连接并捕获异常 try { Class.forName(driver); conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace();// 异常处理 } } return conn;// 返回连接对象 } /** * 关闭数据库连接。 * @param conn 数据库连接 * @param stmt Statement对象 * @param rs 结果集 */ public void closeAll(Connection conn, Statement stmt, ResultSet rs) { // 若结果集对象不为空,则关闭 if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } // 若Statement对象不为空,则关闭 if (stmt != null) { try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } } // 若数据库连接对象不为空,则关闭 if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * 增、删、改的操作 * @param sql 预编译的 SQL 语句 * @param param 预编译的 SQL 语句中的‘?’参数的字符串数组 * @return 影响的行数 */ public int executeUpdate(String preparedSql, Object[] param) { PreparedStatement pstmt = null; int num = 0; conn = getConnection(); try { pstmt = conn.prepareStatement(preparedSql); if (param != null) { for (int i = 0; i < param.length; i++) { pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数 } } num = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally{ closeAll(conn, pstmt, null); } return num; } public ResultSet executeQuery(String sql,Object[] param) throws Exception{ PreparedStatement pstmt = null; ResultSet rs=null; conn = getConnection(); try { pstmt = conn.prepareStatement(sql); if (param != null) { for (int i = 0; i < param.length; i++) { pstmt.setObject(i + 1, param[i]); // 为预编译sql设置参数 } } rs= pstmt.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } finally{ closeAll(conn, pstmt, rs); } return rs; }}
package com.sunny.daoimpl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import com.sunny.dao.BaseDao;import com.sunny.dao.UserDao;import com.sunny.entity.User;public class UserDaoImpl extends BaseDao implements UserDao { @Override public int add(User user) { int result=0; Connection connection=null; connection=this.getConnection(); String sql="insert into user (uname,upwd) values (?,?)"; Object [] param=new Object[]{user.getUname(),user.getUpwd()}; result=this.executeUpdate(sql, param); return result; } @Override /** * 根据对象查找对象 */ public User find(User user) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; User user2=null; try { conn = this.getConnection(); String sql = "select uname,upwd from user where uname = ? and upwd=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, user.getUname()); pstmt.setString(2, user.getUpwd()); rs = pstmt.executeQuery(); //获取user对象的信息 while(rs.next()) { //将获取到的user对象的属性赋给一个新的对象user2 user2 = new User(); user2 .setUname(rs.getString("uname")); user2.setUpwd(rs.getString("upwd")); } } catch (SQLException e) { e.printStackTrace(); } finally { this.closeAll(conn, pstmt, rs); } return user2; }}
阅读全文
1 0
- jsp与数据库
- JSP与数据库
- JSP与数据库交流
- jsp与数据库
- JSP与数据库链接
- 【JSP】JSP与oracle数据库交互案例
- JSP与数据库建立连接
- jSP与数据库的连接
- JSP联系XML与数据库
- jsp数据库基础之---jsp与MySQL数据库的连接
- JSP与数据库的连接方法
- jsp与sql2000数据库的连接
- jsp与各种数据库的连接
- JSP与数据库的连接小结
- 浅谈JSP与数据库的交互
- jsp与数据库的链接编码问题
- JSP与数据库应用(I)
- JSP与数据库应用(II)
- Android N的Audio系统(二)
- RabbitMQ消息队列+spring监听mq服务器多个ip,接收消费mq消息(二)
- 51nod1347 旋转字符串
- (四)初探反应器(event_base)
- 记一次Http问题排查
- jsp与数据库
- [caffe笔记007]:在新版caffe中实现Holistically-Nested Edge Detection
- jQuery--动画篇
- Photographic Image Synthesis with Cascaded Refinement Networks(由语义分割图生成逼真街景图)
- 群论学习之A5群中[5]类和[311]类和[221]类和[11111]类都有多少个?
- POJ
- .NET Core 2.0迁移技巧之web.config配置文件
- MFC中绘制动态曲线
- MyBatis框架解决字段名与实体类属性名不一致的情况