- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
实现流程和思路
项目视频演示
设置数据库studenttest
①、创建user表
创建项目
jsp页面
登录界面login.jsp
主界面mainPage.jsp
添加学生信息AddUser.jsp
修改学生信息change.jsp
Servlet下
登录LoginServlet
查找SearchServlet
删除DeleteServlet
修改ChangeServlet
添加addServlet
Service下
FileServic接口下
FileServiceImpl实现类下
pojo下
User学生类
Dao下
FileDao接口下
FileDaoImpl实现类下
utils下
JDBCUtils
创建jsp页面,然后转发到servlet下,接受jsp页面传过来的值,拿着这些值去访问service服务层接口,服务层写个实类,这个实现类去服务层的接口,然后这个服务层的实现类再去访问dao层的接口,在持久层dao中在编写一个dao层接口的实现类去实现dao层的接口,最后在dao层的的实现类去访问数据库。
图示:
学生管理系统项目演示
其中包括设置删除的u_isdelete(0是存在,1是删除)
create table user(
u_name varchar(20),
u_id int primary key auto_increment,
u_pwd varchar(20) ,
u_phone varchar(20) not null,
u_role int not null,
u_isdelete int not null
)
charset=utf8;
项目界面总览
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<base href="<%=basepath %>"/>
<meta charset="utf-8"/>
<title>登录界面</title>
<%-- css代码--%>
<style>
#fame_{
width: 500px;
height: 400px;
margin-left: 600px;
margin-top: 200px;
background-color: aqua;
/*边框黑色 实线 2像素*/
border: black solid 2px;
}
#inner_{
margin-left: 140px;
margin-top: 160px;
}
</style>
</head>
<body>
<h1 align="center" style="color: red">登录界面</h1>
<div id="fame_" >
<div id="inner_">
用户:<input type="text" id="u_id">
<b> <span id="span1" style="color: crimson">${error}</span></b><br><br>
密码:<input type="password" id="u_pwd">
<b> <span id="span2" style="color: crimson"></span></b><br><br>
<button id="btn01">重置</button>
<button id="btn02" style="margin-left: 20px">登录</button><br>
<script>
<%-- 这里写js代码验证账号密码值是否为空--%>
window.onload=function() {
document.getElementById("btn02").onclick = function () {
let id_value = document.getElementById("u_id").value
if (id_value == null || id_value == "") {
document.getElementById("span1").innerHTML = "用户名不能为空"
return;
}
//判断密码是否为空
let pwd = document.getElementById("u_pwd").value
if (pwd == null || pwd == "") {
document.getElementById("span2").innerHTML = "密码不能为空"
return;
}
//跳转到servlet
window.location.href = "login?u_id=" + id_value + "&u_pwd=" + pwd;
}
//在次点击清空错误信息
document.getElementById("u_id").onfocus = function () {
document.getElementById("span1").innerText = ""
}
document.getElementById("u_pwd").onfocus = function () {
document.getElementById("span2").innerText = ""
}
//清空内容
document.getElementById("btn01").onclick=function () {
document.getElementById("u_id").value="";
document.getElementById("u_pwd").value="";
}
}
</script>
</div>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%--JSTL语法,可以使用user.getName()方法--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>主界面</title>
<style>
#head_{
height: 100px;
width: 100%;
background-color: darkcyan;
}
#personFile{
width: 120px;
height: 50px;
padding-top: 25px;
margin-left: 90%;
}
#search{
height: 70px;
width: 100%;
background-color: aqua;
}
#inner_s{
/*width: 200px;
height: 40px;
padding-right: 70%;
padding-top: 50%;*/
width: 30%;
padding-top:25px;
padding-left: 45%;
}
#table{
margin-left: 30%;
margin-top: 30px;
}
td{
text-align: center;
height: 20px;
width: 150px;
border: darkcyan 2px solid;
padding:6px;
}
</style>
</head>
<body>
<div id="head_">
<h2>
<div id="personFile" >
名字:<span style="color: red">${user.getU_name()}</span><br>
编号:<span style="color: red">${user.getU_id()}</span>
</div>
</h2>
</div>
<div id="search">
<div id="inner_s">
<input type="text" style="font-size: 20px; height: 26px;width: 190px " id="fileId">
<button style="font-size: 18px; height: 28px;" id="cx"margin-left: 30px>查询</button>
<button style="font-size: 18px; height: 28px;" id="addUser" margin-left: 100px>添加</button>
<button style="font-size: 18px; height: 28px;" id="deleteUser">删除</button>
<button style="font-size: 18px; height: 28px;" id="changeUser">修改</button>
<b> <span style="color: crimson">${tip}</span></b>
</div>
</div>
<script>
//提交
document.getElementById("cx").onclick=function(){
let v = document.getElementById('fileId').value;
//访问服务器searchServlet
window.location.href = "searchServlet?v=" + v+"&id=${user.getU_name()}";
}
//删除
document.getElementById("deleteUser").onclick=function () {
let v =document.getElementById('fileId').value;
window.location.href = "deleteServlet?v="+v+"&id=${user.getU_name()}";
}
// 添加跳转到AddUser.jsp界面
document.getElementById("addUser").onclick=function () {
window.location = "AddUser.jsp";
}
//修改,跳转到change.jsp
document.getElementById("changeUser").onclick=function () {
let v =document.getElementById('fileId').value;
window.location.href = "change.jsp";
}
</script>
<div>
<table id="table" style="height: 30px;width: 700px;border: black 1px solid;border-collapse:collapse;">
<tr style="font-weight: bold" >
<td>学生id</td>
<td>学生名字</td>
<td>学生电话</td>
<td>学生成绩</td>
<td>学生品行</td>
</tr>
<%--jstl语法遍历,var是一个指指针--%>
<c:forEach items="${arr}" var="item">
<tr>
<td>${item.getU_id()}</td>
<td>${item.getU_name()}</td>
<td>${item.getU_phone()}</td>
<td style="color: deeppink"> 优秀</td>
<td style="color: red">良好</td>
</td>
</tr>
</c:forEach>
</table>
</div>
<div>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<base href="<%=basepath %>"/>
<meta charset="utf-8"/>
<title>添加信息界面</title>
</head>
<style>
#div1{
width: 400px;
height: 300px;
margin-left: 600px;
margin-top: 200px;
background-color:deepskyblue;
/*边框黑色 实线 2像素*/
border: black solid 2px;
}
</style>
<body>
<h2 align="center" >添加学生信息</h2><br><br>
<div id="div1" >
<tr> 学生id:<input type="text" id="u_id" >
<span id="span01" style="color: red"></span> <br><br></tr>
<tr> 姓名:<input type="text" id="u_name" ><br><br></tr>
<tr> 密码:<input type="password" id="u_pwd"><br><br></tr>
<tr> 电话号码:<input type="text" id="u_phone"><br><br></tr>
<button id="btn3">提交</button>
<script>
document.getElementById("btn3").onclick=function(){
//获取填写的数据
let u_id=document.getElementById("u_id").value;
let u_name=document.getElementById("u_name").value;
let u_pwd=document.getElementById("u_pwd").value;
let u_phone=document.getElementById("u_phone").value;
if(u_id==null||u_id==""||u_name==null||u_name==""){
document.getElementById("span01").innerText="id或名字为为空"
}else{
//把数据发送到后端,发送到servlet的addUser路径下
window.location.href="addUser?u_id="+u_id+"&u_pwd="+u_pwd+"&u_name="+u_name+"&u_phone="+u_phone+"&id=${user.getU_name()}";
}
}
document.getElementById("u_id").onfocus=function () {
document.getElementById("span01").innerText=""
}
</script>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<base href="<%=basepath %>"/>
<meta charset="utf-8"/>
<title>修改界面</title>
</head>
<style>
#div1{
width: 400px;
height: 300px;
margin-left: 600px;
margin-top: 200px;
background-color:deepskyblue;
/*边框黑色 实线 2像素*/
border: black solid 2px;
}
</style>
<body>
<h2 align="center" >修改学生信息</h2><br><br>
<div id="div1" >
修改的学生id:<input type="text" id="u_id" >
<span id="span01" style="color: red"></span> <br><br>
姓名:<input type="text" id="u_name" ><br><br>
密码:<input type="password" id="u_pwd"><br><br>
电话号码:<input type="text" id="u_phone"><br><br>
<button id="btn3">提交</button>
<script>
//获取填写数据
document.getElementById("btn3").onclick=function(){
let u_id=document.getElementById("u_id").value;
let u_name=document.getElementById("u_name").value;
let u_pwd=document.getElementById("u_pwd").value;
let u_phone=document.getElementById("u_phone").value;
//判断非空
if(u_id==null||u_id==""||u_name==null||u_name==""){
document.getElementById("span01").innerText="id或名字为为空"
}else{
//不为空就转发到后端
window.location.href="changeUser?u_id="+u_id+"&u_pwd="+u_pwd+"&u_name="+u_name+"&u_phone="+u_phone+"&id=${user.getU_name()}";
}
}
//再次点击清除错误信息提示
document.getElementById("u_id").onfocus=function () {
document.getElementById("span01").innerText=""
}
</script>
</div>
</body>
</html>
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
import com.StudentTest.service.LoginService;
import com.StudentTest.service.LoginServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String u_id=null,u_pwd=null;
User u = new User();
try{//非数字异常判断
u_id = req.getParameter("u_id");
u_pwd = req.getParameter("u_pwd");
u.setU_id(Integer.valueOf(u_id));
u.setU_pwd(u_pwd);
}catch (Exception e){
req.setAttribute("error", "你输入的不是数字");
//错误就回到主界面
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
//实现登录服务层的业务逻辑层,从服务层service到持久层dao
LoginService ls = new LoginServiceImpl();
//返回user这样可以看到登录的用户是谁
/**
* 如果user为空说明账号密码不一致,跳转到登录界面
* 不为空说明账号密码一致,跳转到主界面
*/
User user = null;
try {
user = ls.loginService(u);
} catch (Exception e) {
e.printStackTrace();
}
if (user != null) {
FileService fs=new FileServiceImpl();
//查看所有数据
ArrayList<User> arrUser=fs.getAllStudent();
//将这些数据转发到前端
req.setAttribute("arr",arrUser);
req.setAttribute("user", user);
//跳转到主界面
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
} else {
req.setAttribute("error", "密码不匹配");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
}
}
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
import com.StudentTest.service.LoginService;
import com.StudentTest.service.LoginServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String u_id=null,u_pwd=null;
User u = new User();
try{//非数字异常判断
u_id = req.getParameter("u_id");
u_pwd = req.getParameter("u_pwd");
u.setU_id(Integer.valueOf(u_id));
u.setU_pwd(u_pwd);
}catch (Exception e){
req.setAttribute("error", "你输入的不是数字");
//错误就回到主界面
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
//实现登录服务层的业务逻辑层,从服务层service到持久层dao
LoginService ls = new LoginServiceImpl();
//返回user这样可以看到登录的用户是谁
/**
* 如果user为空说明账号密码不一致,跳转到登录界面
* 不为空说明账号密码一致,跳转到主界面
*/
User user = null;
try {
user = ls.loginService(u);
} catch (Exception e) {
e.printStackTrace();
}
if (user != null) {
FileService fs=new FileServiceImpl();
//查看所有数据
ArrayList<User> arrUser=fs.getAllStudent();
//将这些数据转发到前端
req.setAttribute("arr",arrUser);
req.setAttribute("user", user);
//跳转到主界面
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
} else {
req.setAttribute("error", "密码不匹配");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
}
}
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/deleteServlet")
public class DeleteServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String del=req.getParameter("v");
//获取登录人信息
String adm=req.getParameter("id");
//访问service层,在service层创建对应的方法
FileService fs=new FileServiceImpl();
ArrayList<User> list=fs.getAllStudent();
//获取登录人员信息
User u = fs.getAdmint(adm);
//service层调用方法,转到dao层执行sql语句
boolean user=fs.delUser(del);
String tip="";
if(user){
tip="删除成功";
}else {
tip="删除失败";
}
//发送到前端
req.setAttribute("user", u);
req.setAttribute("arr", list);
req.setAttribute("tip", tip);
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
}
}
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/changeUser")
public class ChangeServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String id=req.getParameter("u_id");
String pwd=req.getParameter("u_pwd");
String phone=req.getParameter("u_phone");
String name=req.getParameter("u_name");
FileService fs=new FileServiceImpl();
User user=new User(name,Integer.valueOf(id),pwd,phone);
String adm=req.getParameter("id");
//获取登录人员信息
User u = fs.getAdmint(adm);
System.out.println(u);
boolean flag=fs.changeUser( user);
ArrayList<User> list=fs.getAllStudent();
String tip="";
if (flag){
tip="修改成功";
}else{
tip="修改失败";
}
//发送到前端
req.setAttribute("tip",tip);
req.setAttribute("user", u);
req.setAttribute("arr", list);
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
}
}
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
@WebServlet("/addUser")
public class AddServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String id=req.getParameter("u_id");
String pwd=req.getParameter("u_pwd");
String phone=req.getParameter("u_phone");
String name=req.getParameter("u_name");
FileService fs=new FileServiceImpl();
User user=new User(name,Integer.valueOf(id),pwd,phone);
String adm=req.getParameter("id");
//获取登录人员信息
User u = fs.getAdmint(adm);
System.out.println(u);
boolean flag=fs.addUser( user);
ArrayList<User> list=fs.getAllStudent();
String tip="";
if (flag){
tip="添加成功";
}else{
tip="添加失败";
}
//发送到前端
req.setAttribute("tip",tip);
req.setAttribute("user", u);
req.setAttribute("arr", list);
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
}
}
package com.StudentTest.service;
import com.StudentTest.pojo.User;
import java.util.ArrayList;
public interface FileService {
ArrayList<User> getAllStudent();
User searchIdName(String v);
User getAdmint(String id);
boolean delUser(String del);
boolean addUser(User user);
boolean changeUser(User user);
User loginService(User u) throws Exception;
}
package com.StudentTest.service;
import com.StudentTest.Dao.FileDao;
import com.StudentTest.Dao.FileDaoImpl;
import com.StudentTest.pojo.User;
import java.util.ArrayList;
public class FileServiceImpl implements FileService {
FileDao fd= new FileDaoImpl();
@Override
public ArrayList<User> getAllStudent() {
/**
* 访问持久层
*/
return fd.getAllStudent();
}
@Override
public User searchIdName(String v) {
return fd.searchIdName( v);
}
@Override
public User getAdmint(String id) {
return fd.getAdmint(id);
}
@Override
public boolean delUser(String del) {
return fd.delUser(del );
}
@Override
public boolean addUser(User user) {
return fd.addUser(user);
}
@Override
public boolean changeUser(User user) {
return fd.changeUser(user);
}
@Override
public User loginService(User u) throws Exception {
return fd.loginDao( u);
}
}
package com.StudentTest.pojo;
public class User {
private String u_name;
private int u_id;
private String u_pwd;
private String u_phone;
private int u_role;
private int u_isdelete;
public User() {
}
public User(String u_name, int u_id, String u_pwd, String u_phone) {
this.u_name = u_name;
this.u_id = u_id;
this.u_pwd = u_pwd;
this.u_phone = u_phone;
}
public User(String u_name, int u_id, String u_pwd, String u_phone, int u_role, int u_isdelete) {
this.u_name = u_name;
this.u_id = u_id;
this.u_pwd = u_pwd;
this.u_phone = u_phone;
this.u_role = u_role;
this.u_isdelete = u_isdelete;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public int getU_id() {
return u_id;
}
public void setU_id(int u_id) {
this.u_id = u_id;
}
public String getU_pwd() {
return u_pwd;
}
public void setU_pwd(String u_pwd) {
this.u_pwd = u_pwd;
}
public String getU_phone() {
return u_phone;
}
public void setU_phone(String u_phone) {
this.u_phone = u_phone;
}
public int getU_role() {
return u_role;
}
public void setU_role(int u_role) {
this.u_role = u_role;
}
public int getU_isdelete() {
return u_isdelete;
}
public void setU_isdelete(int u_isdelete) {
this.u_isdelete = u_isdelete;
}
@Override
public String toString() {
return "User{" +
"u_name='" + u_name + '\'' +
", u_id=" + u_id +
", u_pwd='" + u_pwd + '\'' +
", u_phone='" + u_phone + '\'' +
", u_role=" + u_role +
", u_isdelete=" + u_isdelete +
'}';
}
}
package com.StudentTest.Dao;
import com.StudentTest.pojo.User;
import java.util.ArrayList;
public interface FileDao {
ArrayList<User> getAllStudent();
User searchIdName(String v);
User getAdmint(String id);
boolean delUser(String del);
boolean addUser(User user);
boolean changeUser(User user);
User loginDao(User u) throws Exception;
}
package com.StudentTest.Dao;
import com.StudentTest.pojo.User;
import com.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class FileDaoImpl implements FileDao {
@Override
public User loginDao(User u) throws SQLException, ClassNotFoundException {
/**在这写数据库的内容
* 获取链接对象,获取sql语句
*/
int id=u.getU_id();
String pwd=u.getU_pwd();
// //访问数据库
// User user=new User("慧宝",1001,"520","15216123240",1,0);
Connection connection= JDBCUtils.getConnection();
PreparedStatement pre =null;
ResultSet res =null;
String sql="select *from user where u_id="+id+" and u_pwd='"+pwd+"'";
try{
pre= connection.prepareStatement(sql);
res=pre.executeQuery();
User user=new User();
while(res.next()){
user.setU_id(res.getInt("u_id"));
user.setU_name(res.getString("u_name"));
user.setU_pwd(res.getString("u_pwd"));
user.setU_phone(res.getString("u_phone"));
user.setU_role(res.getInt("u_role"));
user.setU_isdelete(res.getInt("u_isdelete"));
return user;
}
} catch (SQLException s){
s.printStackTrace();
}
return null;
}
//获取所有学生信息
@Override
public ArrayList<User> getAllStudent() {
ArrayList<User> arr=new ArrayList<>();
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet res =null;
//u_role为1表示普通用户,为0为管理员,u_isdelete为0表示还在,为1表示已删除
String sql="select *from user where u_role=1 and u_isdelete=0 ";
try{
pre= connection.prepareStatement(sql);
res=pre.executeQuery();
while(res.next()){
User user=new User();
user.setU_id(res.getInt("u_id"));
user.setU_name(res.getString("u_name"));
user.setU_pwd(res.getString("u_pwd"));
user.setU_phone(res.getString("u_phone"));
user.setU_role(res.getInt("u_role"));
user.setU_isdelete(res.getInt("u_isdelete"));
arr.add(user);
}
return arr;
} catch (SQLException s){
s.printStackTrace();
}
return null;
}
//找指定学生
@Override
public User searchIdName(String v) {
/**
* sql
*/
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet res =null;
//u_role为1表示普通用户,为0为管理员,u_isdelete为0表示还在,为1表示已删除
String sql="select *from user where u_id ='"+v+"'or+ u_name ='"+v+"' ";
try{
pre= connection.prepareStatement(sql);
res=pre.executeQuery();
while(res.next()){
User user=new User();
user.setU_id(res.getInt("u_id"));
user.setU_name(res.getString("u_name"));
user.setU_pwd(res.getString("u_pwd"));
user.setU_phone(res.getString("u_phone"));
user.setU_role(res.getInt("u_role"));
user.setU_isdelete(res.getInt("u_isdelete"));
return user;
}
} catch (SQLException s){
s.printStackTrace();
}
return null;
}
//获取登录人员信息
@Override
public User getAdmint(String id) {
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet res =null;
String sql="select *from user where u_id ='"+id+"'or+ u_name ='"+id+"' ";
try{
pre= connection.prepareStatement(sql);
res=pre.executeQuery();
while(res.next()){
User user=new User();
user.setU_id(res.getInt("u_id"));
user.setU_name(res.getString("u_name"));
return user;
}
} catch (SQLException s){
s.printStackTrace();
}
return null;
}
//删除信息
@Override
public boolean delUser(String del) {
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet ress=null;
int res=0;
String sql="delete from user where u_name ='"+del+"'or + u_id ='"+del+"' ";
try{
pre= connection.prepareStatement(sql);
res=pre.executeUpdate();
if(res>0)return true;
} catch (SQLException s){
s.printStackTrace();
}
return false;
}
//添加学生
@Override
public boolean addUser(User user) {
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet ress=null;
String name=user.getU_name();
int id=user.getU_id();
String pwd=user.getU_pwd();
String phone=user.getU_phone();
int res=0;
//"insert into user values('"+user.getU_name()+"','"+user.getU_id()+"','"+user.getU_pwd()+"','"+user.getU_phone()+"')"
String sql="insert into user(u_name,u_id,u_pwd,u_phone)\n" +
"values('"+name+"','"+id+"','"+pwd+"','"+phone+"')";
try{
pre= connection.prepareStatement(sql);
res=pre.executeUpdate();
System.out.println(res);
if(res>=0)return true;
} catch (SQLException s){
s.printStackTrace();
}
return false;
}
//修改信息
@Override
public boolean changeUser(User user) {
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet ress=null;
String name=user.getU_name();
int id=user.getU_id();
String pwd=user.getU_pwd();
String phone=user.getU_phone();
int res=0,res1=0;
String sql1="delete from user where u_id="+id;
try{
pre= connection.prepareStatement(sql1);
res=pre.executeUpdate();
if (res>0){
String sql="insert into user(u_name,u_id,u_pwd,u_phone)\n" +
"values('"+name+"','"+id+"','"+pwd+"','"+phone+"')";
pre= connection.prepareStatement(sql);
res1=pre.executeUpdate();
}
System.out.println(res);
if(res1>0)return true;
} catch (SQLException s){
s.printStackTrace();
}
return false;
}
}
package com.utils;
import java.sql.*;
public class JDBCUtils {
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/studenttest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
String username="root";
String password="123456";
Connection conn= DriverManager.getConnection(url,username,password);
return conn;
}
public static void release(Statement stmt,Connection conn){
if(stmt!=null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
stmt=null;
}
if(conn!=null){
try{
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
conn=null;
}
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try{
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
rs=null;
}
release(stmt,conn);
}
}
还有写web-inf下lib下的jar包
在parser.h中: typedef enum _SEX {MALE = 'M', FEMALE = 'F', OTHER = 'O'} SEX; struct course { char gra
共有三个实体: School, teacher and student 有一些规则: 一个学生只能属于一位老师 一名学生只能属于一所学校 一名教师可以属于一所或多所学校 这意味着我的表中有这些关系:
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this qu
我正在构建一个涉及学生和顾问的 Web 应用程序。学生将选择他的独立学习,顾问将对其进行监督。我正在努力为我的程序找到正确的关系。 基本上我的应用程序中的参与者是。 a Student can sel
支持Python的IDE有IPython、Aptana Studio(在Eclipse的基础上加插件集改的)、PyCharm(由 JetBrains 打造的一款 Python IDE,支持 macO
我对 sql 和 Stack Overflow 非常陌生。我希望有人可以帮助我解决这个问题。我的查询应该显示销售额超过 200,000 美元的类别的总销售额和已售商品总数。我已经研究这个查询一个小时了
public class Menu extends Activity { /** Called when the activity is first created. */ public
当我在 R 中运行学生 t-分布时,我得到以下结果: > pt(1.849, 2, lower.tail=FALSE) [1] 0.1028493 当我运行 Apache Commons Math 库
我的机器人实验室正在寻找程序员来从事我们目前的一些项目。 我们确定了要求(主要是 C++ 和 openGL 和 3D 经验),但由于明显的资金限制,我们无力聘请优秀的开发人员。相反,我们将满足于有才华
我正在努力完成一个使用替换密码来编码和解码 txt 文件的程序。经过相当多的工作,我的程序几乎完成了。该程序首先询问用户是否要对文件进行编码或解码。回答这个问题后,程序会提示用户输入正在输入的文件的名
我是一个刚开始编程的菜鸟,我想知道为什么我会收到一条错误消息:非静态数据成员“Lavirint::n”的使用无效? class Lavirint{ private: int n, m;
该项目的目标是创建四个类:Student 类、GradStudent 类、Manager 类和 GUI 类。 GUI 中有两个单选按钮:一个用于学生,一个用于研究生。根据选择的对象,Manager 类
我正在尝试向学生、教师和家长 Activity 实现抽屉导航 Activity ,但它不起作用,请指导我如何在登录后向不同角色的用户添加抽屉导航。 最佳答案 您需要在 中自定义布局像这样:
我是 JS 的新手,甚至是 Jquery 和 fullcalendar 的新手。我创建了一个单独在页面上运行良好的日历。我正在尝试将此代码与另一个页面结合使用,但我无法让日历呈现。 首先我包含一些文件
我正在开发一个简单的 Linux Shell,它可以 fork() 并调用 execvp(),但我最近添加了不需要 fork() 的“内置函数”。 这是执行处理: if (strcmp(cmd
我在使用分配给我的这个初学者 Java 程序时遇到了麻烦,我是 Java 的新手,而且我在使用这个特定程序时遇到了很多麻烦。这些是说明: Your program should prompt user
在下面的代码块中,有几个(故意的)错误,我的任务是找到它们并解释这些错误是否会导致编译代码时出现问题,或者至少会导致一些逻辑问题。 public class Person { private St
我想加载一个文件,将其内容拆分为数组,然后将类应用到内容。 class Student def initialize( name, grade ) @name = name
我有 3 个实体:导师、学生、类(class) 导师教授许多类(class) 学生可以被分配到由多名导师教授的许多类(class) 导师和学生需要能够登录系统 我想使用下表来表示此设计: users
我想弄清楚这里的填充是否可调 这是 HTML: {block:IfHeaderImage} {/block:IfHeaderImage} {block:IfNot
我是一名优秀的程序员,十分优秀!