Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教如何用JAVA 连接 MS SQL Server2000
flyingqifei

电脑为家 网络为舍



发贴: 35
积分: 0
于 2005-07-10 21:12 user profilesend a private message to usersend email to flyingqifeisearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
请教怎么在JAVA里调用MS SQL Server2000数据库。
我的程序如下:

import java.sql.*;

public class Test
{
  public static void main(String arg[])
  {
    ResultSet result;
    try {
      Class.forName("sun.jdbc.odbc.jdbcOdbcDriver");
      Connection con = DriverManager.getConnection("jdbc:odbc:MyDataSource", "sa", "");
      Statement stat = con.createStatement();
      result = stat.executeQuery("Select * from Publicshers");
      
      while (result.next())
      {
        System.out.println (result.getString(2));
      }
    }
    catch(Exception e)
    {
      System.out.println ("Could not execute the query" + e);
    }
  }
}


编译成功,但执行错误。

错误消息如下:
Could not execute the queryjava.lang.ClassNotFoungException: sun.jdbc.odbc.jdbcOdbcDriver

我的SQL Server2000打了SP3补丁,数据源(ODBC)也设置了(MyDataSource),环境变量也配置了(class为E:\Program Files\java_db_jar\lib\msbase.jar;E:\Program Files\java_db_jar\lib\mssqlserver.jar;E:\Program Files\java_db_jar\lib\msutil.jar)。

另:若直接用JDBC应该如何配置。

恳请解答!谢谢!!!


flyingqifei edited on 2005-07-11 11:06

My website: http://flyingqifei.icpcn.com
My BLOG: http://qifei.blogbus.com
作者 Re:JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
flyingqifei

电脑为家 网络为舍



发贴: 35
积分: 0
于 2005-07-11 11:05 user profilesend a private message to usersend email to flyingqifeisearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
用ODBC我已经搞定了,但直接用JDBC还没弄好,请教各位!

编译成功,错误提示如下:
-------------------------------------------------------------------------------------------------
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:140)
at JDBC_ODBC_TEST2.main(JDBC_ODBC_TEST2.java:9)
-------------------------------------------------------------------------------------------------


flyingqifei edited on 2005-07-11 20:36

My website: http://flyingqifei.icpcn.com
My BLOG: http://qifei.blogbus.com
作者 Re:JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
snowbird2005

米兰大脑



发贴: 72
积分: 10
于 2005-07-11 11:09 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
将JDBC驱动加到classpath中


作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
flyingqifei

电脑为家 网络为舍



发贴: 35
积分: 0
于 2005-07-11 11:19 user profilesend a private message to usersend email to flyingqifeisearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我加了。就是那几个jar文件:
E:\Program Files\java_db_jar\lib\msbase.jar;E:\Program Files\java_db_jar\lib\mssqlserver.jar;E:\Program Files\java_db_jar\lib\msutil.jar
我用的是MS SQL已经加了呀



My website: http://flyingqifei.icpcn.com
My BLOG: http://qifei.blogbus.com
作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-07-11 11:25 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
You may want to search on our Database board...

Which OS are you using?
(I would try not to have spaces in the jar/zip/folder's in the CLASSPATH)

show us the result from
> echo %CLASSPATH%

> dir E:\Program Files\java_db_jar\lib\

(don't need to post an image, cut-n-paste from the Command Prompt window will do.)

You may want to show us your code as well.


why edited on 2005-07-11 11:29

作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
flyingqifei

电脑为家 网络为舍



发贴: 35
积分: 0
于 2005-07-11 20:34 user profilesend a private message to usersend email to flyingqifeisearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
晕!怎么不用中文呀!

我还是用中文回复吧,我用的是winXP。
我把那个图片改成文字了,但你那个环境变量设置有点不明白。


flyingqifei edited on 2005-07-11 20:38

My website: http://flyingqifei.icpcn.com
My BLOG: http://qifei.blogbus.com
作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
ccg





发贴: 25
积分: 0
于 2005-07-12 10:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Java的ClassPath最好不要有空格,不行的话把那几个Jar扔到其他路径或者代码的根目录下面试试吧


作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
flyingqifei

电脑为家 网络为舍



发贴: 35
积分: 0
于 2005-07-12 10:57 user profilesend a private message to usersend email to flyingqifeisearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
空格当然是没有的,换目录是没用的。
这个问题困扰几天,郁闷中.........................



My website: http://flyingqifei.icpcn.com
My BLOG: http://qifei.blogbus.com
作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
flyingqifei

电脑为家 网络为舍



发贴: 35
积分: 0
于 2005-07-12 18:42 user profilesend a private message to usersend email to flyingqifeisearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
功夫不负有心人,终于搞好了,ODBC桥接、JDBC都可以运行了。
谢谢大家的支持!!!Big Smile Coffee



My website: http://flyingqifei.icpcn.com
My BLOG: http://qifei.blogbus.com
作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-07-12 22:34 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
flyingqifei wrote:
空格当然是没有的,换目录是没用的。

> E:\Program Files\java_db_jar\lib\msbase.jar;
空格当然是有的!!

功夫不负有心人,终于搞好了

Please let us know how you did it.



作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
flyingqifei

电脑为家 网络为舍



发贴: 35
积分: 0
于 2005-07-12 22:53 user profilesend a private message to usersend email to flyingqifeisearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
呵呵!该有的空格还是不能少,不该有的就不能画蛇添足了,这里的Program Files中间的空格是可以加的,这个我没改。

flyingqifei edited on 2005-07-12 23:01

My website: http://flyingqifei.icpcn.com
My BLOG: http://qifei.blogbus.com
作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2005-07-12 23:55 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
flyingqifei wrote:
功夫不负有心人,终于搞好了


Please let us know how you did it.



作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:flyingqifei]
stevebeans





发贴: 5
积分: 0
于 2005-07-13 10:26 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
恭喜,搞定就好,其實在程序裡面最好還是利用jdbc直接聯接數據庫較好,盡量不使用odbc


作者 Re:请教如何用JAVA 连接 MS SQL Server2000 [Re:why]
flyingqifei

电脑为家 网络为舍



发贴: 35
积分: 0
于 2005-07-13 22:01 user profilesend a private message to usersend email to flyingqifeisearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
why wrote:
Please let us know how you did it.


我用的是JCreator编译运行的,一直只在系统设置系统变量,没在JCeator里设置,偶在以前的贴子里看到,呵呵!还真是这个问题。所以最好的编译环境还是命令行模式下!

赞成用JDBC


flyingqifei edited on 2005-07-13 22:06

My website: http://flyingqifei.icpcn.com
My BLOG: http://qifei.blogbus.com

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923