java - 从 Java 将字节数组存储在 MySQL 中

标签 java mysql sql

我在将 Java 程序的 byte[] 存储到 MySQL 数据库时遇到问题。

我有一个 Java 方法:

public void newUser(User user) {
    Connection conn = pool.checkOut();

    try {
        CallableStatement stmt = conn.prepareCall("NewUser(?,?,?)");
        //name salt password
        stmt.setString(1, user.getName());
        stmt.setBytes(2, user.getSalt());
        stmt.setBytes(3, user.getPass());
        stmt.executeUpdate();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        pool.checkIn(conn);
    }
}

它调用了一个我测试过并且有效的 MySQL 存储过程:

DELIMITER $$

CREATE DEFINER=`[UserNameGoesHere]`@`%` PROCEDURE `NewUser`(in username varchar(50), in salt blob, in pass blob)
BEGIN

INSERT INTO Schema.Users (Username, Salt, PasswordSecure)
VALUES (username, salt, pass)
;

END

该表包含列名称 (varchar)、盐 (blob) 和密码 (blob)。

当我将它作为 JUnit 测试运行时,我得到以下输出:

13:42:22.405 [main] DEBUG c.B.B.model.MySQLConnectionPool - Making a new connection pool 572247323.
13:42:23.695 [ForkJoinPool-1-worker-1] DEBUG c.B.B.model.MySQLConnectionPool - Getting a connection from the pool.
13:42:23.696 [ForkJoinPool-1-worker-1] DEBUG c.B.B.model.MySQLConnectionPool - Pool size is now 0.
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 'NewUser('NewUserTest',_binary'!œ&æÏ£–f',_binary'Ã=ÉMcOü¿zQp4)„g|Ã' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2459)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2376)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2360)
    at com.mysql.jdbc.CallableStatement.executeUpdate(CallableStatement.java:984)
    at com.BGB.BigIssue.model.MySQLDatabase.newUser(MySQLDatabase.java:81)
    at com.BGB.BigIssue.model.MySQLDatabaseTest.testNewUserCreatesNewUser(MySQLDatabaseTest.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
13:42:23.778 [ForkJoinPool-1-worker-1] DEBUG c.B.B.model.MySQLConnectionPool - Checking in connection.
13:42:23.778 [ForkJoinPool-1-worker-1] DEBUG c.B.B.model.MySQLConnectionPool - Pool size is now 1.
13:42:23.778 [main] INFO  c.B.B.controller.LoginController - Username GuyTest was not found.
13:42:23.785 [Thread-0] INFO  c.B.B.model.MySQLConnectionPool - Connection pool 2123610602 closing.

我以前在Oracle中做过这种事情,但我找不到问题所在。有什么想法吗?

最佳答案

解决办法是换行

CallableStatement stmt = conn.prepareCall("NewUser(?,?,?)");

CallableStatement stmt = conn.prepareCall("CALL NewUser(?,?,?)");

这是执行存储过程的正确语法。

关于java - 从 Java 将字节数组存储在 MySQL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21217546/

相关文章:

java - 在 ElasticSearch 上使用 Lucene 进行搜索

java - 如何比较Java中的字符串?

mysql - 出现两个或多个指定值的行数

来自多个列的 MySQL COUNT 次出现

php - 我的数据库没有插入一些值

java - 使用 Scanner 一次读取一个文件夹中的所有文件会给出 NoSuchElementException

Java GUI,需要使用 Action 监听器吗?

mysql - 如何使用 MongoDB 聚合对唯一 ID 进行求和?

SQL:如何将 ID 附加到具有重复值的行

SQL FORMAT 函数错误