How to connect SQLite with Java?

JavaSqliteJdbcClasspath

Java Problem Overview


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

 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.

Java Solutions


Solution 1 - Java

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

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.

Solution 2 - Java

If you are using netbeans Download the http://files.zentus.com/sqlitejdbc/sqlitejdbc-v056.jar">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

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 !

Solution 3 - Java

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

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

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

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

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

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

Solution 7 - Java

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

Instead of this put

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

Solution 8 - Java

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

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

Solution 9 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajapandianView Question on Stackoverflow
Solution 1 - JavaReverend GonzoView Answer on Stackoverflow
Solution 2 - JavalesceView Answer on Stackoverflow
Solution 3 - JavaOlcay ErtaşView Answer on Stackoverflow
Solution 4 - JavaMeanderingCoderView Answer on Stackoverflow
Solution 5 - Javauser2402433View Answer on Stackoverflow
Solution 6 - JavaThevView Answer on Stackoverflow
Solution 7 - JavaVasanth KumarView Answer on Stackoverflow
Solution 8 - JavafiddleView Answer on Stackoverflow
Solution 9 - JavaDeepak S. GavkarView Answer on Stackoverflow