项目结构:
数据库设计:
首先建立JDBC连接数据库:
DBUtil:
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
static {
//加载数据库驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection () {
Connection conn=null;
//获取数据库链接
try {
conn=DriverManager
.getConnection("jdbc:mysql://localhost:3306/wenbin?" +
"useSSL=false&serverTimezone=GMT","root","120831");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//关闭相关链接
public static void release(Connection conn,Statement stmt,ResultSet rs) {
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
建立和数据库对应的类:
classInfoBean:
package com.bean;
public class classInfoBean {
/**
* 属性和数据库中的字段相对应
*
*/
private int cid;
private String cname;
public classInfoBean() {
super();
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "classInfoBean [cid=" + cid + ", cname=" + cname + "]";
}
}
数据库操作:
classInfoDao:
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.management.RuntimeErrorException;
import com.entiry.classInfoBean;
public class classInfoDao {
static {
//加载数据库驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addClassInfo(classInfoBean bean) {
Connection conn=null;
Statement stmt=null;
//获取数据库链接
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/wenbin?"
+ "useSSL=false&serverTimezone=GMT","root","120831");
//整理一条sql语句
String sql="insert into class_info (cname) values('"
+bean.getCname()+ "')";
//创建sql执行对象
stmt=conn.createStatement();
//执行sql语句
int row=stmt.executeUpdate(sql);
if (row!=1) {
throw new RuntimeException( "新增班级失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public List<classInfoBean> findAll(){
Connection conn=null;
Statement stmt=null;
List<classInfoBean> classList=new ArrayList<classInfoBean>();
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/wenbin?"
+ "useSSL=false&serverTimezone=GMT","root","120831");
//整理一条sql语句
String sql="select cid,cname from class_info";
//创建sql执行对象
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
//遍历结果集
while(rs.next()) {
int cid=rs.getInt("cid");
String cname=rs.getString("cname");
classInfoBean bean=new classInfoBean();
bean.setCid(cid);
bean.setCname(cname);
classList.add(bean);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return classList;
}
}
Servlet操作
ClassInfoServlet:
package com.servlet;
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 com.dao.classInfoDao;
import com.entiry.classInfoBean;
/**
* Servlet implementation class ClassInfoServlet
*/
public class ClassInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ClassInfoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码,防止请求乱码
request.setCharacterEncoding("utf-8");
//获取参数
String className=request.getParameter("className");
//创建classInfoBean对象保存信息
classInfoBean bean=new classInfoBean();
bean.setCname(className);
//创建数据库操作对象
classInfoDao dao=new classInfoDao();
//新增班级信息到数据库
dao.addClassInfo(bean);
//查询所有班级信息
List<classInfoBean> classInfo=dao.findAll();
//保存
request.setAttribute("classInfo", classInfo);
//转发请求
request.getRequestDispatcher("/classInfo.jsp").forward(request,response );
}
}