首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

求教!有关Date begintime = rs.getDate ( "begintime" )的有关问题

2012-04-01 
求教!有关Date begintime rs.getDate ( begintime )的问题首先我是想实现修改的功能,后来显示出Value

求教!有关Date begintime = rs.getDate ( "begintime" )的问题
首先我是想实现修改的功能,后来显示出Value '0000-00-00' can not be represented as java.sql.Date这样一个问题,按照网上教的方法在连接数据库语句后面添加zeroDateTimeBehavior=convertToNull,结果所有date类型的值都为0000-00-00了,怎么办呢?其实不是很明白网上这个方法的用意,求指教!下面是修改页面和显示页面的代码:

update_cooperation.jsp:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>无标题文档</title>
</head>

<body>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url= "jdbc:mysql://localhost:3306/graduationproject?user=root&password=123&useUnicode=true&characterEncoding=GB2312&zeroDateTimeBehavior=convertToNull";
Connection connection=DriverManager.getConnection(url); 
Statement statement = connection.createStatement();
String number=request.getParameter("number");
String sql="select * from cooperations where number='"+number+"'";
ResultSet rs = statement.executeQuery(sql);
%>
  <table width="1572">
  <tr>
  <th width="169" scope="col"> 起始时间</th>
  <th width="169" scope="col">结束时间</th>
  <th width="166" scope="col">物料</th>
  <th width="168" scope="col">项目编号</th>
  <th width="169" scope="col"> 项目名称</th>
  <th width="167" scope="col">ICP</th>
  <th width="168" scope="col"> 供应商名称</th>
  <th width="169" scope="col">合作价格</th>
  <th width="169" scope="col">状态</th>
  </tr>
</table>
<% while(rs.next()) {%>


<form id="form1" name="form1" method="post" action="deal_update_cooperation.jsp">
<table width="1698">
  <tr>
  <th width="168" height="49" scope="col">
  <div align="center">
  <input type="hidden" name="number" value="<%=request.getParameter("number")%>"/>
  <input type="text" name="begintime" value="<%=rs.getTime("begintime")%>" />
  &nbsp;</a></div></th>
  <th width="168" scope="col">
  <div align="center">
  <input type="text" name="endtime" value="<%=rs.getTime("endtime")%>" />
  &nbsp;</div></th>
  <th width="168" scope="col">
  <div align="center">
  <input type="text" name="material" value="<%=rs.getString("material")%>" />
  &nbsp;</div></th>
  <th width="168" scope="col"><div align="center">
  <input name="p_number" type="text" value="<%=rs.getString("p_number")%>" />
  &nbsp; </div></th>
  <th width="168" scope="col">


  <div align="center">
  <input type="text" name="project" value="<%=rs.getString("projectname")%>" />
&nbsp; </div></th>
  <th width="168" scope="col"><div align="center">
  <input name="ICP" type="text" value="<%=rs.getString("ICP")%>"/>
  &nbsp; </div></th>
  <th width="168" scope="col">
   
  <div align="center">
  <input type="text" name="suppliername" value="<%=rs.getString("suppliername")%>" />
  &nbsp; </div></th>
  <th width="168" scope="col"><div align="center">
  <input type="text" name="price" value="<%=rs.getInt("price")%>" />
  &nbsp;</div></th>
  <th width="168" scope="col"><div align="center">
  <input type="text" name="situation" value="<%=rs.getString("situation")%>" />
  &nbsp;</div></th>
  <th width="58" scope="col">
  <div align="center">
  <input type="submit" name="Submit" value="保存" />
  &nbsp; </div></th>
  <th width="58" scope="col">
   
  <div align="center">
  <input type="reset" name="Submit2" value="重写" />
  &nbsp; </div></th>
  </tr>
 </table>
</form>
<p>
  <% } %>
</p>

</body>
</html>

cooperation_management.jsp

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>无标题文档</title>
<style type="text/css">
<!--
.STYLE1 {font-size: medium}
.STYLE2 {font-size: x-small}
-->
</style>
</head>

<body>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url= "jdbc:mysql://localhost:3306/graduationproject?user=root&password=123&useUnicode=true&characterEncoding=GB2312";
Connection connection=DriverManager.getConnection(url); 
Statement statement = connection.createStatement();
String sql="select * from cooperations";
ResultSet rs = statement.executeQuery(sql);
%>
<table width="898" align="center">
  <tr bordercolor="#999999">
  <th scope="col"><span class="STYLE1">起始时间</span></th>
  <th scope="col"><span class="STYLE1">结束时间</span></th>
  <th scope="col"><span class="STYLE1">物料</span></th>
  <th scope="col"><span class="STYLE1">项目名称</span></th>
  <th scope="col"><span class="STYLE1">供应商名称</span></th>


  <th scope="col"><span class="STYLE1">合作价格</span></th>
  <th scope="col"><span class="STYLE1">状态</span></th>
  </tr>
</table>
<% 
while(rs.next()) {
String number = rs.getString ( "number" );
Date begintime = rs.getDate ( "begintime" );
Date endtime = rs.getDate ( "endtime" );
String material = rs.getString ( "material" );
String projectname = rs.getString ( "projectname" );
String suppliername = rs.getString ( "suppliername" );
Integer price = rs.getInt ( "price" );
String situation = rs.getString ( "situation" );

%>

<table width="976" align="center">
  <tr bordercolor="#999999">
  <th width="176" scope="col"><span class="STYLE1"><%=begintime%>&nbsp;</a></span></th>
  <th width="139" scope="col"><span class="STYLE1"><%=endtime%>&nbsp;</span></th>
  <th width="74" scope="col"><span class="STYLE1"><%=material%>&nbsp;</span></th>
  <th width="136" scope="col"><span class="STYLE1"><%=projectname%>&nbsp;</span></th>
  <th width="171" scope="col"><span class="STYLE1"><%=suppliername%>&nbsp;</span></th>
  <th width="140" scope="col"><span class="STYLE1"><%=price%>&nbsp;</span></th>
  <th width="75" scope="col"><span class="STYLE1"><%=situation%>&nbsp;</span></th>
  <th width="29" scope="col"><a href="update_cooperations.jsp?number=<%=number %>" class="STYLE2">修改</a><span class="STYLE1">&nbsp;</span></th>
  </tr>
</table>
<p>
  <% } %>
  <a href="cooperation_management_add.jsp">添加</a></p>

</body>
</html>


[解决办法]
你把rs.getTime()改为rs.getDate();
如果要把日期按某种格式输出,用DateFormat;
[解决办法]
如果是下面语句有问题:
Date begintime = rs.getDate ( "begintime" );
Date endtime = rs.getDate ( "endtime" );
你在保存日期值时不要保存什么0000-00-00;直接为NULL就是,
0000-00-00是个啥意思;

热点排行