webservice myeclipse 连接sql2005
package com.myweb.ws;import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
public class IDCreator {
public static Connection dbConn;
private static String driverName;
private static String dbURL;
public static boolean connectDBC(String dbName) {
driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
dbURL = "jdbc:sqlserver://localhost:1433;integratedSecurity=true;DatabaseName="+dbName; // 连接服务器和数据库sample
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL);
System.out.println("Connection Successful!"); // 如果连接成功
// 控制台输出Connection
// Successful!
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
部署时有异常:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at com.myweb.ws.Connect.connectDBC(Connect.java:20)
at com.myweb.ws.Request.<init>(Request.java:18)
at com.myweb.ws.RequestDelegate.<init>(RequestDelegate.java:8)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.xml.ws.api.server.InstanceResolver.createNewInstance(InstanceResolver.java:215)
at com.sun.xml.ws.api.server.InstanceResolver.createDefault(InstanceResolver.java:180)
at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:123)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:467)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:253)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:147)
at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:108)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4334)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
而且我的jdbc4.jar已经添加到reference library目录了了 --------------------编程问答-------------------- 你的jar包是不是搞错了,是sql2005 的吗 --------------------编程问答-------------------- jar包弄错了 --------------------编程问答-------------------- 之前直接在eclipse里面用,就是用的这个包,是没有问题的,而且我还在那个包里面找个这个类 com.microsoft.sqlserver.jdbc.SQLServerDriver了,但是它却报错。。。。 --------------------编程问答-------------------- 之前直接在eclipse里面用,就是用的这个包,是没有问题的,而且我还在那个包里面找个这个类 com.microsoft.sqlserver.jdbc.SQLServerDriver了,但是它却报错。。。。 --------------------编程问答-------------------- 没有找到类,看看是不是包错了,或是包没有加载上 --------------------编程问答-------------------- 可以了,我在新建了文件夹C:\Program Files\Microsoft SQL Server2005 JDBC Driver,然后把sqljdbc4.jar放在里面,然后 在系统环境变量classpath里面添加,就可以了,
谢谢了各位! --------------------编程问答--------------------
你这样的话,打了包的工程,在别的环境部署时 ,还要要求配置环境变量,不推荐的。
主要问题还是额外引入jar的操作步骤有问题。
补充:Java , Java EE