0

here is my code

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();

And the error is

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
4

1 回答 1

2

Your problem is that key is a reserved word. You should escape it using backticks (`). On most keyboards, that's the same key that has the tilde (~).

ps = con.prepareStatement("insert into `key` values (?,?)");
于 2013-08-22T12:10:36.110 回答