Java Connect to SQLite Database Example

Sdílet
Vložit
  • čas přidán 24. 10. 2020
  • Learn how to connect to SQLite database in Java applications via JDBC. What you will learn in details:
    - What is SQLite database?
    - Download JDBC driver for SQLite
    - Code a Java program that connects, inserts and retrieves data from a SQLite database
    - How to use Maven dependency for SQLite JDBC driver
    - How to use sqlite command line tool program to manage SQLite database files
  • Věda a technologie

Komentáře • 50

  • @preciousjumuad2958
    @preciousjumuad2958 Před 2 lety +4

    sir, I have been stuck in my sqlite error for weeks now and have been contemplating on what course to shift to because i am not connecting to sqlite at all, debugging sucks, you are literally my life saver may you continue saving desperate novice programmers like me

  • @utkarshmishra1029
    @utkarshmishra1029 Před 2 lety

    after going to alot videos ...finally i got it .Thankeww Man ...Love from india

    • @CodeJava
      @CodeJava  Před 2 lety

      You're very welcome. Glad it helped!

  • @heitornolla
    @heitornolla Před 11 měsíci

    Absolutely fantastic video. 10/10. Thank you so much.

    • @CodeJava
      @CodeJava  Před 11 měsíci

      Glad you enjoyed it! You're very welcome.

  • @davidramirez3322
    @davidramirez3322 Před 3 lety +2

    Great Video, thks a lot

  • @iyviish
    @iyviish Před 2 lety +1

    Perfect video

  • @X-VIPRIN
    @X-VIPRIN Před rokem +4

    Well, thank you man! I did mine with more OOP involved, and I am hoping to make it so that I can choose my specific query in the db:
    package queryDatabaseTestUsingJDBC1;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSet;
    public class databaseManager {
    static String databaseUrl; //Store the directory/address to the database (.db) file.
    static Connection databaseConnection; //Instance of database; JDBC lib.
    static Statement sqlStatement; //Manages sql statements; JDBC lib.
    static ResultSet result; //Manages the result; JDBC lib.
    static String STUDENT_ID;
    static String STUDENT_NAME;
    static String AGE_IN_YEARS;
    //Constructor of class "databaseManager". Take in database address/directory when instane of class is created.
    public databaseManager(String Url) {
    databaseUrl = Url;
    }

    //Return the address, Url or directory of the database.
    public String getDatabaseUrl() {
    return databaseUrl;
    }

    //Connect to a database using url provided by constructor; Conntains JDBC lib.
    public void connectToDatabase() {
    try {
    databaseConnection = DriverManager.getConnection(databaseUrl);
    } catch (SQLException e) {
    System.out.print("Connection to sqlite database failed...
    ");
    e.printStackTrace();
    }
    }

    //Query the sql database, takes sql statement.
    public void queryDatabase(String passedSqlStatement) {
    try {
    sqlStatement = databaseConnection.createStatement(); //Create sql statement.
    result = sqlStatement.executeQuery(passedSqlStatement); //Execute the query on the database.

    //Print result.
    while (result.next()) { //Get all results.
    STUDENT_ID = result.getString("STUDENT_ID");
    STUDENT_NAME = result.getString("STUDENT_NAME");
    AGE_IN_YEARS = result.getString("AGE_IN_YEARS");

    System.out.print(STUDENT_ID + " | " + STUDENT_NAME + " | " + AGE_IN_YEARS + "
    ");
    }

    } catch (SQLException ea) {
    System.out.print("Error quering database...
    ");
    ea.printStackTrace();
    }

    }


    }

  • @cesarbranco1889
    @cesarbranco1889 Před 3 lety +1

    Hey, I got an error. No suitable driver found for jdbc:sqlite:/home/ceszkraft/.sqlite/client.db. On the String i put the correct location but I still got this error. I don't know what should be wrong. String jdbUrl = "jdbc:sqlite:/home/ceszkraft/.sqlite/client.db";

    • @CodeJava
      @CodeJava  Před 3 lety +1

      perhaps no SQLite jar files present in the classpath. Check for that.

  • @choysito
    @choysito Před 2 lety

    thank you so much

  • @dkg9641
    @dkg9641 Před 3 lety +2

    can you show us how to make a login gui with java/javafx that displays a different screen depending on the type of user that's logged in?

  • @mohammadzaidi8287
    @mohammadzaidi8287 Před 3 lety +2

    despite adding the jar for jdbc to the class path like shown in the video, I am still getting the error "No suitable driver found" do you have any idea how to fix this? Thank you

    • @CodeJava
      @CodeJava  Před 3 lety +1

      ensure that the JDBC url starts with jdbc:sqlite:

    • @SuperFabregas2
      @SuperFabregas2 Před 2 lety

      @@CodeJava This fixed the issue for me thanks

  • @kmer1456
    @kmer1456 Před 3 lety +2

    thankssss

  • @redness4376
    @redness4376 Před 7 měsíci

    im using intellij and i added the jar file to the libraries section of my project and made sure that it gets exported in the dependencies tab i also made sure im specifiying the correct path for it but it still dosent work im getting this error:
    Exception in thread "main" java.sql.SQLException: No suitable driver found for jbdc:sqlite:/C:\Users\username\Desktop\sql\users.db

    • @CodeJava
      @CodeJava  Před 7 měsíci

      you specified the wrong prefix 'jbdc' - it must be 'jdbc'

  • @cxl6808
    @cxl6808 Před 2 lety

    hello! i am using intellij and i added the jar file inside project structure and the jar file is in the same file location as my project, but i am still getting no suitable driver found for jdbc:sqlite:/C:...

    • @CodeJava
      @CodeJava  Před 2 lety

      Did you add the JAR file as external library or dependency? Simply copy the jar file to project directory is not working.

    • @cxl6808
      @cxl6808 Před 2 lety

      @@CodeJavahi! ahh this works now, thank you!

  • @goldenscavengerplayz9150
    @goldenscavengerplayz9150 Před 8 měsíci

    May I ask why did you specifically used Java 1.8

    • @CodeJava
      @CodeJava  Před 8 měsíci

      this video was created at the time when Java 8 was still popular.

  • @mysticknight2084
    @mysticknight2084 Před 2 lety

    Do you know how i can use the local path to the database?

    • @CodeJava
      @CodeJava  Před 2 lety

      it is something like this: jdbc:sqlite:C:/work/product.db
      Learn more: www.codejava.net/java-se/jdbc/connect-to-sqlite-via-jdbc

    • @mysticknight2084
      @mysticknight2084 Před 2 lety

      @@CodeJava i got it, ty!

  • @danishkh4489
    @danishkh4489 Před 2 lety

    How do I add the jar file in sublime text

    • @CodeJava
      @CodeJava  Před 2 lety +1

      Sublime text is not an IDE so you have to use java/javac commands which is quite hard. I recommend you use Eclipse/NetBeans/IntelliJ.

    • @danishkh4489
      @danishkh4489 Před 2 lety

      @@CodeJava ok Thanks : ›

  • @rainermitai1337
    @rainermitai1337 Před měsícem +1

    ja ähm das war sehr sehr lecker

  • @X-VIPRIN
    @X-VIPRIN Před rokem

    11:30 Did this man just declare a variable in a while loop?

    • @CodeJava
      @CodeJava  Před rokem

      that while statement calls result.next() repeatedly until it returns false.

    • @X-VIPRIN
      @X-VIPRIN Před rokem

      @@CodeJava Yeah, I suppose, but Wouldn't the application have to declare the same variable multiple times? This would waste system resources. I think it is more efficent to declare the variable as a static variable, which we can then call from anywhere, in the class, and we can just overide its value, instead of craeting the variable over an over again. I tested this, by making an application that prints a String, 100,000 times to stdio, and found that it was more efficent by about 12% to use a static variable, rather than declaring the variable over and over again, rewriting the varaibles name and address each time.

    • @X-VIPRIN
      @X-VIPRIN Před rokem

      @@CodeJava But to do fair, after the 100th time of printing the same String, its value is probably just inserted into the systems CPU cache, as it is used alot. With my knowledge of low-level, information that is used alot is moved into the cache for faster access by the hardware.

    • @X-VIPRIN
      @X-VIPRIN Před rokem

      @@CodeJava But as you said, the variable is changed to a different value each time, but the variable still has to be rewritten to random access memory in a allocated place each time, so Im not sure. I know if we where in C, declaring a variable in a while loop would probably be a problem?

  • @X-VIPRIN
    @X-VIPRIN Před rokem

    6:03

  • @michaelumeokoli
    @michaelumeokoli Před 8 měsíci

    I use vscode, and I've added the jar file to my referenced libraries but it still doesn't work.
    C:\Users\USER\Desktop\Java-Stuff\sqlitedemo> c: && cd c:\Users\USER\Desktop\Java-Stuff\sqlitedemo && cmd /C ""C:\Program Files\Java\jdk-20\bin\java.exe" @C:\Users\USER\AppData\Local\Temp\cp_8o4t59k8gaxyvyxag6ii58hib.argfile App "
    error connecting to db
    java.sql.SQLException: No suitable driver found for jdbc:sqlite:/usersDB.db
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:708)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:253)
    at App.main(App.java:10)

    • @CodeJava
      @CodeJava  Před 8 měsíci

      I think you should specify the jar file of SQLite via -cp flag. Reference: www.codejava.net/java-core/tools/examples-of-using-java-command