前端页面:连接mysql数据库与login表格中有登陆权限的学生做校验,验证成功进入下一个页面。失败则重新登录。
管理面板:实现信息的增删改查功能
CREATE table student(
sno int PRIMARY key,
sname VARCHAR(12),
sage int,
saddress VARCHAR(20)
);
add.jsp:入口页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="AddStudentServlet" method="post">
学号:<input type="text" name="sno" /><br/>
姓名:<input type="text" name="sname" /><br/>
年龄:<input type="text" name="sage" /><br/>
地址:<input type="text" name="saddress" /><br/>
<input type="submit" value="新增" /><br/>
</form>
</body>
</html>
AddStudentServlet.java:表示层后台
package org.student.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.student.entity.student;
import org.student.service.StudentService;
public class AddStudentServlet extends HttpServlet {
public AddStudentServlet() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//获取add.jsp的4个 值
int no=Integer.parseInt(request.getParameter("sno"));
String name=request.getParameter("sname");
int age=Integer.parseInt(request.getParameter("sage"));
String address=request.getParameter("saddress");
//把4个值封装成对象
student stu = new student(no,name,age,address);
StudentService service = new StudentService();
//调用StudentService的addStudent方法,增加
boolean res = service.addStudent(stu);
response.setContentType("text/html; charset=UTF-8");//java
response.setCharacterEncoding("utf-8");//响应的编码
//===========设置编码在让它响应之前,下面是响应的编码
PrintWriter printWriter = response.getWriter();
if(res) {
//返回的是out对象
printWriter.print("增加成功!");
}else {
printWriter.print("增加失败!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
student.java封装对象的实体类:
package org.student.entity;
public class student {
private int no;
private String name;
private int age;
private String address;
public student() {
}
public student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public student(int no, String name, int age, String address) {
this.no=no;
this.name = name;
this.age = age;
this.address = address;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
studentDao.java:业务逻辑实现
package org.student.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.student.entity.student;
import com.mysql.jdbc.Driver;
//数据访问层:原子性的增删改查
//判断学生是否存在
public class studentDao {
public boolean isExit(int sno) {
return queryStudentByNo(sno)==null?true:false;
}
public boolean addStudent(student stu) {
Connection conn=null;
PreparedStatement prepareStatement=null;
ResultSet rs=null;
try {
Class clazz=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "xxxxx");
conn = driver.connect(url, info);
String sql="insert into student values(?,?,?,?)";
prepareStatement= conn.prepareStatement(sql);
prepareStatement.setInt(1, stu.getNo());
prepareStatement.setString(2, stu.getName());
prepareStatement.setInt(3, stu.getAge());
prepareStatement.setString(4, stu.getAddress());
int res = prepareStatement.executeUpdate();
if(res>0) {
return true;
}else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
try {
if(rs!=null) rs.close();
if(prepareStatement!=null) prepareStatement.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public student queryStudentByNo(int sno) {
int no=-1;
String name=null;
int age=-1;
String address=null;
Connection conn=null;
PreparedStatement prepareStatement=null;
ResultSet rs=null;
try {
Class clazz=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver) clazz.newInstance();
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "xxxxx");
conn = driver.connect(url, info);
String sql="select * from student where sno=?";
prepareStatement= conn.prepareStatement(sql);
prepareStatement.setInt(1, sno);
rs = prepareStatement.executeQuery();
if(rs.next()) {
no = rs.getInt("sno");
name= rs.getString("sname");
age = rs.getInt("sage");
address = rs.getString("saddress");
}
return new student(no,name,age,address);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally {
try {
if(rs!=null) rs.close();
if(prepareStatement!=null) prepareStatement.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void deleteStudentByNo(int sno) {
}
}
studentService.java:对studentDao.java进行封装
package org.student.service;
import org.student.dao.studentDao;
import org.student.entity.student;
//业务逻辑层,逻辑性的增删改查(增加:查+增)
public class StudentService {
studentDao stude=new studentDao();
public boolean addStudent(student stu) {
if(!stude.isExit(stu.getNo())) {
stude.addStudent(stu);
return true;
}else {
System.out.println("此人已存在!");
return false;
}
}
}