Topic: JDBC问题,老是错,为什么?

  Print this page

1.JDBC问题,老是错,为什么? Copy to clipboard
Posted by: didaland
Posted on: 2003-02-11 11:30

import java.sql.*;

public class dd
{
  public static void main(String[] args)
  {

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    String url="jdbc:odbc:GlobalCar";

    Connection dbcn=DriverManager.getConnection(url);

  }
};
----------------------------
上面是一个最简单的JDBC连接ODBC的代码,可连编译都通不过,抛出
dd.java:8: unreported exception java.lang.ClassNotFoundException;
or declared to be thrown
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
^
dd.java:12: unreported exception java.sql.SQLException; must be c
ed to be thrown
Connection dbcn=DriverManager.getConnection(url);
^
的错误,这是为什么?我一直搞不懂,都急死了。

2.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-11 11:41

See the code(in black background)

http://www.cjsdn.com/post/view?bid=1&id=11151&sty=1&tpg=1&age=0

You need to catch exceptions.

3.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 00:43

我按照你的代码改了,编译正常,运行还是会抛出异常:
Exception in thread "main" java.lang.NoClassDefFoundError: mybase

4.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-12 00:49

This is not jdbc related.

Where do you reference your mybase class? Do a search and find it.

5.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 00:51

import java.sql.*;
public class mybase
{
public static void main(String[] args)
{

Connection conn = null;
try
{
Class.forName("sun.jdbc.odbc.JdbOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:space");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (conn != null) conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
}
-------------------------------
代码是这样的,从你的代码改变来。

6.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 00:53

其实最开始能正确运行的,但是后来出错了,偶尔又能运行后就再也不能运行了。
我学JDBC才开了个头,就遇到这种事,唉,烦!

7.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 01:00

import java.sql.*;

public class void mybase
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:space";
Connection conn=DriverManager.getConnection(url);
}
catch(ClassNotFoundException er)
{
System.err.println(er);
}
catch(SQLException sqler)
{
System.err.println(sqler)
}
}
}
----------------------------------------------------------------------------------------------
最初是这样的,还行,多运行几次后就不行了,老是报告:
Exception in thread "main" java.lang.NoClassDefFoundError: mybase

第一段关于JDBC的代码就这样了,太打击信心了,帮帮忙啊!

8.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-12 01:07

Hehe... funny!(Sorry)

Ok, you changed your class name to mybase. Normally you should use capitals at the beginning, like MyBase for your class name. But let's live with this for now.

In dos prompt, do this:
set PATH=%PATH%;.
now go to your dir where the mybase.class is and run
java mybase
or whatever you run before.

You code is fine now. You just need to run it correctly.

9.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-12 01:13

didaland wrote:
import java.sql.*;

public class void mybase
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:space";
Connection conn=DriverManager.getConnection(url);
}
catch(ClassNotFoundException er)
{
System.err.println(er);
}
catch(SQLException sqler)
{
System.err.println(sqler)
}
}
}
----------------------------------------------------------------------------------------------
最初是这样的,还行,多运行几次后就不行了,老是报告:
Exception in thread "main" java.lang.NoClassDefFoundError: mybase

第一段关于JDBC的代码就这样了,太打击信心了,帮帮忙啊!

You omit the conn.close() line. So everytime you run, you open a connection to db, and you dont close it. After several times, db is fed up(hope your dba doesn't pp you, ;D) so you can't run anymore unless you restart db.
My code is really a minimal, so don't be lazier than me, Big Smile. (I am the 1.5th laziest person in the whole world, Tongue).

10.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 01:25

Sad
还是一样啊!
----------
严格按照你的提示做了。

11.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 01:38

import java.sql.*;
public class Mybase
{
public static void main(String[] args)
{

Connection conn = null;
try
{
Class.forName("sun.jdbc.odbc.JdbOdbcDriver");
      String url="jdbc:odbc:space";
conn = DriverManager.getConnection(url);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (conn != null) conn.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
}
--------------------------------------------------
我已经改成这样了,可还是照旧!
真想从楼上跳下去了!

12.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 01:41

并且还重新启动了电脑。Black Eye
--------------------
E:\>java Mybase
Exception in thread "main" java.lang.NoClassDefFoundError: Mybase
是什么原因造成以上的提示呢?
我用的WIN2000 PRO+JDK1.4.1
安装完JDK未做任何设置上的改动。
-------------------
JAVA路漫漫!

13.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 01:59

我灵机一动,来了个这样的:
import java.sql.*;
public class My2
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbOdbcDriver");
}
catch (ClassNotFoundException e)
{
System.out.println("error");
}
}
}
结果还是照旧!
看来关键是什么原因引起的
Exception in thread "main" java.lang.NoClassDefFoundError: My2
研究研究!
你睡觉了?

