连接数据库
1.Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>学生管理</title>
</head>
<body>
<a href="./StudentListAction" target="_parent">学生管理</a>
</body>
</html>
2.stud_list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.student.vo.*" %>
<head>
<title>学生查询列表</title>
</head>
<body>
<input type="button" value="新增" οnclick="addStudent()">
<input type="button" value="修改" οnclick="detalStudent()">
<input type="button" value="删除" οnclick="delStudent()">
<input type="button" value="明细" οnclick="detalStudent()">
<%
List<Student> list=(List<Student>)session.getAttribute("studList");
%>
<table border="1px" width="700px">
<tr>
<th style="width: 25px"></th>
<th width="50px">序号</th>
<th width="80px">姓名</th>
<th width="80px">年龄</th>
<th width="80px">学号</th>
<th width="100px">联系电话</th>
<th width="200px" >地址</th>
</tr>
<%
for(int i=0;i<list.size();i++){
Student stud=list.get(i);
%>
<tr>
<td><input type="radio" name="ids" value="<%=stud.getStudent_id()%>"> </td>
<td><%=stud.getStudent_id()%></td>
<td><%=stud.getStudent_name()%></td>
<td><%=stud.getStudent_age()%></td>
<td><%=stud.getStudent_no()%></td>
<td><%=stud.getStudent_tel()%></td>
<td><%=stud.getStudent_address()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
JDBCConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
public class JDBCConnection {
/*
* 工具类,用于获取数据库连接对象,作用:避免在所有的Dao类重复写此代码
*/
public static Connection getConnection() throws Exception{
Connection conn=null;
// Properties prop=new Properties();
// String path=JDBCConnection.class.getClassLoader().getResource("").getPath();
// prop.load(new FileReader(new File(path+"/jdbc.properties")));
// String username=prop.getProperty("username");
// String password=prop.getProperty("password");
// String url=prop.getProperty("url");
// String driver=prop.getProperty("driver");
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=UTF-8";
String username = "root";// mysql数据库的用户名
String password = "123";// mysql数据的连接密码
Class.forName(driver);
conn=DriverManager.getConnection(url, username, password);
return conn;
}
}
4.Student.java
public class Student {
public int student_id;
public String student_name;
public String student_no;
public String student_tel;
public String student_age;
public String student_address;
public Student(int student_id, String student_name, String student_no,
String student_tel, String student_age, String student_address) {
super();
this.student_id = student_id;
this.student_name = student_name;
this.student_no = student_no;
this.student_tel = student_tel;
this.student_age = student_age;
this.student_address = student_address;
}
public int getStudent_id() {
return student_id;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getStudent_no() {
return student_no;
}
public void setStudent_no(String student_no) {
this.student_no = student_no;
}
public String getStudent_tel() {
return student_tel;
}
public void setStudent_tel(String student_tel) {
this.student_tel = student_tel;
}
public String getStudent_age() {
return student_age;
}
public void setStudent_age(String student_age) {
this.student_age = student_age;
}
public String getStudent_address() {
return student_address;
}
public void setStudent_address(String student_address) {
this.student_address = student_address;
}
}
5.StudentDao.java
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 com.student.vo.Student;
import com.util.JDBCConnection;
public class StudentDao {
//专门负责与数据库交互,获取相应数据
private Connection conn=null;
private PreparedStatement statement=null;
private ResultSet set=null;
public List<Student> queryStudentList() throws Exception{
List<Student> list=new ArrayList<Student>();
conn=JDBCConnection.getConnection();
String sql="select * from stud1";
try {
this.statement=conn.prepareStatement(sql);
this.set=this.statement.executeQuery();
while(this.set.next()){
int id=set.getInt("stud_id");
String name=set.getString("stud_nname");
String no=set.getString("stud_no");
String tel=set.getString("stud_tel");
String age=set.getString("stud_age");
String address=set.getString("stud_address");
Student stud=new Student(id, name, no, tel, age, address);
list.add(stud);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
6.StudentService.java
import java.util.List;
import com.student.dao.StudentDao;
import com.student.vo.Student;
public class StudentService {
//学生管理服务类,用于处理业务逻辑
private StudentDao dao=new StudentDao();
//只需做一个查询,业务逻辑简单,所以仅仅是调用一个方法就可以
public List<Student> queryList() throws Exception{
return dao.queryStudentList();//调用StudentDao中的queryStudentList(),此方法的作用是从数据库中取到数据
}
}
7.StudentListAction.java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.student.service.StudentService;
import com.student.vo.Student;
@SuppressWarnings("serial")
public class StudentListAction extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//Servlet是管理流程控制的
StudentService service=new StudentService();
try {
List<Student> list=service.queryList();//获取服务
HttpSession session=request.getSession();//获取session对象,表示一个会话范围
session.setAttribute("studList",list );//取到数据
response.sendRedirect("./stud_list.jsp"); //数据取到了,要做一个回应、展示,重定向
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
1.Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>学生管理</title>
</head>
<body>
<a href="./StudentListAction" target="_parent">学生管理</a>
</body>
</html>
2.stud_list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.student.vo.*" %>
<head>
<title>学生查询列表</title>
</head>
<body>
<input type="button" value="新增" οnclick="addStudent()">
<input type="button" value="修改" οnclick="detalStudent()">
<input type="button" value="删除" οnclick="delStudent()">
<input type="button" value="明细" οnclick="detalStudent()">
<%
List<Student> list=(List<Student>)session.getAttribute("studList");
%>
<table border="1px" width="700px">
<tr>
<th style="width: 25px"></th>
<th width="50px">序号</th>
<th width="80px">姓名</th>
<th width="80px">年龄</th>
<th width="80px">学号</th>
<th width="100px">联系电话</th>
<th width="200px" >地址</th>
</tr>
<%
for(int i=0;i<list.size();i++){
Student stud=list.get(i);
%>
<tr>
<td><input type="radio" name="ids" value="<%=stud.getStudent_id()%>"> </td>
<td><%=stud.getStudent_id()%></td>
<td><%=stud.getStudent_name()%></td>
<td><%=stud.getStudent_age()%></td>
<td><%=stud.getStudent_no()%></td>
<td><%=stud.getStudent_tel()%></td>
<td><%=stud.getStudent_address()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
JDBCConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
public class JDBCConnection {
/*
* 工具类,用于获取数据库连接对象,作用:避免在所有的Dao类重复写此代码
*/
public static Connection getConnection() throws Exception{
Connection conn=null;
// Properties prop=new Properties();
// String path=JDBCConnection.class.getClassLoader().getResource("").getPath();
// prop.load(new FileReader(new File(path+"/jdbc.properties")));
// String username=prop.getProperty("username");
// String password=prop.getProperty("password");
// String url=prop.getProperty("url");
// String driver=prop.getProperty("driver");
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=UTF-8";
String username = "root";// mysql数据库的用户名
String password = "123";// mysql数据的连接密码
Class.forName(driver);
conn=DriverManager.getConnection(url, username, password);
return conn;
}
}
4.Student.java
public class Student {
public int student_id;
public String student_name;
public String student_no;
public String student_tel;
public String student_age;
public String student_address;
public Student(int student_id, String student_name, String student_no,
String student_tel, String student_age, String student_address) {
super();
this.student_id = student_id;
this.student_name = student_name;
this.student_no = student_no;
this.student_tel = student_tel;
this.student_age = student_age;
this.student_address = student_address;
}
public int getStudent_id() {
return student_id;
}
public void setStudent_id(int student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getStudent_no() {
return student_no;
}
public void setStudent_no(String student_no) {
this.student_no = student_no;
}
public String getStudent_tel() {
return student_tel;
}
public void setStudent_tel(String student_tel) {
this.student_tel = student_tel;
}
public String getStudent_age() {
return student_age;
}
public void setStudent_age(String student_age) {
this.student_age = student_age;
}
public String getStudent_address() {
return student_address;
}
public void setStudent_address(String student_address) {
this.student_address = student_address;
}
}
5.StudentDao.java
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 com.student.vo.Student;
import com.util.JDBCConnection;
public class StudentDao {
//专门负责与数据库交互,获取相应数据
private Connection conn=null;
private PreparedStatement statement=null;
private ResultSet set=null;
public List<Student> queryStudentList() throws Exception{
List<Student> list=new ArrayList<Student>();
conn=JDBCConnection.getConnection();
String sql="select * from stud1";
try {
this.statement=conn.prepareStatement(sql);
this.set=this.statement.executeQuery();
while(this.set.next()){
int id=set.getInt("stud_id");
String name=set.getString("stud_nname");
String no=set.getString("stud_no");
String tel=set.getString("stud_tel");
String age=set.getString("stud_age");
String address=set.getString("stud_address");
Student stud=new Student(id, name, no, tel, age, address);
list.add(stud);
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
6.StudentService.java
import java.util.List;
import com.student.dao.StudentDao;
import com.student.vo.Student;
public class StudentService {
//学生管理服务类,用于处理业务逻辑
private StudentDao dao=new StudentDao();
//只需做一个查询,业务逻辑简单,所以仅仅是调用一个方法就可以
public List<Student> queryList() throws Exception{
return dao.queryStudentList();//调用StudentDao中的queryStudentList(),此方法的作用是从数据库中取到数据
}
}
7.StudentListAction.java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.student.service.StudentService;
import com.student.vo.Student;
@SuppressWarnings("serial")
public class StudentListAction extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//Servlet是管理流程控制的
StudentService service=new StudentService();
try {
List<Student> list=service.queryList();//获取服务
HttpSession session=request.getSession();//获取session对象,表示一个会话范围
session.setAttribute("studList",list );//取到数据
response.sendRedirect("./stud_list.jsp"); //数据取到了,要做一个回应、展示,重定向
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}