Servlet编程实例 续4
Posted siwuxie095
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet编程实例 续4相关的知识,希望对你有一定的参考价值。
---------------siwuxie095
JSP+Servlet+JDBC
继续完善登录实例,将校验逻辑改为:从数据库中获取用户信息进行校验
数据库准备
在 Navicat for mysql 中创建连接:user_conn,创建数据库:user_db,
创建表:user,并内置数据:
JDBC 驱动准备
下载 MySQL 的 JDBC 驱动,下载链接:
https://dev.mysql.com/downloads/connector/j/
mysql-connector-java-5.1.41.zip 解压后一览:
将 mysql-connector-java-5.1.41-bin.jar 放入 WEB-INF 的 lib 文件夹中,
选中该 jar 文件并执行操作:右键->Build Path->Add to Build Path
编写代码
点击选择 src,右键->New->File,创建文件:dbconfig.properties
此时,工程结构目录一览:
后端代码:
ConnectionFactory.java:
package com.siwuxie095.util;
import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties;
//数据库连接工厂类 public class ConnectionFactory {
//四个成员变量用于保存从属性文件中读取到的数据库配置信息 private static String driver; private static String dburl; private static String user; private static String password;
//定义ConnectionFactory类型的成员变量 private static final ConnectionFactory factory=new ConnectionFactory();
//定义Connection类型的成员变量,用于保存数据库连接 private Connection conn;
/** * 用 static 声明一个静态代码块 * 静态代码块用于初始化类,可以为类的属性赋值 * * 当JVM加载类时,会执行其中的静态代码块 * * 因为是在加载类的过程中执行的,所以静态代码块只会执行一次 * * 这里是从属性文件中读取相关的配置信息 */ static{
/** * 创建一个 Properties 对象,Properties 在 java.util 包中, * 继承自 Hashtable 类,可以用来保存属性文件中的键值对, * Properties 的方法专门用于处理属性文件中的键值对 */ Properties prop=new Properties();
try {
/** * 获取属性文件中的内容: * 首先获取当前类的类加载器,使用类加载器的getResourceAsStream()方法, * 读取属性文件中内容,并读取到一个输入流中 */ InputStream in=ConnectionFactory.class.getClassLoader() .getResourceAsStream("dbconfig.properties");
//从输入流中读取属性列表,即键值对 prop.load(in);
} catch (Exception e) { System.out.println("========配置文件读取错误========"); }
//将读取到的值赋值给成员变量 driver=prop.getProperty("driver"); dburl=prop.getProperty("dburl"); user=prop.getProperty("user"); password=prop.getProperty("password"); //属性文件的加载---编写完成 }
//定义一个默认构造方法(空的构造方法) //构造方法私有化是单例化一个类的第一步 private ConnectionFactory(){
}
//定义getInstance()方法,用来获取一个ConnectionFactory的实例 //单例模式,保证在程序运行期间,只有一个ConnectionFactory实例存在 public static ConnectionFactory getInstance() { return factory; }
//创建一个获取数据库连接的方法 makeConnection() public Connection makeConnection() {
try {
Class.forName(driver); conn=DriverManager.getConnection(dburl,user,password);
} catch (Exception e) { e.printStackTrace(); }
return conn;
}
} |
UserEntity.java:
package com.siwuxie095.entity;
public abstract class UserEntity { protected String userName;
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
} |
UserEntityExtd.java:
package com.siwuxie095.entity.extd;
import com.siwuxie095.entity.UserEntity;
public class UserEntityExtd extends UserEntity { private String password;
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
@Override public String toString() { return "UserEntityExtd [password=" + password + ", userName=" + userName + "]"; }
} |
UserDao.java:
package com.siwuxie095.dao;
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException;
import com.siwuxie095.entity.extd.UserEntityExtd;
public interface UserDao { public void save(Connection conn,UserEntityExtd user) throws SQLException; public void update(Connection conn,UserEntityExtd user) throws SQLException; public void delete(Connection conn,UserEntityExtd user) throws SQLException; public ResultSet get(Connection conn,UserEntityExtd user) throws SQLException; } |
UserDaoImpl.java:
package com.siwuxie095.dao.impl;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import com.siwuxie095.dao.UserDao; import com.siwuxie095.entity.extd.UserEntityExtd;
public class UserDaoImpl implements UserDao {
@Override public void save(Connection conn, UserEntityExtd user) throws SQLException { PreparedStatement ps=conn .prepareStatement("insert into user(uname,upwd) values(?,?)"); ps.setString(1, user.getUserName()); ps.setString(2, user.getPassword()); ps.execute(); }
@Override public void update(Connection conn, UserEntityExtd user) throws SQLException { String updateSql="update user set uname=?,upwd=? where uname=?"; PreparedStatement ps=conn.prepareStatement(updateSql); ps.setString(1, user.getUserName()); ps.setString(2, user.getPassword()); ps.execute(); }
@Override public void delete(Connection conn, UserEntityExtd user) throws SQLException { PreparedStatement ps=conn.prepareStatement("delete from user where uname=?"); ps.setString(1, user.getUserName()); ps.execute(); }
@Override public ResultSet get(Connection conn, UserEntityExtd user) throws SQLException { PreparedStatement ps=conn .prepareStatement("select * from user where uname=? and upwd=?"); ps.setString(1, user.getUserName()); ps.setString(2, user.getPassword()); return ps.executeQuery(); }
} |
CheckLoginService.java:
package com.siwuxie095.service;
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException;
import com.siwuxie095.dao.UserDao; import com.siwuxie095.dao.impl.UserDaoImpl; import com.siwuxie095.entity.extd.UserEntityExtd; import com.siwuxie095.util.ConnectionFactory;
public class CheckLoginService {
private UserDao userDao=new UserDaoImpl();
public boolean check(UserEntityExtd user){
Connection conn=null;
try { conn=ConnectionFactory.getInstance().makeConnection(); conn.setAutoCommit(false); ResultSet rs=userDao.get(conn, user); while (rs.next()) { return true; } } catch (SQLException e) { e.printStackTrace(); try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } }finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
return false; }
} |
CheckLoginServlet.java:
package com.siwuxie095.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.siwuxie095.entity.extd.UserEntityExtd; import com.siwuxie095.service.CheckLoginService;
public class CheckLoginServlet extends HttpServlet { private static final long serialVersionUID = 1L;
private CheckLoginService cls=new CheckLoginService();
public CheckLoginServlet() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); Servlet编程实例 续3 |