14.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-12 02:40

Where is your java class file? My2.class or mybase.class file's location?

Where did you type in "java My2"?

15.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-12 02:40

Where is your java class file? My2.class or mybase.class file's location?

Where did you type in "java My2"?

16.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: menzy
Posted on: 2003-02-12 08:31

请列出如下信息:
原文件路径
CLASSPATH参数
ODBC数据源配置

17.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 09:02

.class位置:e:\
jdk位置:e:\weiwei\j2sdk

18.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 09:20

CLASSPATH:E:\weiwei\j2sdk\jre\lib\rt.jar;C:\Program Files\Borland\InterBase\InterClient\interclient.jar
ODBC连接的是一个ACCESS数据库文件,正常的。

19.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-12 11:44

post the output of "e:\>dir", I don't think your My2.class is under there.

Here is what I got:

d:\download\jivecrack>java abc
Exception in thread "main" java.lang.NoClassDefFoundError: abc

since I don't have abc.class under there.

20.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 12:06

E:\>dir My2.*
驱动器 E 中的卷没有标签。
卷的序列号是 1023-61C8

E:\ 的目录

2003-02-12 09:01 591 My2.class
2003-02-12 08:59 382 My2.java
2003-02-12 08:58 209 My2.java.
3 个文件 1,182 字节
0 个目录 11,048,378,368 可用字节
---------------------------------------------------------------

E:\>java My2
Exception in thread "main" java.lang.NoClassDefFoundError: My2
--------------------------------------------------------------
这样的了。

21.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 12:11

这是为什么哦?是不是CLASSPATH设置的问题?
好像是JAVA无法装载sun.jdbc.odbc.JdbOdbcDriver一样。有人说把rt.jar放在CLASSPATH设置里,我做了,还是不行。

22.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-12 12:29

do this:

set CLASSPATH=%CLASSPATH%;.

and try it. Or give the full path

23.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 13:55

E:\>java my2
Exception in thread "main" java.lang.NoClassDefFoundError: my2 (wrong name: My2)

at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
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)
----------------------------------------------------------------------------------------------------
现在来了这么多的提示。

24.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 13:58

我想这个问题和JDBC没有什么关系了。因为现在我的电脑上的JAVA连
public class tt
{
  public static void main(String[] args)
  {
    System.out.println("Hello World");
  }
};
都不能正常运行了。
同样出现
Exception in thread "main" java.lang.NoClassDefFoundError: tt
的提示。
看来是JAVA的问题。

25.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 14:04

奇怪的是JAVA-HOME里的DEMO目录下的例子的程序又都能运行,不过APPLETS在浏览器里无法正常显示,而PLUG-IN形式的在浏览器里又可以。
到底是什么的影响?
我的电脑里就装了JDK1.4.1,DELPHI,INTERBASE,SQL SERVER客户端,OFFICE XP,MACROMEDIA MX和PC-CILLIN,曾经装过IDEA,已经卸载了。

26.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 14:11

用appletviewer浏览APPLET又是可以的。
怎么回事啊?难不成要重新安装WINDOWS?

27.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-12 14:39

didaland wrote:
E:\>java my2
Exception in thread "main" java.lang.NoClassDefFoundError: my2 (wrong name: My2)

at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
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)
----------------------------------------------------------------------------------------------------
现在来了这么多的提示。

