如何检查ResultSet是否为空?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%><%@page import = "java.sql.ResultSet" %>
<%@page import = "java.sql.Connection" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Getting Database Connection</title>
</head>
<body>
<%
ResultSet result = null ;
Connection connection1 = null;
try
{
String driverName1 = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName1);
String serverName = "localhost";
String portNumber = "1521";
String sid = "XE";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "SYSTEM";
String password = "password";
connection1 = DriverManager.getConnection(url, username, password);
out.println(connection1.isClosed());
String Name = request.getParameter("name");
String Age = request.getParameter("age");
int AgeConvert = Integer.parseInt(Age);
String ADR = request.getParameter("address");
String sqlInsert = " INSERT INTO ADDRESS (NAME,AGE,ADR) VALUES ('" + Name + "' , " + AgeConvert + ", '" + ADR +"')";
Statement stm = connection1.createStatement();
stm.execute(sqlInsert);
out.println(" User Created");
}
catch(Exception e )
{
out.println("User not created");
e.printStackTrace();
}
%>
<table>
<tr>
<td> <b>To search the User Please enter the name in the below box </b> </td>
</tr>
<tr>
<td> Name </td>
<td> <input type = "text" name ="userName" /> </td>
</tr>
<tr>
<td> <input type ="submit" value = "Search" /></td>
</tr>
</table>
<% ResultSet results = null ;
String userNameToBeSearched = request.getParameter("userName");
String sqlselect = " SELECT * FROM ADDRESS WHERE NAME ='"+userNameToBeSearched+"'";
Statement stm2 = connection1.createStatement();
results = stm2.execute(sqlselect);
**// getting error in this loop it says can convert boolean to ResultSet please help me**
if (results.next()) {
do {
%>
<TABLE BORDER="1">
<TR>
<TH>Name</TH>
<TH>Age</TH>
<TH>Address</TH>
</TR>
<TR>
<TD> <%= results.getString(1) %> </TD>
<TD> <%= results.getInt(2) %> </TD>
<TD> <%= results.getString(3) %> </TD>
</TR>
</TABLE>
<%
} while (results.next());
} else
{
out.println("User not found");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%><%@page import = "java.sql.ResultSet" %>
<%@page import = "java.sql.Connection" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Getting Database Connection</title>
</head>
<body>
<%
ResultSet result = null ;
Connection connection1 = null;
try
{
String driverName1 = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName1);
String serverName = "localhost";
String portNumber = "1521";
String sid = "XE";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "SYSTEM";
String password = "password";
connection1 = DriverManager.getConnection(url, username, password);
out.println(connection1.isClosed());
String Name = request.getParameter("name");
String Age = request.getParameter("age");
int AgeConvert = Integer.parseInt(Age);
String ADR = request.getParameter("address");
String sqlInsert = " INSERT INTO ADDRESS (NAME,AGE,ADR) VALUES ('" + Name + "' , " + AgeConvert + ", '" + ADR +"')";
Statement stm = connection1.createStatement();
stm.execute(sqlInsert);
out.println(" User Created");
}
catch(Exception e )
{
out.println("User not created");
e.printStackTrace();
}
%>
<table>
<tr>
<td> <b>To search the User Please enter the name in the below box </b> </td>
</tr>
<tr>
<td> Name </td>
<td> <input type = "text" name ="userName" /> </td>
</tr>
<tr>
<td> <input type ="submit" value = "Search" /></td>
</tr>
</table>
<% ResultSet results = null ;
String userNameToBeSearched = request.getParameter("userName");
String sqlselect = " SELECT * FROM ADDRESS WHERE NAME ='"+userNameToBeSearched+"'";
Statement stm2 = connection1.createStatement();
results = stm2.execute(sqlselect);
**// getting error in this loop it says can convert boolean to ResultSet please help me**
if (results.next()) {
do {
%>
<TABLE BORDER="1">
<TR>
<TH>Name</TH>
<TH>Age</TH>
<TH>Address</TH>
</TR>
<TR>
<TD> <%= results.getString(1) %> </TD>
<TD> <%= results.getInt(2) %> </TD>
<TD> <%= results.getString(3) %> </TD>
</TR>
</TABLE>
<%
} while (results.next());
} else
{
out.println("User not found");
}
%>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果光标位于第一行之前,isBeforeFirst() 返回 true;如果光标位于任何其他位置或结果集不包含行,则为 false
isBeforeFirst() returns true if the cursor is before the first row; false if the cursor is at any other position or the result set contains no rows
尝试 stm2.exexuteQuery()
这将获取结果集
try stm2.exexuteQuery()
This will grab the result set
如果没有找到更多行,
ResultSet.next()
返回false
,否则返回true
。ResultSet.next()
returnsfalse
if no more rows are found, otherwisetrue
.这样做不行吗:
.next() 将遍历行并验证是否有行。
Would it not work doing:
.next() will go through the rows and validate if there is any row.