<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.SimpleDateFormat"%>
<jsp:directive.page import="ConnectData.connection" />
<html>
<head>
<title>学生基本信息操作</title>
</head>
<jsp:useBean id="myconn" class="ConnectData.connection"></jsp:useBean>
<body>
<%
int pagesize = 2;
int totalrow = 0;
int mypage = 0;
int totalpage = 0;
String sql;
Connection conn;
Statement stmt;
ResultSet rs = null;
String str;
int i = 0;
Statement stm = null;
int mysex = 0;
String mynum;
String myname;
String mybirth;
String sexString;
conn = myconn.getconn();
stm = conn.createStatement();
request.setCharacterEncoding("gb2312");
if (request.getParameter("ready") != null) {
mynum = request.getParameter("num");
myname = request.getParameter("name");
mybirth = request.getParameter("birth");
sexString = request.getParameter("Sex");
if (sexString != null) {
mysex = sexString.equals("male") ? 0 : 1;
}
if (mynum.equals("") || myname.equals("") || mybirth.equals("")
|| sexString.equals("")) {
out.print("<script>");
out.print("alert('请填写完整的信息!');");
out.print("window.location.href='index.jsp';");
out.print("</script>");
}
else {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
try {
java.util.Date birthday = formatter.parse(request.getParameter("birth"));
}
catch (Exception e) {
out.print("<script>");
out.print("alert('请输入yyyy-MM-dd时间格式!!');");
out.print("window.location.href='index.jsp';");
out.print("</script>");
}
String mysql = "insert into stuBaseInfo values('" + mynum
+ "','" + myname + "'," + mysex + ",'" + mybirth
+ "')";
stm.executeUpdate(mysql);
}
}
stmt = conn.createStatement(
java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
java.sql.ResultSet.CONCUR_READ_ONLY);
if (request.getParameter("ready0") != null) {
if (request.getParameter("text0").equals("")) {
out.print("<script>");
out.print("alert('请输入查询值!!');");
out.print("window.location.href='index.jsp';");
out.print("</script>");
}
if (request.getParameter("text0") != null) {
sql = "select * from stuBaseInfo where stuID like '%"
+ request.getParameter("text0") + "%'";
rs = stmt.executeQuery(sql);
if (!rs.next()) {
out.print("<script>");
out.print("alert('没该记录!!');");
out.print("window.location.href='index.jsp';");
out.print("</script>");
}
}
} else {
sql = "select * from stuBaseInfo";
rs = stmt.executeQuery(sql);
}
rs.last();
totalrow = rs.getRow();
rs.first();
str = request.getParameter("page");
if (str == null)
mypage = 1;
else
mypage = Integer.parseInt(str);
if (mypage < 1)
mypage = 1;
if (mypage >= totalrow)
mypage = totalrow;
totalpage = totalrow % pagesize == 0 ? totalrow / pagesize
: totalrow / pagesize + 1;
if (totalpage > 0)
rs.absolute((mypage - 1) * pagesize + 1);
%>
<table align="center" border="1">
<tr>
<td>
学号
</td>
<td>
姓名
</td>
<td>
性别
</td>
<td>
生日
</td>
<td>
删除
</td>
</tr>
<%
while (i <= pagesize && !rs.isAfterLast()) {
String myID = rs.getString(1);
String myName = rs.getString(2);
Byte mySex = rs.getByte(3);
String myBirth = rs.getString(4);
%>
<tr>
<td>
<%=myID%>
</td>
<td>
<%=myName%>
</td>
<td>
<%=mySex%>
</td>
<td>
<%=myBirth%>
</td>
<td>
<a href="delete.jsp?id=<%=myID%>">删除</a>
</td>
</tr>
<%
rs.next();
i++;
}
conn.close();
%>
<tr>
<td>
当前页为:
<%=mypage%>
</td>
<td>
总的记录数为:
<%=totalrow%>
</td>
<td colspan="3">
总页数为:
<%=totalpage%>
</td>
</tr>
</table>
<div style="z-index:1; position:absolute; left:253px; top:150px">
<%
if (mypage < 2) {
out.print("首页" + "||" + "上页");
} else {
%>
<a href="index.jsp">首页</a> ||
<a href="index.jsp?page=<%=mypage - 1%>">上页</a>
<%
}
%>
<%
if ((totalpage - mypage) < 1) {
out.print("下页" + "||" + "尾页");
} else {
%>
<a href="index.jsp?page=<%=mypage + 1%>">下页</a> ||
<a href="index.jsp?page=<%=totalpage%>">尾页</a>
<%
}
%>
</div>
<div style="z-index:3; position:absolute; left:253px; top:240px">
<form action="index.jsp" method="post" name="form1"
onsubmit="return formcheck();">
请输入关键字:
<input type="text" name="text0">
<input type="submit" value="查询">
<input type="hidden" name="ready0" value="true">
</form>
</div>
<div style="z-index:1; position:absolute; left:253px; top:300px">
<form method="post" name="form" action="index.jsp">
<p>
学号:
<input type="text" name="num">
</p>
<p>
姓名:
<input type="text" name="name">
</p>
<p>
性别:
<input type="radio" name="Sex" value="male" checked>
男
<input type="radio" name="Sex" value="female">
女
</p>
<p>
生日:
<input type="text" name="birth" maxlength="20">
</p>
<p>
<input type="submit" value="提交" name="submit">
<input type="reset" value="重置">
<input type="hidden" name="ready" value="true">
</p>
</form>
</div>
</body>
</html>
这些代码包含了添加和查询的功能,但是这里的查询的功能如果记录为空的时候就会报错,还有就是那个时间格式的有问题,那些代码不知道怎么整理,整理了好就都有问题,希望大家能帮我看看,谢谢,代码有点长......错误信息在下一贴,