Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8567591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:55:04+00:00 2026-06-11T17:55:04+00:00

I am trying to INSERT some data in ms access database using jsp but

  • 0

I am trying to INSERT some data in ms access database using jsp but it shows SQLException. The query has a subquery.
I have tried to run the query in access and the query executes fine. I am not sure why the jsp is throwing exception. I have checked my query over and over again for 3 hours now but still no use. Can anyone help??

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

        //Creating new statement
        Connection conn = DriverManager.getConnection("jdbc:odbc:accdbJava");

        String uName = ((UserInfo) session.getAttribute("userInfo")).getUserName().trim();

        //Creating statement
        PreparedStatement stmt = conn.prepareStatement("INSERT INTO Message (User_ID, Heading, Body, DatePosted) VALUES ('(SELECT ID FROM User WHERE UserName = '" + uName + "')', '" + messageItem1.getSubject() + "', '" + messageItem1.getMessage() + "', '" + messageItem1.getDatePosted() + "');");

        //Executing the update
        stmt.executeUpdate();

        //Closing connection, statement
        stmt.close();
        conn.close();
    }
    catch(Exception e) {
        e.printStackTrace();
    }

The exception is as follows:

*java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression ”(SELECT ID FROM User WHERE UserName = ‘hrai’)”.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLPrepare(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.prepareStatement(Unknown Source)
at PostMessage.addMessageToDatabase(PostMessage.java:118)
at PostMessage.doPost(PostMessage.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
*

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:55:05+00:00Added an answer on June 11, 2026 at 5:55 pm

    Try the following changes

    note this will work for non-Access databases. for Access see the edit below.

    String SQLQuery = "INSERT INTO Message (User_ID, Heading, Body, DatePosted) " +
              "VALUES ((SELECT ID FROM USER WHERE username=?), ?, ?, ?)";
    
    PreparedStatement stmt = conn.prepareStatement(SQLQuery);
    stmt.setString(1, uName);
    stmt.setString(2, messageItem1.getSubject());
    stmt.setString(3,messageItem1.getMessage());
    stmt.setDate(4,messageItem1.getDatePosted());
    

    no semi-colon needed at the end of the query. By using the Prepared statement properly you can simplify your query and the string building you do.

    On a side note, this is probably all being done inside a scriptlet (<%%>) tag which isn’t good practice. You may want to move this code to a back-end java class.

    Edit – After seeing your next error, I opened Access to test the query. Your original query suffers from the problem that you have an extra set of single quotes around your inner select.

    Values ('(SELECT ID FROM User WHERE UserName = 'foo')'...
    

    that extra set of single quotes around the select gives the missing operator exception. If you remove those than you get the error from my query.
    So the two queries are near identical.

    This query would work with another database (MySQL, SQL Server, etc) but Access doesn’t follow all the SQL rules so it can be a bit hit or miss sometimes.

    Try the following modification to my original answer to perform your inserts

    String SQLQuery = "INSERT INTO MESSAGE (User_ID, Heading, Body, DatePosted) " +
        "SELECT ID, ?, ?, ? " + 
        "FROM USER " + 
        "WHERE username=?";
    
    PreparedStatement stmt = conn.prepareStatement(SQLQuery);
    
    stmt.setString(1, messageItem1.getSubject());
    stmt.setString(2,messageItem1.getMessage());
    stmt.setDate(3,messageItem1.getDatePosted());
    stmt.setString(4, uName);
    

    note that I moved around the arguments some. And there is no VALUES keyword used. This query worked for me in Access

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert some data in my database using Entity Framework model, but
I am trying to insert some data into a pysqlite database but even tho
I'm trying to insert some binary data into database using 'mysql' gem in ruby.
I'm trying to insert some data to a database on a linked server but
I'm trying to insert some data into my SQLite database. But I keeo receiving
I have function that has to write some data into database table, but after
I'm trying to insert some data into a database using an html form. The
I have a form that is trying to insert some data into an SQL
I'm trying to insert some data from a proprietary JSON database into MongoDB for
I am trying to INSERT some data into a database. I can do this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.