基于Javaweb、MySQL、jsp的简单的学生信息管理系统,查看所有学生信息,登录验证,增加学生信息,修改和删除就不写了,就是在表单中增加两个链接跳转到servlet控制Mysql
总览
数据库
1.连接池
package utils;
import java.sql.*;
public class JDBCUtils {
static final String DRIVERNAME="com.mysql.cj.jdbc.Driver";
static final String URL = "jdbc:mysql://127.0.0.1:3306/class?useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
static final String USER="root";
static final String PASSWORD="qwer1234";
static {
try {
Class.forName(DRIVERNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
static Connection connection = null;
static PreparedStatement statement = null;
static ResultSet resultSet = null;
public static Connection getconnection(){
try {
connection = DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
public static ResultSet query(String sql,Object... objs){
//预处理
getconnection();
try {
statement = connection.prepareStatement(sql);
//判断objs是否为空或为0
if(objs != null && objs.length > 0){
for (int i = 0; i < objs.length; i++) {
statement.setObject(i+1,objs[i]);
}
}
//查询操作
resultSet = statement.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return resultSet;
}
public static void add(String sql){
getconnection();
try {
statement = connection.prepareStatement(sql);
System.out.println("影响行数:"+ statement.executeUpdate(sql));
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void delete(String sql){
getconnection();
try {
statement = connection.prepareStatement(sql);
System.out.println("executeUpdate:"+ statement.executeUpdate(sql));
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void update(String sql){
getconnection();
try {
statement = connection.prepareStatement(sql);
System.out.println("executeUpdate:"+ statement.executeUpdate(sql));
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void free(){
if (null != resultSet){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (null != statement){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (null != connection){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
对登陆界面的控制类:
package controller;
import dao.StudentDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
@WebServlet(name = "LoginServlet",value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设定编码
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=UTF-8");
//获取表单数据
String account = req.getParameter("account");
String password = req.getParameter("password");
System.out.println("account = " + account);
System.out.println("password = " + password);
//七天免密登录
cookie cookie = new cookie("nameinfo", URLEncoder.encode(account+"-"+password,"utf-8") );
//一天免登录,待验证remember
cookie.setMaxAge(24*60*60);
//增加cookie到responce
resp.addcookie(cookie);
String error;
//账号密码的非空校验 trim去空格
if (account == null || account.trim().length() == 0 || password == null || password.trim().length() == 0){
//告知用户不能为空,同时用户依然停留在登录界面
error = "用户名或密码不能为空!";
//资源的跳转: 重定向 转发
//重定向:根据当前的url进行资源的跳转 /表示是web容器的根路径 但是我们自己想在跳转url的同时携带信息
// resp.sendRedirect(req.getContextPath() + "/html
cookie[] cookies = request.getcookies();
String[] s = null;
if (cookies!=null){
for (cookie cookie : cookies) {
if (URLDecoder.decode(cookie.getName(), "utf-8").equals("nameinfo")){
String str = URLDecoder.decode(cookie.getValue(),"utf-8" );
s = str.split("-");
}
}
}
%>


