2013-07-01 60 views

回答

5

看起來您正試圖爲SQL構建字符串。應該使用PreparedStatement來代替此目的。

PreparedStatement preparedStatement = 
     connection.prepareStatement("select * from x where name = ?"); 
preparedStatement.setString(1, "John"); 

編輯:

假設你使用EntityManager,你可以使用它的等效setParameter

Query q = 
entityManager.createNativeQuery("select * from x where name = ?", MyClass.class); 
q.setParameter(1, "John"); 
+0

相同的答案,但我是太快 –

+0

@nelsonjuan什麼是試圖通過附加在這裏做? –

+0

@Ruchira我實際上使用javax.persistence.EntityManager來照顧數據庫。我正準備將字符串轉換爲em.createNativeQuery(sb.toString(),SomeClass.class).getResultList() –

1

這可能會幫助你。其中con是連接

PreparedStatement preStatement = con.prepareStatement("select * from x where name = ?"); 
    preStatement.setString(1, "John");