RcTomcat
RcTomcat

Reputation: 7

SQL Error or missing database syntax error

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.

Upvotes: 0

Views: 383

Answers (2)

user11264239
user11264239

Reputation:

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.

Upvotes: 0

MikeT
MikeT

Reputation: 56938

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.

Upvotes: 1

Related Questions