使用的oracle 10g数据库
直接在orcal中使用
在使用数据库操作是的语句是可以的
select * from agent where ADRESS LIKE '%?%' 'q';
Object[] s = {'q'};
rs = db.executeQuery("select * from agent where ADRESS LIKE '%?%' ",s );
出现问题:
SQL语句有问题!
java.sql.SQLException: 无效的列类型
sqlhleper中其他操作都是正常的
public ResultSet executeQuery(String sql, Object[] params)
{
getConnection(); //创建连接
try
{
pst = con.prepareStatement(sql);
// 传递参数
if (params != null)
{
if (params.length > 0)
{
for (int i = 0; i < params.length; i++)
{
pst.setObject(i + 1, params[i]);
}
}
}
// 执行
rs = pst.executeQuery();
return rs;
} catch (SQLException e)
{
System.out.println("SQL语句有问题!");
e.printStackTrace();
return null;
}
}JDBCSQLJava数据库
直接在orcal中使用
在使用数据库操作是的语句是可以的
select * from agent where ADRESS LIKE '%?%' 'q';
Object[] s = {'q'};
rs = db.executeQuery("select * from agent where ADRESS LIKE '%?%' ",s );
出现问题:
SQL语句有问题!
java.sql.SQLException: 无效的列类型
sqlhleper中其他操作都是正常的
public ResultSet executeQuery(String sql, Object[] params)
{
getConnection(); //创建连接
try
{
pst = con.prepareStatement(sql);
// 传递参数
if (params != null)
{
if (params.length > 0)
{
for (int i = 0; i < params.length; i++)
{
pst.setObject(i + 1, params[i]);
}
}
}
// 执行
rs = pst.executeQuery();
return rs;
} catch (SQLException e)
{
System.out.println("SQL语句有问题!");
e.printStackTrace();
return null;
}
}JDBCSQLJava数据库
被单引号框定的,无法识别为参数。改为:
rs = db.executeQuery("select * from agent where ADRESS LIKE ?", s);字符串s注意要自带 %题外话:按这个搜索条件,数据库只能可怜兮兮的全表扫描
rs = db.executeQuery("select * from agent where ADRESS LIKE ? ",s );
select * from agent where ADRESS LIKE '%'q'%'
你试试直接运行上面这句看是不是也是这样提示
因为使用这种传参的方式,字符串外面都会自动加上单引号你看select * from agent where ADDRESS=?
传入字符串'abc'
它也会处理成select * from agent where ADDRESS='abc'
而不是select * from agent where ADDRESS=abc
这是同一个道理
不是已经给了建议么?不要把参数写在“单引号”里面,系统会认为这就是个字符串,你并没有设置任何参数,当然后面也就不可能去给不存在的参数设置值,所以 pst.setObject(i + 1, params[i]); 无效列索引。
pst = con.prepareStatement("select * from agent where ADRESS LIKE ? ");
pst..setObject(1, "%" + s + "%");