public static void main(String[] args) throws SQLException {
getconn();
String selectsql="select * from info";
(所有查询代码为:
select * from info where tel like '?%';
select * from info where sex=?;
select * from info where tel like '?%';
select * from info where sex=?;
)
Object[] obj = {};
select(selectsql,obj);
demonext();
democlose();
}
private static void democlose() throws SQLException {
resultSet.close();
pstat.close();
conn.close();
}
private static void demonext() throws SQLException {
while (resultSet.next()){
System.out.println("id="+resultSet.getString("id")
+" name="+resultSet.getString("name")
+" tel="+resultSet.getString("tel")
+" sex="+resultSet.getString("sex"));
}
}
private static void select(String selectsql,Object[] obj) throws SQLException {
pstat = conn.prepareCall(selectsql);
for (int i = 0; i < obj.length; i++) {
pstat.setObject(i+1,obj[i]);
}
resultSet =pstat.executeQuery();
}
该博客主要展示了如何使用Java进行SQL查询,包括预编译语句设置参数和获取查询结果。代码中包含了多条查询语句,涉及条件过滤,如电话号码模糊匹配和性别精确匹配。并提供了关闭资源的方法,确保了良好的数据库操作实践。
836

被折叠的 条评论
为什么被折叠?



