如何使用Java在MySQL中进行不区分大小写的选择

时间:2014-05-30 10:46:28

标签: java mysql utf-8 prepared-statement

我需要帮助在MySQL数据库中进行不区分大小写的选择 我已经找到了在MySQL中执行此操作的解决方案,但无法将其转换为Java。

这是MySQL:

SELECT * FROM allclient where name="Zdi0" collate utf8_bin

哪个有效,但在java中:

PreparedStatement pstm = mysql.Pstm("SELECT id,name FROM allclient WHERE   user =  ? collate utf8_bin  AND pass = ? ");
pstm.setString(1, this.name);
pstm.setString(2, this.pass);
ResultSet rs = pstm.executeQuery();

生成此异常:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: COLLATION 'utf8_bin' is not valid for CHARACTER SET 'latin1'
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
    at Luncher4.run(Luncher4.java:96)

为什么不起作用?

1 个答案:

答案 0 :(得分:0)

您可以在MySQL中使用不区分大小写的查询作为示例:

SELECT * FROM `users` WHERE UPPER(username) = 'ZDI';
SELECT * FROM `users` WHERE LOWER(username) = 'zdi';