在進行數據庫方面的編程的時候,難免要拼接SQL語句,一般不帶單引號的語句比較容易,如果有單引號,則稍微難一丟丟。下面以一個示例進行說明:
String sql = "select * from stuDB where SID='"+stuId+"'";
假設不需要動態傳參數的時候,語句為:select * from stuDB where SID ='1409044101';
進行如下步驟:
1. select * from stuDB where SID = ' '; 然后在' ' 中添加 " "
2. select * from stuDb where SID = ' " " '; 然后在 " " 中添加 + + ,兩個+號
3. select * from stuDB where SID = ' "++ " ' ; 然后在 ++ 中添加 要動態傳入的字符串
4. select *from stuDB where SID = ' " +stuId+ " '; 然后把語句 復制到 String sql = " "; 中的引號中