0

我有 servlet -

@WebServlet("/servlet123")
public class servlet123 extends HttpServlet {


    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
                String usrID = "111" ; 
            String strAllAccocuntsSQL = "SELECT * (FROM account, account_person) "
                    + "WHERE (account.accountNum = account_person.accountNum"
                    + "AND account_person.personID=? )ORDER BY account.accountNum;";
            PreparedStatement prepareSQL = connection
                    .prepareStatement(strAllAccocuntsSQL);
                        prepareSQL.setString(1, usrID);   
            ResultSet resultWithAccounts = prepareSQL.executeQuery();
    }

}

我检查了一切connection,一切正常。

当它到达该行时ResultSet resultWithAccounts = prepareSQL.executeQuery();,它会抛出异常 -

 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'account_person.personID='111' )ORDER BY account.accountNum' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2468)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2629)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2719)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2318)

上表2的SQL代码是——

CREATE TABLE  `account_person` (
 `accountNum` INT( 11 ) NOT NULL ,
 `personID` TEXT NOT NULL
)

CREATE TABLE  `account` (
 `accountNum` INT( 11 ) NOT NULL ,
 `dateCreated` DATE NOT NULL ,
 `accountName` TEXT,
 `description` TEXT,
 `statusAccount` TEXT,
 `sumOfMoney` INT( 11 ) NOT NULL DEFAULT  '0'
)

中的 SQL 语句有什么问题prepareSQL

4

3 回答 3

3

我猜测:

+ "WHERE (account.accountNum = account_person.accountNum"
+ "AND account_person.personID=? )ORDER BY account.account

表示您正在连接accountNum并且AND没有中间空间。这与错误消息有关。

于 2012-08-07T15:43:31.517 回答
0

尝试删除 pharentesis 并添加一个空格:

@WebServlet("/servlet123")
public class servlet123 extends HttpServlet {


    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
                String usrID = "111" ; 
            String strAllAccocuntsSQL = "SELECT * FROM account, account_person "
                    + "WHERE account.accountNum = account_person.accountNum "
                    + "AND account_person.personID=? ORDER BY account.accountNum;";
            PreparedStatement prepareSQL = connection
                    .prepareStatement(strAllAccocuntsSQL);
                        prepareSQL.setString(1, usrID);   
            ResultSet resultWithAccounts = prepareSQL.executeQuery();
    }

}
于 2012-08-07T15:44:17.273 回答
0

SQL 字符串中有错误。您应该删除 FROM 部分周围的括号

String strAllAccocuntsSQL = "SELECT * FROM account, account_person "
                    + "WHERE (account.accountNum = account_person.accountNum"
                    + "AND account_person.personID=? )ORDER BY account.accountNum;";

我也会取消 ; 最后,如果它还不起作用

于 2012-08-07T15:48:05.123 回答