当语句没有返回结果集时,我正在尝试处理SQLServerException。我还是java新手,所以我无法找到解决问题的方法。请问任何人都可以建议我如何解决错误?我遇到困难的地方是这个存储过程没有返回任何结果集,我想显示“找不到记录”之类的东西。我该如何解决?
stmt = conn.prepareCall("{call p_GetAllowedPublicIPs(?,?,?)}");
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
PrintWriter out = response.getWriter();
String IsLoginDisabled = null;
String BankID =null;
String publicip=null;
try {
//processRequest(request, response);
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = null;
CallableStatement myStmt = null;
int count =0;
conn = DriverManager.getConnection("jdbc:sqlserver://MPESA\\SQL2012;user=realm;password=friend;database=ElmaTest");
if(conn!=null)
{
out.println("Connection Succcesful");
myStmt = conn.prepareCall("{call sp_GetPortalUser(?,?,?)}");
myStmt.setString("Country", "Kenya");
myStmt.setString("BankName", "CS");
myStmt.setString("UserID", "Frank");
ResultSet rs= myStmt.executeQuery();
while(rs.next())
{
count++;
BankID = rs.getString("BankID");
String employeeid= rs.getString("EmployeeID");
String FirstName = rs.getString("FirstName");
String LastName= rs.getString("LastName");
String MiddleName = rs.getString("MiddleName");
String Address = rs.getString("Address");
String MobileNumber= rs.getString("MobileNumber");
String Emailid = rs.getString("EmailID");
String TypeofID= rs.getString("TypeOfID");
String IDNumber = rs.getString("IDNumber");
String ipaddress = rs.getString("IPAddress");
IsLoginDisabled = rs.getString("isLoginDisabled");
String LoginFailureIPaddress = rs.getString("LoginFailureIPAddress");
System.out.println("count"+count);
System.out.println("BankID" +BankID);
System.out.println("EmployeeId"+employeeid);
System.out.println("FirstName"+FirstName);
System.out.println("MiddleName"+MiddleName);
System.out.println("LastName"+LastName);
System.out.println("Address"+Address);
System.out.println("MobileNumber"+MobileNumber);
System.out.println("EmailId"+Emailid);
System.out.println("TypeoFiD"+TypeofID);
System.out.println("Idnumber"+IDNumber);
System.out.println("ipaddress"+ipaddress);
System.out.println("isLoginDisabled"+IsLoginDisabled);
System.out.println("LoginFailureIPaddress"+LoginFailureIPaddress);
}
if(count>0)
{
int logindisabled = Integer.valueOf(IsLoginDisabled);
CallableStatement stmt = null;
if (logindisabled!=1)
{
try {
stmt = conn.prepareCall("{call p_GetAllowedPublicIPs(?,?,?)}");
} catch (SQLException ex) {
Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, null, ex);
}
stmt.setString("Country", "Kenya");
stmt.setString("BankID", "99");
stmt.setString("PublicIP", "1");
ResultSet rp = stmt.executeQuery();
// System.out.println(rp.next());
while(rp.next())
{
String ipaddress = rp.getString("IPAddress");
System.out.println("ipaddress"+ipaddress);
}
}
}
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:5)
你的程序没有引发任何异常的事实是没有结果也不例外。
您可以使用CallableStatement的execute()方法。 execute方法将返回一个布尔值,指示是否存在与执行请求相对应的ResultSet。
链接到Javadoc:https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#execute()
您仍然可以使用超类Statement的getResultSet方法获取ResultSet:https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getResultSet()
在您的情况下,您将执行以下操作:
boolean gotResults = myStmt.execute();
ResultSet rs = null;
if(!gotResults){
System.out.println("No results returned");
} else {
rs = myStmt.getResultSet();
}
答案 1 :(得分:3)
我遇到了同样的问题,经过一些研究后我知道我们必须包括
CREATE PROCEDURE {proc_name} (parameters)
AS
BEGIN
SET NOCOUNT ON
...........
----your sqloperations
END
开始你的程序后,你必须包含上面的sql语句,然后问题就会得到解决。