开发者

Connect to SQLite Database using Eclipse (Java)

开发者 https://www.devze.com 2022-12-30 12:14 出处:网络
I\'m trying to connect to SQLite database with Ecplise but I have some errors. This is my Java code and the errors that I get on output. Please see if you can help me. Thank you in advance.

I'm trying to connect to SQLite database with Ecplise but I have some errors. This is my Java code and the errors that I get on output. Please see if you can help me. Thank you in advance.


package jdb;
import java.sql.*;
public class Test {
  public static void main(String[] args) throws Exception {
    Class.forName("org.sqlite.JDBC");
    Connection conn = DriverManager.getConnection("jdbc:sqlite:/Applications/MAMP/db/sqlite/test.sqlite");
    Statement stat = conn.createStatement();
 开发者_开发问答   stat.executeUpdate("drop table if exists people;");
    stat.executeUpdate("create table people (name, occupation);");
    PreparedStatement prep = conn.prepareStatement("insert into people values (?, ?);");

prep.setString(1, "Gandhi");
prep.setString(2, "politics");
prep.addBatch();
prep.setString(1, "Turing");
prep.setString(2, "computers");
prep.addBatch();
prep.setString(1, "Wittgenstein");
prep.setString(2, "smartypants");
prep.addBatch();

conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);

ResultSet rs = stat.executeQuery("select * from people;");
while (rs.next()) {
  System.out.println("name = " + rs.getString("name"));
  System.out.println("job = " + rs.getString("occupation"));
}
rs.close();
conn.close();

} }

ans that what I get in Ecplise :


Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at jdb.Test.main(Test.java:7)

Thank you


Make sure you have sqlitejdbc-v056.jar in your Eclipse classpath


While can retrieve the database in this way also but their is simple way to set the database like download the SQLlite database browser then u can easily upgrade the data. if u gonna go in this way then i will get back to u soon.

0

精彩评论

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