Java Connect to SQLite Database Example

Поделиться
HTML-код
  • Опубликовано: 24 окт 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
  • НаукаНаука

Комментарии • 50

  • @preciousjumuad2958
    @preciousjumuad2958 2 года назад +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

    • @CodeJava
      @CodeJava  2 года назад +3

      Glad this video helps. thanks for watching :)

    • @eulysispabellan7355
      @eulysispabellan7355 2 года назад +2

      Oy precious, Nice to see you here LMAO

    • @preciousjumuad2958
      @preciousjumuad2958 2 года назад +1

      HI EUL! HAHA SMALL WORLD! 😭🤣

    • @nelfranklamba6402
      @nelfranklamba6402 2 года назад +1

      Dugo akong ilong sa english paki translate daw sa bisaya by the way Nel akong name

    • @eulysispabellan7355
      @eulysispabellan7355 2 года назад

      @@nelfranklamba6402 shshhsh nangalapait mis java frank

  • @utkarshmishra1029
    @utkarshmishra1029 2 года назад

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

    • @CodeJava
      @CodeJava  2 года назад

      You're very welcome. Glad it helped!

  • @heitornolla
    @heitornolla 10 месяцев назад

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

    • @CodeJava
      @CodeJava  10 месяцев назад

      Glad you enjoyed it! You're very welcome.

  • @dkg9641
    @dkg9641 3 года назад +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?

    • @CodeJava
      @CodeJava  3 года назад +2

      Noted. I will.

  • @cesarbranco1889
    @cesarbranco1889 3 года назад +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  3 года назад +1

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

  • @mohammadzaidi8287
    @mohammadzaidi8287 3 года назад +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  3 года назад +1

      ensure that the JDBC url starts with jdbc:sqlite:

    • @SuperFabregas2
      @SuperFabregas2 2 года назад

      @@CodeJava This fixed the issue for me thanks

  • @iyviish
    @iyviish 2 года назад +1

    Perfect video

  • @davidramirez3322
    @davidramirez3322 3 года назад +2

    Great Video, thks a lot

    • @CodeJava
      @CodeJava  3 года назад

      Glad you liked it!

  • @redness4376
    @redness4376 7 месяцев назад

    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  7 месяцев назад

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

  • @X-VIPRIN
    @X-VIPRIN Год назад +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();
    }

    }


    }

  • @cxl6808
    @cxl6808 2 года назад

    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  2 года назад

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

    • @cxl6808
      @cxl6808 2 года назад

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

  • @choysito
    @choysito 2 года назад

    thank you so much

  • @goldenscavengerplayz9150
    @goldenscavengerplayz9150 8 месяцев назад

    May I ask why did you specifically used Java 1.8

    • @CodeJava
      @CodeJava  8 месяцев назад

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

  • @mysticknight2084
    @mysticknight2084 2 года назад

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

    • @CodeJava
      @CodeJava  2 года назад

      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 2 года назад

      @@CodeJava i got it, ty!

  • @danishkh4489
    @danishkh4489 2 года назад

    How do I add the jar file in sublime text

    • @CodeJava
      @CodeJava  2 года назад +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 2 года назад

      @@CodeJava ok Thanks : ›

  • @kmer1456
    @kmer1456 3 года назад +2

    thankssss

  • @X-VIPRIN
    @X-VIPRIN Год назад

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

    • @CodeJava
      @CodeJava  Год назад

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

    • @X-VIPRIN
      @X-VIPRIN Год назад

      @@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 Год назад

      @@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 Год назад

      @@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?

  • @rainermitai1337
    @rainermitai1337 26 дней назад +1

    ja ähm das war sehr sehr lecker

  • @X-VIPRIN
    @X-VIPRIN Год назад

    6:03

  • @michaelumeokoli
    @michaelumeokoli 8 месяцев назад

    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  8 месяцев назад

      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