![](/img/trans.png)
[英]Statement.execute(sql) vs executeUpdate(sql) and executeQuery(sql)
[英]execute SQL statement using executeQuery in servlet
我有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)
上面兩個表的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'
)
prepareSQL
的SQL語句中有什么錯誤?
我猜測:
+ "WHERE (account.accountNum = account_person.accountNum"
+ "AND account_person.personID=? )ORDER BY account.account
表示您要串聯的accountNum
和AND
中間沒有空格。 這與錯誤消息有關。
嘗試刪除相貌並添加一個空格:
@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();
}
}
SQL字符串中有錯誤。 您應該刪除包圍FROM部分的括號
String strAllAccocuntsSQL = "SELECT * FROM account, account_person "
+ "WHERE (account.accountNum = account_person.accountNum"
+ "AND account_person.personID=? )ORDER BY account.accountNum;";
我也取消; 最后,如果還不行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.