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 Наука
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
Glad this video helps. thanks for watching :)
Oy precious, Nice to see you here LMAO
HI EUL! HAHA SMALL WORLD! 😭🤣
Dugo akong ilong sa english paki translate daw sa bisaya by the way Nel akong name
@@nelfranklamba6402 shshhsh nangalapait mis java frank
after going to alot videos ...finally i got it .Thankeww Man ...Love from india
You're very welcome. Glad it helped!
Absolutely fantastic video. 10/10. Thank you so much.
Glad you enjoyed it! You're very welcome.
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?
Noted. I will.
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";
perhaps no SQLite jar files present in the classpath. Check for that.
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
ensure that the JDBC url starts with jdbc:sqlite:
@@CodeJava This fixed the issue for me thanks
Perfect video
Great Video, thks a lot
Glad you liked it!
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
you specified the wrong prefix 'jbdc' - it must be 'jdbc'
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();
}
}
}
great job!
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:...
Did you add the JAR file as external library or dependency? Simply copy the jar file to project directory is not working.
@@CodeJavahi! ahh this works now, thank you!
thank you so much
You're welcome!
May I ask why did you specifically used Java 1.8
this video was created at the time when Java 8 was still popular.
Do you know how i can use the local path to the database?
it is something like this: jdbc:sqlite:C:/work/product.db
Learn more: www.codejava.net/java-se/jdbc/connect-to-sqlite-via-jdbc
@@CodeJava i got it, ty!
How do I add the jar file in sublime text
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.
@@CodeJava ok Thanks : ›
thankssss
You're welcome!
11:30 Did this man just declare a variable in a while loop?
that while statement calls result.next() repeatedly until it returns false.
@@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.
@@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.
@@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?
ja ähm das war sehr sehr lecker
6:03
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)
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