我正在使用MySQL和JDBC将数据库用于JSP中的站点。
我为搜索命令生成一个SQL查询,当我在phpMyAdmin上手动运行它并且返回1行匹配时,该查询有效。
但是在执行我的查询后, ResultSet 为空(我无法获取表的值)。
这是执行查询的代码:
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;
}
查询例如:
SELECT * FROM Products WHERE prodName LIKE'%מהיר%'AND exists = 1
代码:
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 :(得分:2)
您没有设置MySQL连接编码,因此将使用默认编码。默认编码可能是latin1
,不包括希伯来字符。这意味着SQL MySQL实际执行将更像... WHERE prodName LIKE '%????%' ...
,并且不会返回任何结果。
可能的解决方法是设置支持希伯来语的编码,例如utf8
:
ds.setServerName("localhost");
ds.setEncoding("utf8");