简体   繁体   中英

Why I didn't get results when adding Where clause to my query?

I'm using Mysql JDBC driver.. but I have some problem..

the problem is when I use SELECT Query in java source code.

When I use this query:

select * from [name of table]

I've gotten result of query from DB successfully.

but when I use this query:

 select * from student where (substring(stu_name,0,1)>= '가' and substring(stu_name,0,1) < '나') ;

I've not gotten result of query from DB..

The difference thing is just that using where or not.

What's the problem?

How can I solve this problem?

this is my code below

this query isn't working

select * from student where (substring(stu_name,0,1)>= '가' and substring(stu_name,0,1) < '나') ;

this query is working very well

select * from student;

The difference thing is just only query information .. rest of the source code is the same absolutely


I added my java source code public class Center {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String sql =  "select * from student where (substring(stu_name,0,1)>= '가' and substring(stu_name,0,1) < '나') ";
    String url = "jdbc:mysql://localhost:3306/test";
    String driverName = "com.mysql.jdbc.Driver";
    String id = "root";
    String password = "jsyun0415";
    Connection con = null;
    PreparedStatement prepareState = null;
    Statement stmt = null;
    ResultSet rs = null;
    ArrayList<String> list = new ArrayList<String>();
    ArrayList<String> list_second = new ArrayList<String>();

    try {
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {
        System.out.println("Failed to load JDBC driver..");
        e.printStackTrace();
        return;
    }

    try {
        con = DriverManager.getConnection(url, id, password);
        stmt = con.createStatement();
        rs = stmt.executeQuery(sql);


            while (rs.next()) {
                list.add(rs.getNString("stu_no"));
                list_second.add(rs.getNString("stu_ename"));
            }

        //test = list.get(2);
    } catch (SQLException sqex) {
        System.out.println("SQLException: " + sqex.getMessage());
        System.out.println("SQLState: " + sqex.getSQLState());
    } finally {
        try {
            if (stmt != null)
                stmt.close();
            if (con != null)
                con.close();
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


}

}

You will need to set the the character encoding in the JDBC connection URL:

String url = "jdbc:mysql://localhost:3306/test?useUnicode=yes&characterEncoding=UTF-8";

Otherwise the character encoding between client and server is automatically detected upon connection, this means that the Korean text in your query will be wrongly encoded and will probably cause the query to return zero results.
You can read more about this in the MySQL JDBC driver documentation - Using Character Sets and Unicode

This answer suggests that there's no problem with the Java code; it's your data.

Can you run the query in the MySQL admin tool and get back a result set that's not empty? If yes, there's a problem with your Java code. There must be an error that you either aren't supplying or you swallow with an empty catch block.

Do you get an error message or stack trace, or is the result set empty? If it's the latter, perhaps there is no student row with number 20001001.

MySQL函数“ substring”参数0错误,仅允许大于零。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM