我正面临一个小问题。我从表中获取数据然后 我循环结果集。在每个循环中我使用返回的行数据 通过resultset在另一个表上执行另一个查询。我在发帖 以下代码。
问题是,我在我的代码中感觉到一些问题,但不能 识别它。代码运行正常,没有任何错误或 例外。帮我找到问题。并向我解释一下。
我知道我们可以使用Dao作为第一个结果集。但不是这样 创建太多对象。
public ResultSet search_NatSupport() throws ClassNotFoundException, SQLException {
String query = "SELECT SYMBOL, Date, sup, res FROM Nat_Support WHERE SYMBOL IS NOT ?;";
PreparedStatement stmt = null;
ResultSet rs = null;
DBHelper helper = DBHelper.getInstance();
stmt = helper.getConn().prepareStatement(query);
stmt.setString(1, null);
rs = stmt.executeQuery();
return rs;
}
public void searchAgain(ResultSet rs) {
String query = "select * from bhav_NSE where symbol = ?";
PreparedStatement stmt = null;
try {
while (rs.next()) {
stmt = DBHelper.getInstance().getConn().prepareStatement(query);
stmt.setString(1, rs.getString(1));
ResultSet res = stmt.executeQuery();
while(res.next()){
System.out.println(res.getString(1));
}
res.close();
}
rs.close();
} catch (SQLException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
BhavMaster_NSE finals = new BhavMaster_NSE();
try {
finals.searchAgain(finals.search_NatSupport());
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 0 :(得分:1)
而不是两个结果集,在键上加入表,(这里是符号)并在同一个查询中组合不为空
public void search()
{
PreparedStatement stmt = null;
String query = "select bnse.* from bhav_NSE bnse,Nat_Support nsup where bnse.symbol=nsup.symbol and nsup.symbol is not null";
stmt = DBHelper.getInstance().getConn().prepareStatement(query);
ResultSet res = stmt.executeQuery();
while(res.next()){
// Get data res.getString(..);
}
res.close();
}
rs.close();
}
答案 1 :(得分:0)
一旦我需要使用resultset
的返回结果作为其他人的输入,我得到了这个问题,但我发现我无法做到这一点,经过调试我发现当你写一个Resultset
Resultset
Null
中的public void searchAgain(ResultSet rs) {
try {
while (rs.next()) {
bhavNse(rs.getString(1));
}
rs.close();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public void bhavNse(String var) {
try {
String query = "select * from bhav_NSE where symbol = ?";
PreparedStatement stmt = null;
stmt = DBHelper.getInstance().getConn().prepareStatement(query);
stmt.setString(1, var);
ResultSet res = stmt.executeQuery();
while(res.next()){
System.out.println(res.getString(1));
}
res.close();
}
catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
等于socket
所以这是一个诡计:
#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime
subprocess.call('clear', shell=True)
remoteServer = raw_input("Enter a remote host to scan: ")
remoteServerIP = socket.gethostbyname(remoteServer)
print "-" * 60
print "Please wait, scanning remote host", remoteServerIP
print "-" * 60
t1 = datetime.now()
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((remoteServerIP, 443))
if result == 0:
print "Port {443}: Open"
else:
print "Port {443}: Close"
sock.close()
except KeyboardInterrupt:
print "You pressed Ctrl+C"
sys.exit()
except socket.gaierror:
print 'Hostname could not be resolved. Exiting'
sys.exit()
except socket.error:
print "Couldn't connect to server"
sys.exit()
t2 = datetime.now()
total = t2 - t1
print 'Scanning Completed in: ', total
答案 2 :(得分:0)
;
。Closeable
资源即使您想将Exception
投放到被叫方,也可以使用throw
关键字进行投放。
try(Connection conn = DBHelper.getInstance().getConn()) {
PreparedStatement stmt = conn.prepareStatement(query);
...
ResultSet res = stmt.executeQuery();
...
} catch (SQLException ex) {
// handle exception here or throw it as below :
throw ex;
}
即使出现任何故障, try-with-resource
也会关闭所有资源。
或旧方法是介意关闭资源失败并成功。