热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何用Java连接SQLite?-HowtoconnectSQLitewithJava?

IamusingonesimplecodetoaccesstheSQLitedatabasefromJavaapplication.Mycodeis我使用一个简单的

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.

我怎么能做到这一点,请帮帮我。

9 个解决方案

#1


53  

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)放弃了Zentus项目,现在以sqlite-jdbc的名义维护它。它将本地驱动程序绑定到主要平台,因此您不需要单独配置它们。

#2


8  

If you are using netbeans Download the sqlitejdbc driver Right 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驱动程序,请右键单击项目窗口中的Libraries文件夹并选择Add Library,然后单击Create按钮,输入库名称(SQLite)并点击OK。

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/文件夹。按下按钮,选择你下载的sqlitejdbc文件,点击OK,就可以开始了!

#3


7  

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

如果您使用的是使用Maven来添加库的Netbeans,那就更容易了。我试过用上面的方法,但没有用。


    
      org.xerial
      sqlite-jdbc
      3.7.2
    

I have added Maven dependency and java.lang.ClassNotFoundException: org.sqlite.JDBC error gone.

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

#4


5  

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,我复制了您的代码并得到了相同的错误。然后我打开了项目属性->Java构建路径->库->添加外部jar…c:\ jrun4 \ lib \ sqlitejdbc-v056。jar工作起来很有魅力。如果您刚刚复制了.jar文件,您可能需要重新启动web服务器。

#5


3  

    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;

        }

#6


1  

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

Instead of this put

而不是这把

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

#7


1  

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

#8


1  

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");
}

#9


0  

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;
            }
        }

    }

推荐阅读
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • DropDownList分层显示!
    publicstaticvoidBindDropFatherItem(DropDownListDropDownList){DropDownList.Items.Clear();st ... [详细]
  • SQL   AS关键字
    AS关键字1.as是别名关键字,换句话说就是重新给sql某个字段取个别名的关键字,但as本身并不改变sql的字段的名称,只是在使用的时候有 ... [详细]
  • 1.添加工具类MDBHelp.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSys ... [详细]
  • SqlBulkCopy 极速插入数据
    代码SqlBulkCopysqlBulkCopynull;声明SqlBulkCopy对象try{DbHelperdbnewDbHelper();微软DbHelper类using(S ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 本文讨论了在使用sp_msforeachdb执行动态SQL命令时,当发生错误时如何捕获数据库名称。提供了两种解决方案,并介绍了如何正确使用'?'来显示数据库名称。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • OleDbDataAdapter充当DataSet和数据源之间的桥梁,用于检索和保存数据。OleDbDataAdapter通过以下方法提供这个桥接器:使用Fill将数据从数 ... [详细]
  • 我正在一个涉及SQLite的项目中,我只有一个数据库文件,现在我正在测试我的应 ... [详细]
  • 州的先生(https:zmister.com)在很多项目中都有使用到SQLite数据库作为数据存储的工具,其中包括一些桌面图形界面程序和线上的Web应用程序。至今为止,它们都运行良 ... [详细]
  • Python使用SQLite1.sqlite3的安装python2.5.x以上版本默认自带sqlite3模块。2.链接sqlite3数据库```#导入sqlite3模块import ... [详细]
  • 背景使用sqlite3的命令实现数据去重,与无效数据删除等操作。所有操作均封装在shellscript中。创建数据库邮件数据库:UserEmail.dbEmail表:TABLE_EM ... [详细]
author-avatar
花儿在绽放2502857073
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有