从Java存储MySQL中的字节数组

时间:2014-01-19 13:46:58

标签: java mysql sql

我在MySQL数据库的Java程序中存储byte []时遇到了麻烦。

我有一个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),salt(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中做过这种事情,但我在这里找不到问题。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

修复方法是更改​​行

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

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

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