#Creating tables in Sqlite with Java 8

14 messages · Page 1 of 1 (latest)

vernal lavaBOT
#

This post has been reserved for your question.

Hey @modern spruce! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

modern spruce
#

I can see that stmt is null but how is conn.createStatement() returning null if the connection is not null?

vernal lavaBOT
#

<@&765578700724371486>

Requested by Spring#9999
next flicker
#

conn is closed at that point

#

and you cannot use it after it is closed

modern spruce
#

How did it close? Did I not open it here

try (Connection conn = DriverManager.getConnection(url)) {
            if (conn != null) {
                DatabaseMetaData meta = conn.getMetaData();
                this.conn = conn;
            } else{
                Util.tellConsole("Conn is null");
            }
        } catch (SQLException e) {
            Util.tellConsole(e.getMessage());
next flicker
#

try closes the resource once the end of the try-block is reached

#
try (Connection conn = DriverManager.getConnection(url)) {
            if (conn != null) {
                DatabaseMetaData meta = conn.getMetaData();
                this.conn = conn;
            } else{
                Util.tellConsole("Conn is null");
            }
        } //CLOSED HERE
        catch (SQLException e) {
            Util.tellConsole(e.getMessage());
modern spruce
#

Ahh, I see I was not aware of that. Thank you.

next flicker
#

don't use a try with resources statement

#
    public Connection getDBConnection() {
        String url = "jdbc:sqlite:HarvHoe.db";
        try {
            Connection conn = DriverManager.getConnection(url)
            if (conn != null) {
                return conn;
            } else{
                Util.tellConsole("Conn is null");
            }
        } catch (SQLException e) {
            Util.tellConsole(e.getMessage();
        }
        return null;
    }
#

but you need to make sure you are closing the Connection once you are finished

vernal lavaBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.