0

I get an sql error when trying to insert something into my DB.

I give a bunch of input to my method, convert that input into strings or sql time and want to store it.

   public static void setCourseList(String courseDescription, String courseName, LocalTime courseStart, LocalTime courseEnd, LocalDate courseDate, DayOfWeek courseDay) {
 Connection conn = null;
        try {
            // db parameters
            // path to db relative to run time directory
            String url = "jdbc:sqlite:Holiday.db";


        String sqlInsertCourse = "INSERT INTO COURSE (Name,Start,End,Date,Day,Description) VALUES (?, ?,?, ?,?, ?,);";

   conn = DriverManager.getConnection(url);
            System.out.println("Connected");
         Statement stmt = conn.createStatement();
         PreparedStatement pstmt = conn.prepareStatement(sqlInsertCourse);
         pstmt.setString(1, courseName);
         String courseStartString = courseStart.toString();
pstmt.setString(2, courseStartString);
java.sql.Time courseEndTime = Time.valueOf(courseEnd);
pstmt.setTime(3, courseEndTime);
java.sql.Date courseDateDate = java.sql.Date.valueOf(courseDate);
pstmt.setDate(4, courseDateDate);
String courseDayString = courseDay.toString();
pstmt.setString(5, courseDayString);
pstmt.executeUpdate();
pstmt.close();


            System.out.println("Connection to SQLite has been established.");
// create tables if they do not exists
            stmt.execute(sqlInsertCourse);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException ex) {
                System.out.println(ex.getMessage());
            }
        }
        }


I would expect it to store the input in my db. I do get an [SQLITE_ERROR] SQL error or missing database (near ")": syntax error) error instead.

Any help is appreciated. I am new to sql.

2
  • 2
    There is one comma too much behind the last question mark in your statement. Commented Jun 7, 2019 at 9:16
  • Thank you, i somehow missed that. Tricky beasts those commas ;) Commented Jun 7, 2019 at 11:28

2 Answers 2

1

Change

String sqlInsertCourse = "INSERT INTO COURSE (Name,Start,End,Date,Day,Description) VALUES (?, ?,?, ?,?, ?,);";

To

String sqlInsertCourse = "INSERT INTO COURSE (Name,Start,End,Date,Day,Description) VALUES (?, ?,?, ?,?, ?);"; //<<<<<<<<<< extra comma removed

As per the comment on the line the final comma after the last ? has been removed.

Sign up to request clarification or add additional context in comments.

Comments

0

Same as what Mike has answered, you can change it to

    String sqlInsertCourse = "INSERT INTO COURSE (Name,Start,End,Date,Day,Description) VALUES (""put values here"");";

If you are wondering why it doesn't throw you an error, it's because there is no syntax error in the java, there's an error in the SQL which only the database can throw, but you're computer can't recognize. Hope this answers your question.

1 Comment

So you mean like ``` String sqlInsertCourse = "INSERT INTO COURSE (Name,Start,End,Date,Day,Description) VALUES (""courseName,courseStart,courseend..."");"; ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.