插入字符串>ÄX%ß=dÜgž=ŒT8ï8L]4C®°进入sql显示错误

时间:2013-08-22 12:01:41

标签: java mysql

这是我的代码

key = KeyGenerator.getInstance(algorithm).generateKey();
byte[] keyBytes = key.getEncoded();
String k = new String(keyBytes);

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement st = con.createStatement();  
rs = st.executeQuery("select tid from tid");  
while(rs.next())  
{  
    mid = Integer.parseInt(rs.getString(1));
    b = Integer.toString(mid);
    ++mid;
    a = Integer.toString(mid);    
} 
out.println(k);

ps = con.prepareStatement("insert into key values (?,?)");
ps.setString(1,a);
ps.setString(2,k);
ps.executeUpdate();

错误是

javax.servlet.ServletException: 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 'key values ('8','›ÄX%ß=dÜgž=ŒT8ï8L]4C®°')' at line 1

1 个答案:

答案 0 :(得分:2)

您的问题是key是保留字。你应该使用反引号(`)来逃避它。在大多数键盘上,这是具有波浪号(〜)的相同键。

ps = con.prepareStatement("insert into `key` values (?,?)");