我正在尝试使用 jsp 在 ms access 数据库中插入一些数据,但它显示 SQLException。该查询有一个子查询。 我试图在访问中运行查询并且查询执行得很好。我不确定jsp为什么会抛出异常。我已经一遍又一遍地检查了我的查询 3 个小时,但仍然没有用。有人可以帮忙吗??
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();
}
例外情况如下:
*java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 查询表达式 ''(SELECT ID FROM User WHERE UserName = 'hrai')'' 中的语法错误(缺少运算符)。在 sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source) 在 sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source) 在 sun.jdbc.odbc.JdbcOdbc.SQLPrepare(Unknown Source) 在 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 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.