我试图在附加的Java中返回计数值,这就是我所拥有的......
package com.example.tests;
import java.awt.geom.GeneralPath;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestNew {
public void generateid(){
int count;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://IP:PORT;DatabaseName=blabla;integratedSecurity=true;");
System.out.println("Connection Successfull");
System.out.println(conn);
//--------------------------------------------------------------------
Statement stmt = conn.createStatement();
String query1= "SELECT COUNT(*) from dbo.vSubscriberReporting where [SubscriberEmailAddressStatusID] in (5,1,6,7) and [SubscriberStatusID] = 1 AND [SubscriberAddresseeStatusID] = 1 and CompanySizeCodeID IN (4)";
ResultSet rs= stmt.executeQuery(query1);
count = rs.getInt(1);
//i cant store the count value from query in this variable... Please help me.
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
TestNew gen = new TestNew();
gen.generateid();
}
}
查询运行,但返回
com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
虽然我知道应该有数据,只有一个COUNT记录..
我希望能够将结果存储到变量中,然后使用它来验证selenium脚本中的字段吗?
答案 0 :(得分:3)
在尝试拨打rs.next()
getInt(1)
if(rs.next()) //Expecting one row.
{
count = rs.getInt(1);
}
答案 1 :(得分:0)
因为这是一个结果;你也可以这样做:
rs.moveFirst();
count = rs.getInt(1);