[英]execute SQL statement using executeQuery in servlet
I have 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();
}
}
I check all about connection
and it was OK . 我检查了所有有关
connection
,一切正常。
When it reach to the line ResultSet resultWithAccounts = prepareSQL.executeQuery();
当到达行
ResultSet resultWithAccounts = prepareSQL.executeQuery();
it throws exception - 它引发异常-
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)
The SQL code of the 2 above tables is - 上面两个表的SQL代码是-
CREATE TABLE `account_person` (
`accountNum` INT( 11 ) NOT NULL ,
`personID` TEXT NOT NULL
)
AND 和
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'
)
What wrong in the SQL statement in prepareSQL
? prepareSQL
的SQL语句中有什么错误?
I suspect: 我猜测:
+ "WHERE (account.accountNum = account_person.accountNum"
+ "AND account_person.personID=? )ORDER BY account.account
means you're concatenating accountNum
and AND
without an intermediate space. 表示您要串联的
accountNum
和AND
中间没有空格。 That ties in with the error message. 这与错误消息有关。
Try removing the pharentesis and add a space: 尝试删除相貌并添加一个空格:
@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();
}
}
There is an error in the SQL string. SQL字符串中有错误。 You should delete the parenthesis that sorround the FROM section
您应该删除包围FROM部分的括号
String strAllAccocuntsSQL = "SELECT * FROM account, account_person "
+ "WHERE (account.accountNum = account_person.accountNum"
+ "AND account_person.personID=? )ORDER BY account.accountNum;";
I would also cancel the ; 我也取消; at the end, if it doesnt work yet
最后,如果还不行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.