需要帮助第一次使用java在eclipse上设置SQLite

时间:2014-07-25 19:35:12

标签: java eclipse sqlite

我一直在试图弄清楚如何让SQLite在eclipse juno上工作。我一直在关注此网站上的说明http://wiki.eclipse.org/Connecting_to_SQLite。问题不在于每个步骤都与解释完全一致,因此我猜测天气是否正确。我觉得我可能已经完全正确,直到第13步,没有SQL Model-JDBC Connection条目。所以我尝试使用通用JDBC和使用SQLite的步骤13-16。 SQLite没有一个驱动程序,由于步骤5没有任何意外。我到目前为止尝试过的任何方式都会导致ping失败,下面列出了详细信息。有人必须有更好的方法来完成这个过程。

java.sql.SQLException: java.lang.UnsatisfiedLinkError: SQLite.Database.open(Ljava/lang/String;I)V
at SQLite.JDBCDriver.connect(JDBCDriver.java:68)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(JDBCConnection.java:328)
at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(DriverConnectionBase.java:105)
at org.eclipse.datatools.connectivity.DriverConnectionBase.open(DriverConnectionBase.java:54)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.open(JDBCConnection.java:96)
at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnectionFactory.createConnection(JDBCConnectionFactory.java:53)
at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(ConnectionFactoryProvider.java:83)
at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(ConnectionProfile.java:359)
at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(PingJob.java:76)
at org.eclipse.datatools.connectivity.ui.PingJob.run(PingJob.java:59)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)

1 个答案:

答案 0 :(得分:16)

确保从 https://bitbucket.org/xerial/sqlite-jdbc/downloads 获取驱动程序,然后将驱动程序导入到项目中。

enter image description here

enter image description here

现在,您可以通过创建java类Sample.java

来测试配置
   import java.sql.Connection;
   import java.sql.DriverManager;
   import java.sql.ResultSet;
   import java.sql.SQLException;
   import java.sql.Statement;

   public class Sample
    {
    public static void main(String[] args) throws ClassNotFoundException
     {
      // load the sqlite-JDBC driver using the current class loader
      Class.forName("org.sqlite.JDBC");

      Connection connection = null;
      try
      {
         // create a database connection
         connection = DriverManager.getConnection("jdbc:sqlite:sample.db");

         Statement statement = connection.createStatement();
         statement.setQueryTimeout(30);  // set timeout to 30 sec.


         statement.executeUpdate("DROP TABLE IF EXISTS person");
         statement.executeUpdate("CREATE TABLE person (id INTEGER, name STRING)");

         int ids [] = {1,2,3,4,5};
         String names [] = {"Peter","Pallar","William","Paul","James Bond"};

         for(int i=0;i<ids.length;i++){
              statement.executeUpdate("INSERT INTO person values(' "+ids[i]+"', '"+names[i]+"')");   
         }

         //statement.executeUpdate("UPDATE person SET name='Peter' WHERE id='1'");
         //statement.executeUpdate("DELETE FROM person WHERE id='1'");

           ResultSet resultSet = statement.executeQuery("SELECT * from person");
           while(resultSet.next())
           {
              // iterate & read the result set
              System.out.println("name = " + resultSet.getString("name"));
              System.out.println("id = " + resultSet.getInt("id"));
           }
          }

     catch(SQLException e){  System.err.println(e.getMessage()); }       
      finally {         
            try {
                  if(connection != null)
                     connection.close();
                  }
            catch(SQLException e) {  // Use SQLException class instead.          
               System.err.println(e); 
             }
      }
  }
 }

代码将创建一个名为sample.db的数据库,将数据插入,然后打印行。