2014-01-19 130 views
2

我在Java數據庫中將Java程序中的byte []存儲起來時遇到問題。從Java中存儲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. 

我做這種事情在甲骨文之前,但我不能在這裏找到問題。有任何想法嗎?

+1

我很感興趣的是,錯誤消息不包括結束')'在最後...不知道它有幫助,但... –

+0

這是奇怪的是啊。我看不到SQL或Java中缺少支架的地方。 –

回答

0

解決方法是,以改變線

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

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

即,用於執行所存儲的程序的正確語法。