0

I have a database that I manage through a jsp file. I want to add some data to it, but I need the data to be taken from a String variable. I tried the following code but the "VALUES" parameter doesn't accept variables. Any help?

function UploadNot() {
     <%
          String NotificationToUpload = "Test Notification";
          Class.forName("org.sqlite.JDBC");
          conn = DriverManager.getConnection("jdbc:sqlite:d:\\Databases\\DataBase1.db");
          stat = conn.createStatement();
          String sql = "INSERT INTO Nots (Notifications) " +
               "VALUES (NotificationToUpload);";  //ERROR here, Does not accept variables, only direct Strings
          stat.executeUpdate(sql);
     %>
}

1 Answer 1

1

You forget the "+" operator in order to concatenate your value.

String sql = "INSERT INTO Nots (Notifications) VALUES ('" +NotificationToUpload+ "');"; 
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't work, I get a syntax error: "javax.servlet.ServletException: org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (near "Notification": syntax error)"
You have to quote the value if it's not an integer field.
I edited your correction, you added incorrect whitespaces.

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.