与MySQL连接时,结果集返回空

时间:2017-09-06 09:23:38

标签: java mysql jdbc prepared-statement resultset

我正在尝试使用PreparedStatementResultSet在MySQL中的数据库中执行查询。

问题是我在MySQL中得到一个空结果集,否则我在Derby上得到了正确的结果。

Connection con= null;
ResultSet reslt =null;
PreparedStatement ps = null;
try 
{
    //Class.forName("org.apache.derby.jdbc.ClientDriver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/SAIID","SAIID","SAIID"); 
    //con = DriverManager.getConnection("jdbc:derby://localhost:1527/pavillons","saiid","saiid"); 
    String Query =" SELECT * FROM ETUDIANT_PAV WHERE PAVILLONS = ? AND CHAMBRE = ? "

    ps = con.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ps.setString(1, "A");
    ps.setString(2, "1");
    reslt = ps.executeQuery();
    //String thequeryresult= reslt.getString("NOM_PRENOM");
    //System.out.println ("this is the query result"+thequeryresult);
    JOptionPane.showMessageDialog(null, "Query Executed");
    //con.close();


}
catch (Exception ex) 
{
    JOptionPane.showMessageDialog(null, ex.getMessage());

}

2 个答案:

答案 0 :(得分:3)

看到这一行:

reslt = ps.executeQuery(); 
while(reslt.next()){ //read all lines
    System.out.println(reslt.getString("NOM_PRENOM"));
}

如果您使用该注释行检查结果,则无法使用。您需要将该结果集的光标移动到第一行(从-1开始)。

为此,使用ResultSet.next()将返回before_save,直到没有更多行要阅读。

def Booking < ActiveRecord::Base
    before_save :init
    PRICE_INCREMENT = [one, two, three, four]

    def init
        self.duration ||= (end_date - start_date).to_i
        self.price    ||= duration * PRICE_INCEREMENT[duration]
    end
 end

答案 1 :(得分:-1)

def checkProxies(self):
    loop = asyncio.get_event_loop()
    proxies = self.proxies[:10]
    tasks = [self.checkProxy(proxy) for proxy in proxies]


    loop.run_until_complete(
        asyncio.gather(
            *tasks
        )
    )

    loop.close()

uk = GetUkProxy()
uk.checkProxies()
print(uk.proxies)