目录
四:实现Java程序与页面之间的链接--------servlet
目标
实质:实现web界面化对数据库的查询显示,增加,删除,更改。
应用:对软件用户的新建,更改,删除等。
示例:有一张通信工程的学生表,其中包含学生的学号,姓名,班级,性别,住址信息,请在web端显示,并以GUI界面的形式设置有对这张表进行增删改查功能。
步骤:
-
首先制作jdbc与数据库链接的底层Java代码,bin包,db包,biz包,dao包
-
再绘制jsp的显示页面,应用到了模态框,模态框中有<from>表单,用于进行数据的输入,然后可以用jspl循环来进行输出。包含js函数。
-
制作servlet
-
其中servlet与jsp页面可以通过重定向或者转发。
MVC之间的关系:
具体实现
一:建立数据库
首先数据库是一个项目的根本,所以我们必须先建立数据库。这里采用MySql数据库来创建,若为Oracle数据库则创建过程基本类似。
CREATE TABLE tongxingongcheng(
stuid INT PRIMARY KEY AUTO_INCREMENT,##实现自增
stuname VARCHAR(20),
stuclass VARCHAR(20),
stusex VARCHAR(10),
stuaddress VARCHAR (50)tongxingongcheng
);
ALTER TABLE tongxingongcheng ADD typekey INT DEFAULT 1;
INSERT INTO tongxingongcheng VALUES (NULL,'小桃红','通信三班','女','碑林区',1);
SELECT * FROM tongxingongcheng ;
建立的结果为:
二:利用jdbc连接数据库。
- 具体为将链接数据库的过程拆分,拆分为src/main/java包下的bean包,db包,dao包,biz包。
- bean包的作用是实现将数据库中的字段封装成一个类。每一条记录理所当然的就成一个Java的对象。
- db包的主要功能是实现Java和数据库的链接与关闭,因为方法基本是用类来调用的,所以常将属性和方法设置为静态的即static类型。
- dao包是这几个包中最为重要的一个包,在它里面要实现对sql语句的封装,并执行sql语句。根据sql语句的不同,可以实现对数据库表的增删改查。
- biz包,它的作用是业务层,将所需要的业务封装,让其他类可以简便快捷的调用对数据库的操作。
bean包下的类:
package bean;
public class Tongxininfo {
private int stuid;
private String stuname;
private String stuclass;
private String stusex;
private String stuaddress;
private int typekey;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStuclass() {
return stuclass;
}
public void setStuclass(String stuclass) {
this.stuclass = stuclass;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
public int getTypekey() {
return typekey;
}
public void setTypekey(int typekey) {
this.typekey = typekey;
}
@Override
public String toString() {
return "Tongxininfo [stuid=" + stuid + ", stuname=" + stuname + ", stuclass=" + stuclass + ", stusex=" + stusex
+ ", stuaddress=" + stuaddress + ", typekey=" + typekey + "]";
}
}
db包下的类:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Driver;
public class DataSource {
private static String url="jdbc:mysql://localhost:3306/db05";
private static String user="root";
private static String password="123456";
/**
* 获取连接
* @throws ClassNotFoundException
*/
public static Connection getConnection() {
try {
Class.forName(Driver.class.getName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn=null;
try {
conn=(Connection) DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 关闭连接
* @param conn
*/
public static void closeConnection(Connection conn,PreparedStatement ps,ResultSet rs) {
try {
if(conn!=null) {
conn.close();
}
if(ps!=null) {
ps.close();
}
if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
dao包下的类:
package dao;
//增删改查的持久化操作
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 bean.Tongxininfo;
import db.DataSource;
public class TongxinDAO {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
/**
* 增
*/
public void addTongXinStu(Tongxininfo tx) {
String sql="insert into tongxingongcheng values(null,?,?,?,?,1)";
conn=DataSource.getConnection();
try {
ps=conn.prepareStatement(sql);
ps.setString(1, tx.getStuname());
ps.setString(2, tx.getStuclass());
ps.setString(3, tx.getStusex());
ps.setString(4, tx.getStuaddress());
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DataSource.closeConnection(conn, ps,null);
}
}
/**
* 删
*/
/**
* 改
*/
/**
* 查
*/
public Tongxininfo findById(int id) {
Tongxininfo t=new Tongxininfo();
String sql="select * from tongxingongcheng where stuid=? and typekey=1";
conn=DataSource.getConnection();
try {
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
rs=ps.executeQuery();
if(rs.next()) {
t.setStuid(rs.getInt(1));
t.setStuname(rs.getString(2));
t.setStuclass(rs.getString(3));
t.setStusex(rs.getString(4));
t.setStuaddress(rs.getString(5));
t.setTypekey(rs.getInt(6));
}else {
t=null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DataSource.closeConnection(conn, ps, rs);
}
return t;
}
/**
* 查找所有的学生
*/
public List<Tongxininfo> findAll() {
List<Tongxininfo> list=new ArrayList<Tongxininfo>();
String sql="select * from tongxingongcheng where typekey=1";
conn=DataSource.getConnection();
try {
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
Tongxininfo t=new Tongxininfo();
t.setStuid(rs.getInt(1));
t.setStuname(rs.getString(2));
t.setStuclass(rs.getString(3));
t.setStusex(rs.getString(4));
t.setStuaddress(rs.getString(5));
t.setTypekey(rs.getInt(6));
if(t.getTypekey()==1) {
list.add(t);
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
biz包下的类:
package biz;
//业务层
import java.util.List;
import bean.Tongxininfo;
import dao.TongxinDAO;
public class TongxinBiz {
private TongxinDAO tx=new TongxinDAO();
public Tongxininfo findById(int id){
Tongxininfo t;
return t=tx.findById(id);
}
public void addTongXinStu(Tongxininfo t) {
tx.addTongXinStu(t);
}
public List<Tongxininfo> findAll() {
List<Tongxininfo> list=tx.findAll();
return list;
}
}
三:绘制jsp显示页面
在这里需要用到HTML的知识,和jsp的知识。
<%@ page language="java" isELIgnored="false" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>通信工程页面</title>
<head>
<!-- 你就可以使用bootstrap了 -->
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<c:if test="${empty STULEST }">
<c:redirect url="stu" />
</c:if>
<body>
<div class="table-responsive">
<table class="table">
<caption>学生信息表格</caption>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal"> 添加学生信息 </button>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>班级</th>
<th>性别</th>
<th>住址</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach var="i" items="${STULEST }" varStatus="k">
<tr>
<td>${k.count }</td>
<td>${i.stuname }</td>
<td>${i.stuclass }</td>
<td>${i.stusex }</td>
<td>${i.stuaddress }</td>
<td>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="checkbox"> 更改信息
</label>
<label class="btn btn-primary">
<input type="checkbox">删除
</label>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>
<!-- 需要一个模态框 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 class="modal-title" id="myModalLabel">
请输入待添加学生的信息
</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="firstname" class="col-sm-2 control-label">姓名:</label>
<div class="col-sm-4">
<input type="text" class="stuname" placeholder="请输入名字">
</div>
</div>
<div class="radio">
<label for="firstname" class="col-sm-2 control-label">班级</label>
<label>
<input type="radio" name="stuclass" value="通信一班" checked>通信一班
</label>
<label>
<input type="radio" name="stuclass" value="通信二班">通信二班
</label>
<label>
<input type="radio" name="stuclass" value="通信三班">通信三班
</label>
<label>
<input type="radio" name="stuclass" value="通信四班">通信四班
</label>
<label>
<input type="radio" name="stuclass" value="通信五班">通信五班
</label>
</div>
<div class="radio">
<label for="firstname" class="col-sm-2 control-label">性别:</label>
<label>
<input type="radio" name="stusex" value="男" checked>男
</label>
<label>
<input type="radio" name="stusex" value="女">女
</label>
</div>
<div class="form-group">
<label for="firstname" class="col-sm-2 control-label">住址:</label>
<div class="col-sm-5">
<input type="text" class="stuaddress"
placeholder="请输入居住地">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">
确认提交
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal -->
</div>
其显示结果为:
四:实现Java程序与页面之间的链接--------servlet