jdbc 连接数据库查询 在 ResultSet 迭代的时候 还可以连接数据库查询吗?
PreparedStatementstmt=con.prepareStatement(select*fromtableName1);ResultSetrs=stmt.ex...
PreparedStatement stmt = con.prepareStatement(select * from tableName1);
ResultSet rs = stmt.executeQuery();
while (rs.next()) { //这里已经是在连接数据库查询
PreparedStatement stmt = con.prepareStatement(select * from tableName2 where tableName2_id = rs.getInt(1)); //再连接数据查询一张表
}
这样做可以吗,求帮忙,求指点! 展开
ResultSet rs = stmt.executeQuery();
while (rs.next()) { //这里已经是在连接数据库查询
PreparedStatement stmt = con.prepareStatement(select * from tableName2 where tableName2_id = rs.getInt(1)); //再连接数据查询一张表
}
这样做可以吗,求帮忙,求指点! 展开
2个回答
展开全部
可以
PreparedStatement stmt = con.prepareStatement(select * from tableName1);
ResultSet rs = stmt.executeQuery();
while (rs.next()) { //这里已经是在连接数据库查询
PreparedStatement stmt2 = con.prepareStatement(select * from tableName2 where tableName2_id = rs.getInt(1)); //再连接数据查询一张表
ResyktSer rs2 =stmt2.executeQuery();
while( rs2.next()){
}
rs2.close();
stmt2.close();
}
当然,最好再写个方法 ,rs.getInt(1)作为参数传
PreparedStatement stmt = con.prepareStatement(select * from tableName1);
ResultSet rs = stmt.executeQuery();
while (rs.next()) { //这里已经是在连接数据库查询
test( rs.getInt(1)));
....
. ...
}
固然,最好的办法是写成一条语句,不知道你到底什么需求,看这样子肯定一句话解决啊
select t2.* from tableName2 t2 left join tableName1 t1 on t1.XXX =t2.tableName2_id
XXX填你关联的字段, 看实际情况啊。肯定有好办法。那样写太差劲了
PreparedStatement stmt = con.prepareStatement(select * from tableName1);
ResultSet rs = stmt.executeQuery();
while (rs.next()) { //这里已经是在连接数据库查询
PreparedStatement stmt2 = con.prepareStatement(select * from tableName2 where tableName2_id = rs.getInt(1)); //再连接数据查询一张表
ResyktSer rs2 =stmt2.executeQuery();
while( rs2.next()){
}
rs2.close();
stmt2.close();
}
当然,最好再写个方法 ,rs.getInt(1)作为参数传
PreparedStatement stmt = con.prepareStatement(select * from tableName1);
ResultSet rs = stmt.executeQuery();
while (rs.next()) { //这里已经是在连接数据库查询
test( rs.getInt(1)));
....
. ...
}
固然,最好的办法是写成一条语句,不知道你到底什么需求,看这样子肯定一句话解决啊
select t2.* from tableName2 t2 left join tableName1 t1 on t1.XXX =t2.tableName2_id
XXX填你关联的字段, 看实际情况啊。肯定有好办法。那样写太差劲了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询