我必须使用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