简体   繁体   English

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

[英]need help setting up SQLite on eclipse with java for the first time

I have been trying to figure out how to get SQLite working on eclipse juno. 我一直试图弄清楚如何让SQLite在eclipse juno上工作。 I have been following the instructions on this site http://wiki.eclipse.org/Connecting_to_SQLite . 我一直按照本网站上的说明http://wiki.eclipse.org/Connecting_to_SQLite The problem is not every step is exactly as explained so I am guessing on weather I got it right or not. 问题不在于每个步骤都与解释完全一致,因此我猜测天气是否正确。 I feel that I have probably gotten it all correct until step 13, there is no SQL Model-JDBC Connection entry. 我觉得我可能已经完全正确,直到第13步,没有SQL Model-JDBC Connection条目。 So I have tried step 13-16 with a generic JDBC and with one that says SQLite. 所以我尝试使用通用JDBC和使用SQLite的步骤13-16。 The SQLite one does not have a driver which is no surprise due to step 5. Any way that I have tried so far ends up failing ping with details listed below. SQLite没有一个驱动程序,由于步骤5没有任何意外。我到目前为止尝试过的任何方式都会导致ping失败,下面列出了详细信息。 Someone must have a better way through this process. 有人必须有更好的方法来完成这个过程。

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)

Make sure you get the driver from https://bitbucket.org/xerial/sqlite-jdbc/downloads , then import the driver into your project. 确保从https://bitbucket.org/xerial/sqlite-jdbc/downloads获取驱动程序,然后将驱动程序导入到项目中。

在此输入图像描述

在此输入图像描述

Now you can test the configuration by creating a java class Sample.java 现在,您可以通过创建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); 
             }
      }
  }
 }

The code will create a database named sample.db , inserting data into, and then prints the rows. 代码将创建一个名为sample.db的数据库,将数据插入,然后打印行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM