如何将 SQLite 与 Java 连接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1525444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:49:31  来源:igfitidea点击:

How to connect SQLite with Java?

javasqlitejdbcclasspath

提问by Rajapandian

I am using one simple code to access the SQLite database from Java application . My code is

我正在使用一个简单的代码从 Java 应用程序访问 SQLite 数据库。我的代码是

 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
 public class ConnectSQLite 
 {  
  public static void main(String[] args) 
  {  
     Connection connection = null;  
     ResultSet resultSet = null;  
     Statement statement = null;  

     try 
     {  
         Class.forName("org.sqlite.JDBC");  
         connection = DriverManager.getConnection("jdbc:sqlite:D:\testdb.db");  
         statement = connection.createStatement();  
         resultSet = statement  
                 .executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");  
         while (resultSet.next()) 
         {  
             System.out.println("EMPLOYEE NAME:"  
                     + resultSet.getString("EMPNAME"));  
         }  
     } 
     catch (Exception e) 
     {  
         e.printStackTrace();  
     }
     finally 
     {  
         try 
         {  
             resultSet.close();  
             statement.close();  
             connection.close();  
         } 
         catch (Exception e) 
         {  
             e.printStackTrace();  
         }  
     }  
 }  
}  

But this code gives one exception like

但是这段代码给出了一个例外,比如

java.lang.ClassNotFoundException: org.sqlite.JDBC

How can I slove this,please help me.

我怎么能解决这个问题,请帮助我。

采纳答案by Reverend Gonzo

You need to have a SQLite JDBC driver in your classpath.

您的类路径中需要有一个 SQLite JDBC 驱动程序。

Taro L. Saito (xerial) forked the Zentus project and now maintains it under the name sqlite-jdbc. It bundles the native drivers for major platforms so you don't need to configure them separately.

Taro L. Saito (xerial) fork Zentus 项目,现在以sqlite-jdbc的名义维护它。它捆绑了主要平台的本机驱动程序,因此您无需单独配置它们。

回答by lesce

If you are using netbeans Download the sqlitejdbc driverRight click the Libraries folder from the Project window and select Add Library , then click on the Create button enter the Library name (SQLite) and hit OK

如果您使用的是 netbeans 下载sqlitejdbc 驱动程序右键单击“项目”窗口中的“库”文件夹并选择“添加库”,然后单击“创建”按钮,输入库名称 (SQLite) 并单击“确定”

You have to add the sqlitejdbc driver to the class path , click on the Add Jar/Folder.. button and select the sqlitejdbc file you've downloaded previously Hit OK and you are ready to go !

您必须将 sqlitejdbc 驱动程序添加到类路径中,单击 Add Jar/Folder.. 按钮并选择您之前下载的 sqlitejdbc 文件点击 OK,您就可以开始了!

回答by MeanderingCoder

I'm using Eclipse and I copied your code and got the same error. I then opened up the project properties->Java Build Path -> Libraries->Add External JARs... c:\jrun4\lib\sqlitejdbc-v056.jar Worked like a charm. You may need to restart your web server if you've just copied the .jar file.

我正在使用 Eclipse,我复制了您的代码并遇到了同样的错误。然后我打开了项目 properties->Java Build Path -> Libraries->Add External JARs... c:\jrun4\lib\sqlitejdbc-v056.jar 像魅力一样工作。如果您刚刚复制了 .jar 文件,则可能需要重新启动 Web 服务器。

回答by Vasanth Kumar

connection = DriverManager.getConnection("jdbc:sqlite:D:\testdb.db");

Instead of this put

而不是这个 put

connection = DriverManager.getConnection("jdbc:sqlite:D:\testdb");

回答by fiddle

Hey i have posted a video tutorial on youtube about this, you can check that and you can find here the sample code :

嘿,我在 youtube 上发布了一个关于这个的视频教程,你可以检查一下,你可以在这里找到示例代码:

http://myfundatimemachine.blogspot.in/2012/06/database-connection-to-java-application.html

http://myfundatimemachine.blogspot.in/2012/06/database-connection-to-java-application.html

回答by Deepak S. Gavkar

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;   


public class Connectdatabase {

        Connection con = null;

        public static Connection ConnecrDb(){

            try{
                //String dir = System.getProperty("user.dir");
                Class.forName("org.sqlite.JDBC");
                Connection con = DriverManager.getConnection("jdbc:sqlite:D:\testdb.db");
                return con;
            }
            catch(ClassNotFoundException | SQLException e){
                JOptionPane.showMessageDialog(null,"Problem with connection of database");
                return null;
            }
        }

    }

回答by user2402433

    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.swing.JOptionPane;
    import org.sqlite.SQLiteDataSource;
    import org.sqlite.SQLiteJDBCLoader;

    public class Test {

        public static final boolean Connected() {
            boolean initialize = SQLiteJDBCLoader.initialize();

            SQLiteDataSource dataSource = new SQLiteDataSource();
            dataSource.setUrl("jdbc:sqlite:/home/users.sqlite");
            int i=0;
            try {
                ResultSet executeQuery = dataSource.getConnection()
                        .createStatement().executeQuery("select * from \"Table\"");
                while (executeQuery.next()) {
i++;
                    System.out.println("out: "+executeQuery.getMetaData().getColumnLabel(i));

                }



            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex);
            }

            return initialize;

        }

回答by Olcay Erta?

If you are using Netbeansusing Mavento add library is easier. I have tried using above solutions but it didn't work.

如果您使用Netbeans使用Maven添加库更容易。我曾尝试使用上述解决方案,但没有奏效。

<dependencies>
    <dependency>
      <groupId>org.xerial</groupId>
      <artifactId>sqlite-jdbc</artifactId>
      <version>3.7.2</version>
    </dependency>
</dependencies>

I have added Mavendependency and java.lang.ClassNotFoundException: org.sqlite.JDBCerror gone.

我添加了Maven依赖项,java.lang.ClassNotFoundException: org.sqlite.JDBC错误消失了。

回答by Thev

You have to download and add the SQLite JDBC driver to your classpath.
You can download from here https://bitbucket.org/xerial/sqlite-jdbc/downloads

您必须下载 SQLite JDBC 驱动程序并将其添加到您的类路径中。
您可以从这里下载https://bitbucket.org/xerial/sqlite-jdbc/downloads

If you use Gradle, you will only have to add the SQLite dependency:

如果您使用 Gradle,则只需添加 SQLite 依赖项:

dependencies {
    compile 'org.xerial:sqlite-jdbc:3.8.11.2'
} 

Next thing you have to do is to initialize the driver:

接下来你要做的是初始化驱动程序:

try {
    Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException eString) {
    System.err.println("Could not init JDBC driver - driver not found");
}