簡體   English   中英

為什么在向查詢中添加Where子句時沒有得到結果?

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

我正在使用Mysql JDBC驅動程序..但是我有一些問題..

問題是當我在Java源代碼中使用SELECT Query時。

當我使用此查詢時:

select * from [name of table]

我已經成功地從數據庫獲得了查詢結果。

但是當我使用此查詢時:

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

我沒有從數據庫查詢的結果。

區別在於,僅where使用。

有什么問題?

我怎么解決這個問題?

這是我的代碼在下面

此查詢不起作用

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

這個查詢工作得很好

select * from student;

區別在於只是查詢信息..其余的源代碼是絕對相同的


我添加了Java源代碼公共類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();
        }

    }


}

}

您將需要在JDBC連接URL中設置字符編碼:

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

否則,客戶端和服務器之間的字符編碼將在連接時自動檢測到,這意味着查詢中的韓文文本將被錯誤編碼,並可能導致查詢返回零結果。
您可以在MySQL JDBC驅動程序文檔中閱讀有關此內容的更多信息- 使用字符集和Unicode

這個答案表明Java代碼沒有問題。 這是您的數據。

您可以在MySQL管理工具中運行查詢並獲取不為空的結果集嗎? 如果是,則您的Java代碼存在問題。 您可能沒有提供一個錯誤,或者吞下了一個空的捕獲塊。

您收到錯誤消息或堆棧跟蹤,還是結果集為空? 如果是后者,則可能沒有編號為20001001的學生行。

MySQL函數“ substring”參數0錯誤,僅允許大於零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM