Search code examples
javasqlitejavafxlocaldate

java.sql.SQLException: no such column and false Period getDays result


I'm a java beginner and struggle with two problems.

1) The SQL Exception: no such column 'Ofen'

This is my Code and I want to get specific data from a SQLite Database called "kleintest.db" with 2 tables "maindata" and "Zahlwertuntertable". maindata contains the 'Ofen' entry as TEXT. The ResultSet rs should generally take all Data from maindata and the ResultSet rs2 should take the weight from Zahlwertuntertable. But running the programm now shows the mentioned error.

public static void readDB() {

    try {
        Statement stmt = connection.createStatement();
        //ResultSet rs = stmt.executeQuery("SELECT * FROM Gewichtsabnahme;");
        ResultSet rs = stmt.executeQuery("SELECT * FROM maindata;");
        ResultSet rs2 = stmt.executeQuery("SELECT * FROM Zahlwertuntertable;");

        while (rs.next()) {
            System.out.println("Ofen = " + rs.getString("Ofen"));
            System.out.println("Platznummer = " + rs.getInt("Zahlwert"));
            System.out.println("Startdatum = " + rs.getString("Startdatum"));
            LocalDate heute = LocalDate.now();
            String Datum = rs.getString("Startdatum");
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
            LocalDate Wägetag = LocalDate.parse(Datum, formatter);
            Period DiffTag = Period.between(heute, Wägetag);
            System.out.format("Tage = " + DiffTag.getDays() + "\n");            //

            System.out.println("Gewicht = " + rs2.getInt("Startgewicht")); 

        }
        rs.close();           
        rs2.close();
        connection.close();

    } catch (SQLException e) {
        System.err.println("Zugriff auf DB nicht möglich.");
        e.printStackTrace();
    }
}

The table maindata contains the following items: Laufnummer Ofen Zahlwert Startdatum

But Laufnummer is just a primary key and should not be retrieved.

2) Next Question is the thing with the Period function. This worked well but as a printed result I'll get P 1D or P 1M 2D what looks slightly confusing. I like to print just the simple amount of days like 45 or 45D and added the getDays() to my DiffTag. Now my result is -1 what makes no sense at all. What's wrong here?

Period DiffTag = Period.between(heute, Wägetag);
        System.out.format("Tage = " + DiffTag.getDays() + "\n");

Thanks for suggestions and links I may have missed. But everything I looked so far didn't point out my specific questions.


Solution

  • You can only have one result set open at a time for a Statement object so when you execute the second query agains "Zahlwertuntertable" the first one gets closed.

    So either add another statement or handle one query at a time.

    Also, right now it looks strange that you call rs.next() but never rs2.next()