简体   繁体   English

无法将MS SQL Server 2008与Java连接

[英]Not able to connect MS SQL server 2008 with java

I tried to connect java with My MS SQL express, I downloaded the sqljdbc4.jar also 我尝试将Java与我的MS SQL Express连接,我也下载了sqljdbc4.jar

When I don't bother with the CLASSPATH and all and I try to execute my program( even with sqljdbc in 当我不关心CLASSPATH和所有代码时,我尝试执行我的程序(即使使用sqljdbc

C:\\Program Files (x86)\\Java\\jre6\\lib C:\\ Program Files(x86)\\ Java \\ jre6 \\ lib

I get the following error 我收到以下错误

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at Connect.getConnection(Connect.java:24)
    at Connect.displayDbProperties(Connect.java:42)
    at Connect.main(Connect.java:78)

Error Trace in getConnection() : com.microsoft.jdbc.sqlserver.SQLServerDriver Error: No active Connection getConnection()中的错误跟踪:com.microsoft.jdbc.sqlserver.SQLServerDriver错误:无活动连接

But If I try and set the CLASSPATH variable to point to sqljdbc4.jar then ClassNotHoundExcepetion is occurring with identifying my classname. 但是,如果我尝试将CLASSPATH变量设置为指向sqljdbc4.jar,则在识别我的类名时会发生ClassNotHoundExcepetion。

Need immediate help. 需要立即的帮助。 kindly respond. 请回应。

I use Command line mode of invoking it, I tried setting the classpath in the system variables dialog box of windows 7. and when I do that or use set CLASSPATH="C:\\temp\\sqljdbc4.jar" the jre fails to recogonize the main class of my program and throws 我使用命令行模式进行调用,我尝试在Windows 7的系统变量对话框中设置类路径。当我这样做或使用set CLASSPATH =“ C:\\ temp \\ sqljdbc4.jar”时,jre无法重新设置我程序的主类和抛出

Exception in thread "main" java.lang.NoClassDefFoundError: Connect
Caused by: java.lang.ClassNotFoundException: Connect
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Could not find the main class: Connect. 找不到主类:连接。 Program will exit. 程序将会退出。

So I am at a loss here. 所以我在这里茫然。

Your problem occur because classloader has not found driver jar, how you start your program? 发生您的问题是因为classloader找不到驱动程序jar,如何启动程序? Have you put -cp or -classpath if it command line ? 您是否在命令行中放置了-cp-classpath Or add to lib folder if web application ? 还是添加到lib文件夹(如果是Web应用程序)?

You should set the classpath to include the sqljdbc4.jar file. 您应该将类​​路径设置为包括sqljdbc4.jar文件。 See details here . 在这里查看详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM