开发者

How to use SQLite3 with Java

开发者 https://www.devze.com 2023-01-01 17:28 出处:网络
I am trying to build a simple java program which creates a db file, then a table and inserts dummy values in the table. I found this page http://www.zentus.com/sqlitejdbc/index.html and tried out the

I am trying to build a simple java program which creates a db file, then a table and inserts dummy values in the table. I found this page http://www.zentus.com/sqlitejdbc/index.html and tried out the example given on the page but I am getting the following error -

Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java开发者_开发问答.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Test.  Program will exit.


Well that looks like it's a matter of the classpath not being right.

My guess is that you're

If you've put something like

java -cp sqlitejdbc-v056.jar Test

then you probably just need to add the current directory to the classpath:

# Windows
java -cp sqlitejdbc.jar-v056;. Test

# Unix
java -cp sqlitejdbc.jar-v056:. Test

Having looked at that page, my guess is that you used : as the classpath separator, as shown on the page, rather than ; which you need to use if you're on Windows.


Are you sure you're building the test correctly? Here are the steps you'll need to take:

  1. Create a file named Test.java, because it will contain the class named Test
  2. Paste the Java code from the Getting Started section into this file and save it
  3. Download the sqlitejdbc-v056.jar file and put it in the same directory as the Test.java file
  4. Use javac to compile Test.java into Test.class
  5. Run the command from the Getting Started section

You should see:

name = Gandhi
job = politics
name = Turing
job = computers
name = Wittgenstein
job = smartypants

as output.

0

精彩评论

暂无评论...
验证码 换一张
取 消