我必須使用PreparedStatement在表的字段上創建一個索引。我所要執行的查詢如下:Java PreparedStatement和Alter Table語法錯誤
ALTER TABLE esa_matrix ADD INDEX doc_index (id_doc)
所以,我創建一個PreparedStatement
實例與查詢相同的文字和對其執行executeUpdate()
方法。但在執行時,我得到一個SQL語法錯誤。 這是創建PreparedStatement
實例:
PreparedStatement ps = conn.prepareStatement("ALTER TABLE "+ESATable+ "ADD INDEX doc_index ("+idDocLabel+")");
ps.executeUpdate();
ps.close();
此SQLException我得到:
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 'doc_index (id_doc)' at line 1
我怎樣才能解決這個問題?
由於提前,
安東尼
我鼓勵你爲這種「stringconstruction」使用String.format()而不僅僅是DB-querys。 'String query = String.format(「ALTER TABLE%s ADD INDEX doc_index(%s)」,ESATable,idDocLabel);' – 2012-10-24 12:21:06