数据库连接不上的问题,解决立即给分

cwl500449 2003-04-09 04:50:04
有下面一段小程序,连接数据库总是报错,但我把它改成JDBC-ODBC桥接器的方式连接数据就可以,而且我用纯JAVA也是用下面的JDBC这种方式连接数据没有问题,我使用的是WIN2000+tomcat5.0+jdk1.4 我想肯定是JDBC驱动程序的问题,但我不知道怎样解决,请大虾们帮我解决为谢。

<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://online:1433;DatabaseName=faq";

String user="sa";
String password="cwl";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from faqs";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%> <br>
您的第二个字段内容为:<%=rs.getString(2)%> <br>
您的第三个字段内容为:<%=rs.getString(3)%><br>
<br>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
...全文
7 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yzxasd 2003-04-09
很有可能是驱动问题
msbase.jar
mssqlserver.jar
msutil.jar
回复
rttw 2003-04-09
更换新版本的驱动!另外,所有有关数据操作的代码请写到try{}里面捕获异常,然后在最后的catch(){}里面打印出异常信息,这样可以得到一部分信息
回复
55918 2003-04-09
完全同意anar(喜面狗) 的观点!

你的问题在于只加入了mssqlserver.jar这一个文件,需要把三个文件全部加上!

回复
cwl500449 2003-04-09
我没用那个JBUILDER好东东,我用的是文本编制器,错误如下:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:692)
at org.apache.jsp.test_jsp._jspService(test_jsp.java:82)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:136)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:323)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:293)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:288)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:294)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:490)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1020)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:196)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:490)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1020)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2625)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:490)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1020)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:490)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1020)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:196)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:612)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:430)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:623)
at java.lang.Thread.run(Thread.java:536)


root cause

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1312)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1159)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:192)
at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:118)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:322)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:130)
at org.apache.jsp.test_jsp._jspService(test_jsp.java:46)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:136)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:323)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:293)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:288)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:294)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:490)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1020)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:196)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:490)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1020)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2625)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:490)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1020)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:490)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1020)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:196)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:612)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:430)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:623)
at java.lang.Thread.run(Thread.java:536)



回复
dybine 2003-04-09
什么问题都不知道怎么下手啊
回复
风清扬-3 2003-04-09
http://www.microsoft.com/downloads/details.aspx?FamilyID=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&DisplayLang=en#filelist下载JDBC驱动程序。安装,得到msbase.jar,mssqlserver.jar和msutil.jar三个文件,将三个文件COPY到TOMCAT 4.1下common\lib文件夹中。
用jsp测试。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为你的数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from tongxue"; //你的数据库中的表
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
回复
hogcheng 2003-04-09
因为JDBC-ODBC桥接器JDK本身自带,所以不用追加.而SQLServerDriver的驱动程序需要自己安装,所以必须追加.
1.首先确定SQLServerDriver的驱动程序已经安装.
2.如果你用的开发工具是JBUILDER,在你建立的工程上点击右键,选择属性,在PATH选项卡里选择REQUIED选项,把你安装的SQLServerDriver的驱动程序载入.
3.在你的程序里,import com.microsoft.*;就可以了.

祝你成功.
回复
yzxasd 2003-04-09
先将错误帖出来
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-04-09 04:50
社区公告
暂无公告