Java 靠近'的Mysql语法错误;第1行的键(公钥)值(_binary';0?Ÿ;0\r*†;H†;÷;\r\0???\00?‰;\0';)
我想将公钥存储到数据库中。我对公钥进行了编码,并使用setBytes()插入到数据库中,但收到以下错误 错误Java 靠近'的Mysql语法错误;第1行的键(公钥)值(_binary';0?Ÿ;0\r*†;H†;÷;\r\0???\00?‰;\0';),java,mysql,Java,Mysql,我想将公钥存储到数据库中。我对公钥进行了编码,并使用setBytes()插入到数据库中,但收到以下错误 错误 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 'K
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 (publickey) VALUES (_binary'0?Ÿ0\r *†H†÷\r\0??\00?‰??\0' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
at com.mysql.jdbc.Util.getInstance(Util.java:372)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2535)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1911)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2145)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2081)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2066)
at Test.insertPublicKey(Test.java:24)
at Test.main(Test.java:48)
这是我的表(键)的详细信息
这是我将公钥插入数据库的实现
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.sql.Connection;
import java.sql.DriverManager;
import com.mysql.jdbc.PreparedStatement;
public class Test {
static Connection conn;
static PreparedStatement stmt;
public static void insertPublicKey(byte[] publickey) {
try {
//connect to database
conn = DriverManager.getConnection("jdbc:mysql://host:3306/name","username","password");
stmt = (PreparedStatement) conn.prepareStatement("INSERT INTO Key (publickey) VALUES (?)");
stmt.setBytes(1,publickey);
stmt.executeUpdate();
stmt.close();
conn.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
KeyPair pair = null;
PublicKey publicKey = null;
KeyPairGenerator keyGen = null;
try {
keyGen = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
pair = keyGen.generateKeyPair();
publicKey = pair.getPublic();
insertPublicKey(publicKey.getEncoded());
}
}
关键字是mysql中的word
使用其他tablename或在tablename中使用反勾号
conn.prepareStatement("INSERT INTO `Key` (publickey) VALUES (?)");