这是我的一个记录帖,我是按照黑马程序员的视频学习的。
刚学习完JDBC & JSP,使用工具Eclipse做了一个简单的学生信息管理系统。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
欢迎使用学生信息管理系统
请输入管理员账号密码
package zjb.StuManager;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import zjb.dao.StudentDao;
import zjb.dao.UserDao;
import zjb.daoimpl.StudentDaoImpl;
import zjb.daoimpl.UserDaoImpl;
import zjb.domain.Student;
//用于处理登录的servlet
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//改编码格式
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//获取用户端提交的信息
String userName = request.getParameter("username");
String passWord = request.getParameter("password");
//访问数据库有没有这个用户
UserDao dao = new UserDaoImpl();
boolean isSuccess = dao.login(userName, passWord);
if(isSuccess) {
//1.查询所有的学生信息
StudentDao stuDao = new StudentDaoImpl();
List<Student> list = stuDao.Select();
//把查询出来的信息集合存入作用域中
request.getSession().setAttribute("list", list);
//重定向
response.sendRedirect("stu_list.jsp");
}else {
response.getWriter().write("用户名或密码错误!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package zjb.dao;
public interface UserDao {
boolean login(String userName , String passWord);
}
package zjb.daoimpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import zjb.dao.UserDao;
import zjb.util.JDBCUtil;
public class UserDaoImpl implements UserDao{
@Override
public boolean login(String userName , String passWord) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//得到连接对象
conn = JDBCUtil.getConn();
String sql = "select * from user where username=? and password=?";
//创建ps对象
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, passWord);
//执行
rs = ps.executeQuery();
//如果返回ture就是有这个用户
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.release(conn, ps, rs);
}
return false;
}
}
/**
* 工具类,用来连接数据库和释放资源
*/
package zjb.util;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
public class JDBCUtil {
static String url = null;
static String name = null;
static String password = null;
static String driverClass = null;
static {
try {
//创建一个属性配置对象
Properties properties = new Properties();
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//导入输入流
properties.load(is);
//读取属性
url = properties.getProperty("url");
name = properties.getProperty("name");
password = properties.getProperty("password");
driverClass = properties.getProperty("driverClass");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
//注册驱动
Class.forName(driverClass);
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//建立连接
conn = DriverManager.getConnection(url, name, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//释放资源
public static void release(Connection conn, Statement st, ResultSet rs) {
closeRs(rs);
closeConn(conn);
closeSt(st);
}
//两个参数的release重载
public static void release(Connection conn, Statement st) {
closeConn(conn);
closeSt(st);
}
private static void closeRs(ResultSet rs) {
try {
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
rs = null;
}
}
private static void closeConn(Connection conn) {
try {
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn = null;
}
}
private static void closeSt(Statement st) {
try {
if(st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
st = null;
}
}
public static void main(String[] args) {
}
}
driverClass = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost/stumanager
name = root
password = 123admin
package zjb.dao;
import java.util.List;
import zjb.domain.Student;
//查询出来的学生信息返回给List
public interface StudentDao {
List<Student> Select();
}
package zjb.daoimpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import zjb.dao.StudentDao;
import zjb.domain.Student;
import zjb.util.JDBCUtil;
public class StudentDaoImpl implements StudentDao{
@Override
public List<Student> Select() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
//还需要把几个学生对象装入集合中
List<Student> list = new ArrayList<Student>();
try {
//得到连接对象
conn = JDBCUtil.getConn();
String sql = "select * from student";
//创建ps对象
ps = conn.prepareStatement(sql);
//执行
rs = ps.executeQuery();
while(rs.next()) {
//把学生的五个信息数据都装到一个学生对象中
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stu.setAge(rs.getInt("age"));
stu.setSex(rs.getString("sex"));
stu.setAddress(rs.getString("address"));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.release(conn, ps, rs);
}
return list;
}
}
package zjb.domain;
public class Student {
//到底有哪些成员,并不是取出数据库中的所有数据,而是想要在页面上显示哪些数据
private int id;
private int age;
private String name;
private String sex;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
学生信息管理系统
学生信息列表
编号
姓名
性别
年龄
地址
操作
${stu.id }
${stu.name }
${stu.sex }
${stu.age }
${stu.address }
更新 删除
大家学习可以去看黑马程序员的视频,挺详细的
[1]: http://www.itheima.com/