mySQL“select”通过JDBC区分大小写,但不通过命令行

时间:2013-01-25 17:49:04

标签: mysql jdbc applet case-sensitive case-insensitive

我正在尝试使用mySQL通过JDBC搜索我的数据库。当我使用这个陈述时:

SELECT * FROM tablename WHERE name='joe';

它执行不区分大小写的搜索并返回任何具有“Joe”或“joe”的行,这就是我想要的。我运行select collation(version()),这返回'utf8_general_ci',显然应该是不区分大小写的。但是,当我在Java applet中使用JDBC运行相同的查询时,它会进行区分大小写的搜索。这是我的query2函数:

try {
        Vector<Vector<String>> out = new Vector<Vector<String>>() ;
        Connection con = connect() ;
        Statement statement = con.createStatement() ;
        System.out.println( "SQL: " + s ) ;
        ResultSet rs = statement.executeQuery(s) ;
        while( rs.next() )
        {
            String r = rs.getString(1) ; // RS is indexed from 1
            Vector<String> q = new Vector<String>() ;
            for( int i = 2 ; i <= tableSize ; i ++ )
            {
                q.add(r) ;
                r = rs.getString(i) ;
            }
            q.add(r) ;
            out.add(q) ;
        }
        con.close() ;
        return( out ) ;
    } catch (SQLException e) {
        System.err.println( "SQL EXCEPTION" ) ;
        System.err.println( s ) ;
        e.printStackTrace();
    }

这是我的select函数调用query2

public static Vector<Vector<String>> select( String table , List<String> columns , List<String> values )
{
    String statement = "SELECT * from `" + table + "` WHERE " ;
    for( int i = 0 ; i < columns.size(); i ++ )
    {
        statement += columns.get(i) + "='" + values.get(i) + "'" ;
        if( i + 1 < columns.size() )
            statement += " AND " ;
    }
    statement += " ;" ;
    return query2 ( statement , tableSize(table) ) ;
}

知道如何让这个查询不区分大小写吗?

2 个答案:

答案 0 :(得分:0)

这行不需要阅读

statement += "UPPER("+columns.get(i) + ")='UPPER(" + values.get(i) + ")'"

垮台......不能使用索引,因为它正在上升所有东西。

答案 1 :(得分:0)

事实证明,我认为使用select函数的搜索实际上使用了不同的函数。此功能从数据库中选择所有内容并通过客户端进行排序。我测试了select函数,它实际上不区分大小写,不必使用给出的任何提示(但感谢您的建议!)。

相关问题