JDBC连接SQLServer2000问题

pzhuyy 2005-09-21 05:13:52
系统win2003+SqlServer2000(sp4)+Jdbc+Tomcat5.0.28+Jdk1.4.2
用下面的程序测试通过
//Connect.java
import java.*;
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String serverName= "192.168.0.63";
private final String portNumber = "1433";
private final String databaseName= "pubs";
private final String userName = "sa";
private final String password = "pzhuyy";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";

// Constructor
public Connect(){}

private String getConnectionUrl(){
return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
}

private java.sql.Connection getConnection(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
if(con!=null) System.out.println("Connection Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}

/*
Display the driver properties, database details
*/

public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriver Name: "+ dm.getDriverName());
System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
System.out.println("\nDatabase Information ");
System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
System.out.println("Avalilable Catalogs ");
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcatalog: "+ rs.getString(1));
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error: No active Connection");
}catch(Exception e){
e.printStackTrace();
}
dm=null;
}

private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperties();
}
}

但用JSP却不能正常连接.已将lib文件下的文件拷至common/lib下.classpath变量也没有问题.(见一java程序已通过.)
用下面的JSP程序却报错!

//test.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*" %>


<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jsp";
String user="sa";
String pw="test";
Connection conn=DriverManager.getConnection(url,user,pw);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql="select * from messagebook";
ResultSet rs=stmt.executeQuery(sql);
while (rs.next())
{
%>
one:<%=rs.getString("uid")%>

<%
}
rs.close();
stmt.close();
conn.close();
%>

错误信息如下:
javax.servlet.ServletException: com.microsoft.jdbc.sqlserver.SQLServerDriver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.testjsp_jsp._jspService(testjsp_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1340)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:148)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:69)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:141)
org.apache.jsp.testjsp_jsp._jspService(testjsp_jsp.java:48)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

请问是什么原因?多谢!
...全文
239 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pzhuyy 2005-09-21
  • 打赏
  • 举报
回复
已加入了classpath,因为用前面那个java程序就能运行,原来我修改了classpath的时候没有重启tomcat,多谢楼上诸位.因为你们这样一说,我突然想到要重启一下tomcat,一切搞定!
guming123416 2005-09-21
  • 打赏
  • 举报
回复
呵呵,将他加到JDK的LIB目录下,然后在classpath中配置一下,就okle
pzhuyy 2005-09-21
  • 打赏
  • 举报
回复
试试!
doway 2005-09-21
  • 打赏
  • 举报
回复
三个文件加入 classpath 比较合理,楼主再建其他应用是不必 copy 来 copy 去。
loveyousomuch 2005-09-21
  • 打赏
  • 举报
回复
三个驱动包放到发布目录的WEB-INF/lib下
Theface 2005-09-21
  • 打赏
  • 举报
回复
lib文件下的文件拷至common/lib?

SQL驱动的三个包放在你项目的WEB-INF/lib/下,最好放common/lib/下
myth822 2005-09-21
  • 打赏
  • 举报
回复
WEB-INF/lib下面

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