JSP/JDBC/SERVLET的简单拼接功能实现
在家玩了一个多月,感觉之前学的JAVA都忘了,不得已,又重新敲起了基础。
代码结构层次图
先从最简单的增删该查回忆起,我最开始甚至已经忘记了JDBC怎么写了,大脑里只有注入驱动,获取连接,但具体全部忘却了
那么我们先看一下我的JDBC工具类
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3305/test";
private static String username = "root";
private static String password = "root";
Connection con = null;// 连接对象
PreparedStatement pstmt = null;// 语句对象
ResultSet rs = null;// 结果集对象
SQLException {
Class.forName(driver);//注入驱动
con = DriverManager.getConnection(url, username, password);//获取连接
return con;
}
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
这个是MySql,的注入,但其实我工作时用到的是Oracle,因为自己笔记本只有MySql,所以只能这样了。
下面是Oracle的
关于实体类bean的写法倒是没有忘记,毕竟Eclipse的SET/GET快捷功能还是记得的
private String name;
private String sex;
private int password;
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 int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
我在Dao层里把增删改查的方法全放进了里面,我写一半时才发现,我忘了增删改查,其实写俩个查和改就行了,但是写都已经写了,也懒得删了。
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import Wl.Test.utils.JdbcUtils;
JdbcUtils jdbc = new JdbcUtils();
int i = 0;
try {
Connection con = jdbc.getConnection();
PreparedStatement pre = con.prepareStatement(sql);
i = pre.executeUpdate();
jdbc.close(null, pre, con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int i = 0;
try {
Connection con = jdbc.getConnection();
PreparedStatement pre = con.prepareStatement(sql);
i = pre.executeUpdate();
jdbc.close(null, pre, con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int i = 0;
try {
Connection con = jdbc.getConnection();
PreparedStatement pre = con.prepareStatement(sql);
i = pre.executeUpdate();
jdbc.close(null, pre, con);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
person per = new person();
try {
Connection con = jdbc.getConnection();
PreparedStatement pre = con.prepareStatement(sql);
ResultSet rs = pre.executeQuery();
while (rs.next()) {
per.setName(rs.getString("name"));
per.setPassword(rs.getInt("password"));
per.setSex(rs.getString("sex"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return per;
}
而在Control层,对了写了一半。发现,这也是简单的MVC模型,Servlet这层代码其实增删改差不多,就举一个例子吧
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
* Servlet implementation class AddServlet
*/
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
HttpServletResponse response) throws ServletException, IOException {
MysqlFunction fun = new MysqlFunction();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String password = request.getParameter("password");
String sql = "insert into person(name,sex,password) values("
+ name + ',' + sex + ',' + password + ")";
int b = 0;
b = fun.insert(sql);
if (b != 0) {
try {
request.getRequestDispatcher("./Add.jsp?error=true").forward(
request, response);
System.out.println("Exception");
}
} else {
try {
request.getRequestDispatcher("./Add.jsp?error=false").forward(
request, response);
System.out.println("Exception");
}
}
只有查找比较麻烦,毕竟人家的返回值不是int啊,还是int大法好,
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import Wl.Test.dao.MysqlFunction;
* Servlet implementation class SelectServlet
*/
public class SelectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
HttpServletResponse response) throws ServletException, IOException {
MysqlFunction fun = new MysqlFunction();
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String password = request.getParameter("password");
String sql = "select * from person where name=" + name + " AND sex= "
+ sex;
person per=new person();
per=fun.select(sql);
if(per==null){
try {
request.getRequestDispatcher("/Select.jsp?error=false").forward(
request, response);
} catch (Exception e) {
System.out.println("Exception");
}
}else{
HttpSession session = request.getSession(true);
session.setAttribute("person", per);
try {
request.getRequestDispatcher("/Result.jsp").forward(request,
response);
} catch (Exception e) {
System.out.println("Exception.54");
}
}
}
写着写着到JSP那块,我是真的绝望,我发现前面的我还是会的,不会百度一下什么一看,就想起来了,但是对于页面这一块,我是真的感觉要去W3C重新看一下了。
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add</title>
<script type="text/javascript" src="jquery-1.9.x.js"></script>
<script type="text/javascript">
var error="<%=request.getParameter("error")%>"
alert("SUCCESS!");
}
if(error=='false'){
alert("FALL!");
}
</head>
<body>
<form action="AddServlet">
username:<input type="text" name="name"><br>
sex:<input type="text" name="sex"><br>
password:<input type="password" name="password"><br>
<input type="submit">
</form>
</body>
</html>
这是我现在写的JSP,只记得文本框之类的了,在翻下我三个月前的JSP
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Result</title>
<script type="text/javascript" src="jquery-1.9.x.js"></script>
<script type="text/javascript">
var errori="<%=request.getParameter("CHOSE")%>";
var check="<%=request.getParameter("check")%>";
if(check=='yes'){
alert("删除失败,请确保信息一致");
}
if(errori=='yes'){
alert("您没有勾中删除框");
}
<script>
function back(){
window.location.href = "./Key-In.jsp";
}
</script>
<script>
function deletes() {
form.submit();
}
form.action = "ResultServlet";
form.submit();
</script>
<style>
.body {
heigt: 100%;
width: 950px;
margin: 0 auto;
}
height: 40px;
}
width: 30px;
height: 40px;
background-color: orange;
float: left;
}
margin: 0 0 float:left;
font-size: 35px;
color: white;
background-color: black;
}
height: 80px;
padding: 20px 10px;
}
width: 33.33%;
float: left;
height: 32px;
}
background-color: darkgray;
float: left;;
height: 30px;
width: 150px;
line-height: 30px;
color: white;
}
text-align: center;
width: 150px;
}
text-align: center;
}
margin-top: 20px;
}
width: 80px;
height: 30px;
margin-right: 6px;
background-color: yellow;
}
</style>
<script>
<%List list = (List) session.getAttribute("list");
students student = (students) list.get(0);%>
</script>
</head>
<body>
<div class="body">
<div class="header">
<div class="logo"></div>
<div class="text">Student Score Maintenance Result</div>
</div>
<div>
<hr />
</div>
<div class="nav">
<div class="res1">
<div class="info">Strudent ID</div>
<td><%=student.getSID()%></td>
</div>
<div class="res1">
<div class="info">Strudent Name</div>
<td><%=student.getSNM()%></td>
</div>
<div class="res1"></div>
<div class="res1">
<div class="info">Class</div>
<td><%=student.getCLASS()%></td>
</div>
<div class="res1">
<div class="info">Grade</div>
<td><%=student.getGRADE()%></td>
</div>
<div class="res1">
<div class="info">Sex</div>
<td><%=student.getSEX()%></td>
</div>
</div>
<hr />
<form action="ResultServlet" method="post" name="form">
<table border="1" bgcolor="gray" cellspacing="0px">
<th>No.</th>
<th>Sel</th>
<th>Curriculum ID</th>
<th>Curriculum Name</th>
<th>Score</th>
<%
List list1 = (List) session.getAttribute("list1");
for (int i = 0; i < list1.size(); i++) {
students student1 = (students)list1.get(i);
String cid = student1.getCID();
String cnm = student1.getCNM();
String score = student1.getSCORE();
%>
<TR>
<TD><%=i + 1%></TD>
<TD><INPUT TYPE="checkbox" name="checkbox<%=i%>"></INPUT></TD>
<TD><%=cid%></TD>
<TD><%=cnm%></TD>
<TD><%=score%></TD>
</TR>
<%
}
%>
</table>
<div class="sub-btn">
<input type="button" value="Update(U)" onclick="submit();" /><input
type="button" value="Delete(D)" onclick="deletes();" /><input
type="button" value="Back(B)" onclick="back();" />
</div>
</form>
</div>
<div class="footer"></div>
</body>
</html>
我是真的很绝望以及无奈啊,明天有场面试,我感觉GG,我得花点时间,看看书了。
简单的Result页面,接受查询的结果
<%@page import="Wl.Test.bean.person"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Result</title>
<script type="text/javascript" src="jquery-1.9.x.js"></script>
<script>
<%person per = (person) session.getAttribute("person");
session.setAttribute("name", per.getName());
session.setAttribute("sex", per.getSex());%>
</script>
</head>
<body>
<div class="res1">
<div class="info">NAME</div>
<td name="name"><%=per.getName()%></td>
</div>
<div class="res1">
<div class="info">SEX</div>
<td name="sex"><%=per.getSex()%></td>
</div>
</body>
</html>
当JSPt提交时,
String SCORE = request.getParameter("score1" + a);
信息可以携带跳转,
又或者session,cookie
String CID = (String)session.getAttribute("cid" + a);