2013-05-31 72 views
-1

我正在使用MySQL和JDBC將數據庫用於JSP中的站點。查詢適用於phpMyAdmin,但不適用於Java

我爲搜索命令生成一個SQL查詢,當我在phpMyAdmin上手動運行該查詢並且它返回1行匹配時,查詢起作用。

但執行我的查詢後,ResultSet是空的(我無法獲得表的值)。

這是執行查詢的代碼:

SELECT * FROM產品WHERE個prodName LIKE '%מהיר%' AND存在= 1

public static Product findLikeProd(String ProductName) 
{ 
    Product Product = null; 
    try 
    { 
     ResultSet rs = Database.executeSelect1("SELECT * FROM products WHERE prodName LIKE '%"+ProductName+"%' AND exist=1"); 
     if (rs.next()) 
     { 
      Product = new Product(rs.getInt("PKid"), rs.getString("prodName"), rs.getString("description"), rs.getDouble("price"), rs.getInt("deliveryTime"), rs.getString("imgUrl")); 
     } 
     //Database.closeCon(); 
    } 
    catch (SQLException e) 
    { 
     e.printStackTrace(); 
    } 
    return Product; 
} 

例如查詢

該代碼:

public synchronized static ResultSet executeSelect1(String sqlCmd) { 
    ResultSet rs = null; 
    try { 
     MysqlDataSource ds = new MysqlConnectionPoolDataSource(); 
     ds.setServerName("localhost"); 
     ds.setDatabaseName("tarazJsp"); 
     con=ds.getConnection("root",""); 

     Statement st = con.createStatement(); 
     rs = st.executeQuery(sqlCmd); //The problem is here. rs is received(!=null) but I can't get his parameters.(empty) 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } 

    return rs; 
} 

當我嘗試從rs獲取字符串時,它會引發SQLException。

我該如何解決?

+0

什麼是SQL查詢的重複? –

+0

您已發佈兩個(幾乎)相同/重複的問題。刪除他們不污染SO – informatik01

+0

嘗試[這個答案](http://stackoverflow.com/questions/14738732/mysql-does-not-store-properly-some-utf8-chars/14738790#14738790) – Eran

回答

2

您沒有設置MySQL連接編碼,因此將使用默認編碼。默認編碼可能是latin1,它不包括希伯來字符。這意味着SQL MySQL將實際執行的更像... WHERE prodName LIKE '%????%' ...,並且不會返回任何結果。

一個可能的修復程序設置,不支持希伯來文,像utf8編碼:

ds.setServerName("localhost"); 
    ds.setEncoding("utf8"); 
+0

我以爲關於它,但在phpmyadmin中,我創建了列作爲utf-8,並且當我添加這行ds.setEncoding(「utf8」); ,問題保持原樣,SQL查詢是... WHERE prodName LIKE'%מהיר%'...所以問題我想是其他地方? –

+0

查詢的起源是什麼?你能確定查詢中的希伯來字符沒有以某種方式被破壞嗎?順便說一句,這是很好的列在utf8編碼,但字符編碼用於客戶端連接獨立。 – Joni

相關問題