| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 621 人关注过本帖
标题:关于记录为空和时间格式的转换!1
取消只看楼主 加入收藏
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
 问题点数:0 回复次数:1 
关于记录为空和时间格式的转换!1

<%@ 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 {
%>
&nbsp;
<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>

这些代码包含了添加和查询的功能,但是这里的查询的功能如果记录为空的时候就会报错,还有就是那个时间格式的有问题,那些代码不知道怎么整理,整理了好就都有问题,希望大家能帮我看看,谢谢,代码有点长......错误信息在下一贴,

搜索更多相关主题的帖子: 格式 记录 时间 
2007-10-09 15:26
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 
记录为空时的错误信息,

org.apache.jasper.JasperException: ResultSet 没有当前行。
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

javax.servlet.ServletException: ResultSet 没有当前行。
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.index_jsp._jspService(index_jsp.java:349)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

com.microsoft.sqlserver.jdbc.SQLServerException: ResultSet 没有当前行。
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyOnValidRow(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerResultSet.getString(Unknown Source)
org.apache.jsp.index_jsp._jspService(index_jsp.java:201)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

时间格式的错误!

org.apache.jasper.JasperException: 从字符串转换为 datetime 时发生语法错误。
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

javax.servlet.ServletException: 从字符串转换为 datetime 时发生语法错误。
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:858)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.index_jsp._jspService(index_jsp.java:349)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

com.microsoft.sqlserver.jdbc.SQLServerException: 从字符串转换为 datetime 时发生语法错误。
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerStatement$StatementExecutionRequest.executeStatement(Unknown Source)
com.microsoft.sqlserver.jdbc.CancelableRequest.execute(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerConnection.executeRequest(Unknown Source)
com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(Unknown Source)
org.apache.jsp.index_jsp._jspService(index_jsp.java:127)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

可能我说的还不怎么明白,我想的纠正是,当查询记录为空时,会报错:
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>");
}

当时间格式不对时会报错:

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>");
}

麻烦大家了!
2007-10-09 15:30
快速回复:关于记录为空和时间格式的转换!1
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029205 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved