关于jsp连接access的问题!已经连了n天了,小弟实在无能为力了!(跪求帮助)

goslingg 2004-10-03 11:29:13
这是光盘上的一个留言板程序
以下是他的连接数据库的javabean
数据源设好了
可是怎么就连不上,已经好几天了!5555!
我的系统是win2003+access2003
跪求大人帮忙!!!!!!!!
package com.jspdev.ch9;
import java.sql.*;
import java.util.*;

public class MessageBean
{
private Connection con;
MessageVO msg;

//获得数据库连接。
public MessageBean()
{


try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con=java.sql.DriverManager.getConnection("jdbc:odbc:one","","");
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setMessage(MessageVO msg)
{
this.msg=msg;
}
public void addMessage()throws Exception
{
try
{
PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)");
stm.setString(1,msg.getTitle());
stm.setString(2,msg.getName());
stm.setDate(3,new java.sql.Date(new java.util.Date().getTime()));
if((msg.getEmail()).length()==0)
stm.setString(5,null);
else stm.setString(5,msg.getEmail());
stm.setString(4,msg.getContent());
try
{
stm.executeQuery();
}
catch(Exception e)
{
}

con.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public Collection getMessage()throws Exception
{
Collection ret=new ArrayList();
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select count(*) from message");
int message_count=0;
if(result.next())
{
message_count=result.getInt(1);
result.close();
}
if(message_count>0)
{
result=stm.executeQuery("select * from message order by time desc");

while(result.next())
{
String title=result.getString("title");
String name=result.getString("name");
String mail=result.getString("mail");
String content=result.getString("content");
java.sql.Date date=result.getDate("time");
MessageVO message=new MessageVO();
message.setName(name);
message.setTitle(title);
message.setContent(content);
message.setDate(date);
message.setEmail(mail);
ret.add(message);
}
result.close();
stm.close();
}

con.close();

}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
return ret;
}
}





以下是tomcat里面的日志
2004-10-03 22:56:16 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at com.jspdev.ch9.MessageBean.getMessage(MessageBean.java:65)
at org.apache.jsp.viewMessages_jsp._jspService(viewMessages_jsp.java:57)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:211)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:805)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:696)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
at java.lang.Thread.run(Thread.java:534)

2004-10-03 22:59:16 StandardContext[/jsp-examples]SessionListener: contextDestroyed()
2004-10-03 22:59:16 StandardContext[/jsp-examples]ContextListener: contextDestroyed()
2004-10-03 22:59:16 StandardContext[/servlets-examples]SessionListener: contextDestroyed()
2004-10-03 22:59:16 StandardContext[/servlets-examples]ContextListener: contextDestroyed()


谢谢了!!!
...全文
288 点赞 收藏 15
写回复
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
朋友别哭 2004-10-04
呵呵
系统都2003了.怎么数据库还这么落后啊^_^
回复
todouwang 2004-10-04
要看错误,贴出来吧,代码看得累
回复
mbl 2004-10-04
可能是环境设的不对
回复
zzw0598 2004-10-04
好像access并不落后啊,只是小了点。这种方式连接数据库是要用odbc先建数据源的,换了机子要重建数据源
可以换一种方式
public class JdbcAccess {

public static void GetResult() {
Connection con;
Statement stmt;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//注意DBQ的写法 herald.mdb是数据库的名字
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\herald.mdb";
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
String query = "select * from herald";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String str = rs.getString("title");
System.out.print(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
GetResult();
}
}

回复
goslingg 2004-10-04
自己顶
回复
goslingg 2004-10-04
自己顶啊
回复
goslingg 2004-10-04
谢谢大家
用zzw0598(口是心非)的方法成功了!
结帐!
回复
longjian012 2004-10-04
给个最简单的代码你,在页面里写就行了
conn.jsp
------------
<%
String url="jdbc:odbc:myjsp";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url);
Statement stmt=con.createStatement();
%>

-----
test.jsp
---------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ include file="connAccess.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<%
String sql="select * from T_Test";
ResultSet result=stmt.executeQuery(sql);
while(result.next()){
String title=result.getString("title");
%>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><%=title%> </td>
</tr>
</table>
</body>
<%
}
%>

</html>
回复
ricky921 2004-10-04
连access数据库有必要那么麻烦吗?
用zzw0598(口是心非) 那个方法就可以一点~~ 其实只需要家驱动程序 加载数据源就差不多了!!!
回复
nwpulipeng 2004-10-04
帮顶混分
回复
sxl6159078 2004-10-04
要不你加一个数据源,看看行不行。
回复
goslingg 2004-10-04
环境设置对了
运行一些jsp小程序还可以的!
回复
aleel008 2004-10-03
到底发生了什么错误,错误页面上怎么显示
回复
goslingg 2004-10-03
自己先顶一下!
回复
goslingg 2004-10-03
自己先顶一下!
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告