基本思路:
一个Regist.jsp注册页面,用于收集用户信息,发送请求给控制器Servlet;控制器层Servlet封装模型层对象 jBean,并调用其方法regiser实现用户信息的保存;模型层JavaBean.java 接收控制器层的对象,完成对数据库的操作。
话不多说,上代码。
首先有个数据库访问公共类,以后凡是操作数据库的页面都可以调用。不容多说:
Unilt.java
/** * */package javaBeanServlet;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * @author Administrator * */public class Unilt { private static final String URL = "jdbc:mysql://localhost:3306/userdb"; private static final String USER = "root"; private static final String PASSWORD = "111111"; protected static Statement s = null; protected static ResultSet rs = null; protected static Connection coon = null; public static synchronized Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { coon = DriverManager.getConnection(URL, USER, PASSWORD); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return coon; } public static int executeUpdate(String sql) { int result = 0; try { s = getConnection().createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { result = s.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public static ResultSet executeQuery(String sql) { try { s = getConnection().createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { rs = s.executeQuery(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs ; } public static PreparedStatement executePreparedStatement(String sql) { PreparedStatement ps = null; try { ps = getConnection().prepareStatement(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ps; } public static void rollback() { try { getConnection().rollback(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close() { if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (s != null) { try { s.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (coon != null) { try { coon.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
接下来是一个简单的注册页面:
Register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>Insert title here
Servlet.java
package javaBeanServlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class Servelt */@WebServlet("/Servelt")public class Servelt extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servelt() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置编码格式 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset = utf-8"); /* 实例化JavaBean 的一个对象 jBean ,并通过request.getParameter()获取Regist表单提交的 username、password值 */ JavaBean jBean = new JavaBean(); jBean.setFd_username(request.getParameter("username")); jBean.setFd_password(request.getParameter("password")); jBean.register(jBean); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
JavaBean.java
/** * */package javaBeanServlet;import java.sql.*;import javaBeanServlet.*;import javaBeanServlet.Unilt;/** * @author Administrator * */public class JavaBean { // 依据数据表结构声明成员变量 private String fd_username ; private String fd_password ; // 为属性提供setter和getter方法 public String getFd_username() { return fd_username; } public void setFd_username(String fd_username) { this.fd_username = fd_username; } public String getFd_password() { return fd_password; } public void setFd_password(String fd_password) { this.fd_password = fd_password; } public boolean register(JavaBean javaBean){ String sql = "insert into users(username ,password) values (?,?)"; int result = 0; PreparedStatement ps = Unilt.executePreparedStatement(sql); try { ps.setString(1,javaBean.fd_username); ps.setString(2,javaBean.fd_password); result = ps.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 注册成功,返回true;注册失败,返回false if (result > 0) { System.out.println("注册成功"); return true; } else { System.out.println("注册失败"); return false; } }}
下面是运行的效果:
新手上路,请多指教!