Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
hackerss
hackerss

Posted on

Java Connect to sqlite database and query an example table


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ConnectToSqlite {

    public static void main(String[] args) throws Exception {
        //Connect to the database
        Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\josep\\Documents\\GitHub\\Java-Training\\Java-Training\\src\\main\\resources\\sqlite.db");
        //Create a statement object
        Statement stmt = conn.createStatement();
        //Execute the query and store the result in a ResultSet object
        ResultSet rs = stmt.executeQuery("SELECT * FROM example");
        //Iterate through the result set and print the values
        List<String> list = new ArrayList<String>();
        while (rs.next()) {
            list.add(rs.getString("name"));
        }
        System.out.println(list);
    }
}

Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)