改行を含む SQL ステートメントに文字列を渡そうとしたため、エラーが発生しました。
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 'WHERE nric = 'S3456789A\n'' at line 1
だから私は実行して改行を削除しました
nric = nric.replace("\n","");
それから私にエラーを投げました:
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 'WHERE nric = 'S3456789A'' at line 1
問題は、私が System.out.println を実行したときに、アポストロフィが表示されなかったということS3456789A
ですS3456789A'
。アポストロフィを削除するにはどうすればよいですか?
コード:
Androidからデータを取得して投稿する:
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("nric",nric));
response = CustomHttpClient.executeHttpPost("http://172.27.176.181:8080/SRD/SaveLocation", postParameters);
サーブレット SaveLocation からの必要な部分:
String nric = request.getParameter("nric");
nric = nric.replace("\n", "");
User elder = new User();
elder.setNric(nric);
try {
if(elder.retrieveUserWithNric())
{
User エンティティ クラスから UserWithNric を取得します。
public boolean retrieveUserWithNric() throws SQLException
{
boolean success = false;
ResultSet rs = null;
try {
Context ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/srd");
} catch (NamingException e) {
System.out.println("User: Naming Exception");
e.printStackTrace();
}
Connection conn = ds.getConnection();
PreparedStatement pstmt = null;
String dbQuery = "SELECT * FROM User WHERE nric = ?";
System.out.println("retrieveUserwithNric nric is "+nric);
try {
pstmt = conn.prepareStatement(dbQuery);
pstmt.setString(1, nric);
rs = pstmt.executeQuery();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
if (rs.next()) {
id = rs.getInt("id");
nric = rs.getString("nric");
password = rs.getString("password");
salt = rs.getString("salt");
name = rs.getString("name");
mobileNo = rs.getInt("mobile_no");
address = rs.getString("address");
postal = rs.getInt("postal_code");
relativeElderly = rs.getString("relative_elderly");
role = rs.getString("role");
organization = rs.getString("organization");
elderlyList = rs.getString("elderly_list");
// image = rs.getBlob("image");
success = true;
}
else
{
System.out.println("rs does not have next");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("name is "+name);
System.out.println("role is "+role);
conn.close();
return success;
}