You should type in "java My2" with capital M!

do this:

set CLASSPATH=%CLASSPATH%;.

and try it. Or give the full path like: java e:\My2

28.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 14:41

奇了,还是不成。
不过,我确通过JSP成了。弄了个RESIN,然后弄了个访问数据库的JSP代码,成了,没有任何问题。
问题是JAVA命令行到底是怎么了?

29.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-12 18:12

终于按捺不住将WIN2000重新安装一次,一切正常了!!!!!!

30.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: floater
Posted on: 2003-02-13 00:54

didaland wrote:
终于按捺不住将WIN2000重新安装一次,一切正常了!!!!!!

Hehe, finally, huh?

Good!

31.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: jackycct
Posted on: 2003-02-13 10:29

try to run like this :
e:\weiwei\j2sdk\java -cp "e:/" mybase

if you still can't get it work, maybe the JRE registry has been messed up. Try to reinstall the JDK 1.4 again.

Hope that help

32.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-14 22:22

现在来说说怎么会出现上面说的那种现象的:
未安装INTERBASE时,一切正常,
安装INTERBASE时,选择了安装InterClient,INTERBASE安装程序修改了系统参数:CLASSPATH=C:\Program Files\Borland\InterBase\InterClient\interclient.jar,然后,问题就来了。
我把CLASSPATH删除掉,又正常,就这样了。
想问一下,“CLASSPATH=C:\Program Files\Borland\InterBase\InterClient\interclient.jar”里还要添加什么才能正常?

33.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-14 22:28

这里谢过总版主及各位大侠的帮助!!!
我开始上路了Smile

34.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: R7733251
Posted on: 2003-02-15 11:00

SmileSadBig SmileCoolBlushTongueEvilWinkClownBlack EyeShyShockAngryDeadSleepyKissesApprovedDisapprovedQuestionSmileBig SmileOh My GodTounge Wink Sad Confused What are you talking about? Cry Embaressed Cool Angry Angel Give me a hug My heart Broken heart... Present Rose Wilted Rose Camera Film Phone Kitty bowwow Coffee Light Bulb MoonSleepy Start Musical Note Envelope Cake Clock Thumbs up Thumbs down Beer Food Kiss Fight Music Stupid Devil

35.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: menzy
Posted on: 2003-02-17 11:18

恭喜,通常这样的问题是由于没有把当前路径加入CLASSPATH中,所以无法找到类。如果用Resin,有自己的默认路径,所以运行起来没有问题。

36.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-17 14:00

对,台湾人王森的《JAVA深度历限》里有详细的说明,看看就明白了。

37.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: jackycct
Posted on: 2003-02-17 14:45

You may set the classpath like this
CLASSPATH="%CLASSPATH%;C:\Program Files\Borland\InterBase\InterClient\interclient.jar"

Of course, the best solution is to supply a clean classpath to java.exe by -cp

Wink

38.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: ryanzhou
Posted on: 2003-02-19 10:49


39.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: parasound
Posted on: 2003-02-22 19:12

Make sure you use java from your JDK path. There is java.exe in c:\win2k\system32\java.exe which you want to avoid.

40.别急,我来帮忙 [Re: didaland] Copy to clipboard
Posted by: ccic134302
Posted on: 2003-02-24 16:34

运行以下代码即可:
import java.sql.*;

public class dd
{
public static void main(String[] args)
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url="jdbc:odbc:GlobalCar";

Connection dbcn=DriverManager.getConnection(url);
}
catch(SQLExceptionb se){};
}
};

41.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: didaland
Posted on: 2003-02-25 16:09

我用的是这种:
CLASSPATH=".;C:\Program Files\Borland\InterBase\InterClient\interclient.jar",现在很好,而且知道出现那种错误的原因是什么了。真让人高兴。

42.Re:JDBC问题,老是错,为什么? [Re: didaland] Copy to clipboard
Posted by: jacob
Posted on: 2003-02-25 16:37

He, Congratulations!
welcome to JAVA world!


   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