0

I want to store the public key into my database.I encoded the publickey and used setBytes() to insert to the database but I received the following error.

Error

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)

This is the details of my Table(key)

enter image description here enter image description here

This is my implementation of insertion of the public key to my database

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());
    }
}
user3820292
  • 310
  • 3
  • 19

1 Answers1

0

Key is reserve word in mysql

use a different tablename or use backticks in tablename

 conn.prepareStatement("INSERT INTO `Key` (publickey) VALUES (?)");
singhakash
  • 7,891
  • 6
  • 31
  • 65