我正在将 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。
我应该如何解决它?