本文介绍了如何为字符数组传递PreparedStatement setArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JDBC代码,如下所示:

I have some JDBC code as follows:

String selectSQL = "SELECT * FROM DBUSER WHERE USER_ID = ? and PASSWORD = ?";

Integer userId = 1000;
char[] passwordString = new char[] { 't', 'e', 's', 't' };

PreparedStatement preparedStatement = dbConnection.prepareStatement(selectSQL);
preparedStatement.setInt(1, 1001);
preparedStatement.setArray(2,... ??? // how to do this part?

// execute select SQL statement
ResultSet rs = preparedStatement.executeQuery();

如何调用preparedStatement.setArray设置查询中的第二个参数?我不想在这里使用字符串参数来保护密码.

How do I call preparedStatement.setArray to set the second parameter in the query? I don't want to use a string parameter here to protect the password.

请注意,我正在使用Hypersonic DB,但如果有用的话,请计划移至MySql.

Note I am using Hypersonic DB but plan to move to MySql if this is useful.

推荐答案

收到了首先,您应该使用JDBC Conncetion的方法创建数组,然后才能将其传递给setArray.

PreparedStatement#setArray received a java.sql.ArrayFirst you should use the JDBC Conncetion's createArrayOf method to create the array, only then you can pass it to setArray.

由于该方法仅接受Object[],因此您应该创建Character的数组而不是char.

As the method only accept Object[] you should create an array of Character instead of char.

例如:

Character[] passwordString = new Character[] { 't', 'e', 's', 't' };
Array sqlArray = con.createArrayOf("CHAR", passwordString);
preparedStatement.setArray(2, sqlArray);

这篇关于如何为字符数组传递PreparedStatement setArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 23:26