麻烦高手们帮忙看一下,为什么这个程序只能输出查询到的条数,但是输出不了查询结果啊?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
try{
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="javaee";//数据库用户名
String userPasswd="123456";//密码
String dbName="drug";//数据库名
String tableName="clean_drug_like12";//表名
String url="jdbc:mysql://202.201.14.241/"+dbName+"?user="+userName+"&password="+userPasswd;//连结字符串
Connection connection;
Statement statement;
int RowCount;
Class.forName(driverName).newInstance();
connection=DriverManager.getConnection(url);
statement = connection.createStatement();
ResultSet rs=null;
String sql="select count(*) from clean_drug_like12";
String sql1="select * from clean_drug_like12 limit 1,10";
rs=statement.executeQuery(sql);
rs.next();
RowCount=rs.getInt(1);
out.println(RowCount);
rs=statement.executeQuery(sql1);
out.println(rs.getInt("ID"));
out.println(rs.getDouble("XLogP"));
}catch(Exception e){
System.out.println("Exception:"+e.getMessage());
}
%>
</body>
</html>
非常感谢!
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
try{
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="javaee";//数据库用户名
String userPasswd="123456";//密码
String dbName="drug";//数据库名
String tableName="clean_drug_like12";//表名
String url="jdbc:mysql://202.201.14.241/"+dbName+"?user="+userName+"&password="+userPasswd;//连结字符串
Connection connection;
Statement statement;
int RowCount;
Class.forName(driverName).newInstance();
connection=DriverManager.getConnection(url);
statement = connection.createStatement();
ResultSet rs=null;
String sql="select count(*) from clean_drug_like12";
String sql1="select * from clean_drug_like12 limit 1,10";
rs=statement.executeQuery(sql);
rs.next();
RowCount=rs.getInt(1);
out.println(RowCount);
rs=statement.executeQuery(sql1);
out.println(rs.getInt("ID"));
out.println(rs.getDouble("XLogP"));
}catch(Exception e){
System.out.println("Exception:"+e.getMessage());
}
%>
</body>
</html>
非常感谢!
RowCount=rs.getInt(1);
out.println(RowCount);
rs=statement.executeQuery(sql1);
while(rs.next()){//使用循环打印出来,上面你懂得用rs.next(),为什么这里没有用呢?
out.println(rs.getInt("ID"));
out.println(rs.getDouble("XLogP"));
}
在rs=statement.executeQuery(sql1);
加上rs.next();
看看这里
while(rs.next()){
out.println(rs.getInt("ID"));
}
out.println(rs.getInt("ID"));
out.println(rs.getDouble("XLogP"));
一般返回多个结果时都用while(rs.next()),即判断又往前移。
<body>
<%
try{
String driverName="com.mysql.jdbc.Driver";//驱动程序名
String userName="javaee";//数据库用户名
String userPasswd="123456";//密码
String dbName="drug";//数据库名
String tableName="clean_drug_like12";//表名
String url="jdbc:mysql://202.201.14.241/"+dbName+"?user="+userName+"&password="+userPasswd;//连结字符串
Connection connection;
Statement statement;
int RowCount;
Class.forName(driverName).newInstance();
connection=DriverManager.getConnection(url);
statement = connection.createStatement();
ResultSet rs=null;
int p,m,n;
int PageSize=10;
int ShowPage=1;
int PageCount;
int flag=0; //标志位 查询是否都为空?
int flag2=0; //标志位 查询是否为第一个条件?
int i=1;
String sql="select * from clean_drug_like12";
String sql1="";
String sql2="select count(*) from clean_drug_like12";
String sql3="";
String search_txt_id="";
String search_txt_XLogP1="";
String search_txt_XLogP2="";
String search_txt_bonds1="";
String search_txt_bonds2="";
String ToPage=request.getParameter("ToPage");
if(request.getParameter("search_txt_id")!=null&&!request.getParameter("search_txt_id").equals("")){
search_txt_id=new String(request.getParameter("search_txt_id").trim().getBytes("8859_1"));
}
if(request.getParameter("search_txt_XLogP1")!=null&&!request.getParameter("search_txt_XLogP1").equals("")){
flag=1;
search_txt_XLogP1=new String(request.getParameter("search_txt_XLogP1").trim().getBytes("8859_1"));
}
if(request.getParameter("search_txt_XLogP2")!=null&&!request.getParameter("search_txt_XLogP2").equals("")){
flag=1;
search_txt_XLogP2=new String(request.getParameter("search_txt_XLogP2").trim().getBytes("8859_1"));
}
if(request.getParameter("search_txt_bonds1")!=null&&!request.getParameter("search_txt_bonds1").equals("")){
flag=1;
search_txt_bonds1=new String(request.getParameter("search_txt_bonds1").trim().getBytes("8859_1"));
}
if(request.getParameter("search_txt_bonds2")!=null&&!request.getParameter("search_txt_bonds2").equals("")){
flag=1;
search_txt_bonds2=new String(request.getParameter("search_txt_bonds2").trim().getBytes("8859_1"));
} if(search_txt_id!=null&&!search_txt_id.equals("")){
sql1=" where id="+search_txt_id;
sql3=" where id="+search_txt_id;
}
else if(flag==1){
if(search_txt_XLogP1!=null&&!search_txt_XLogP1.equals("")){
sql1=" where XLogP>="+search_txt_XLogP1;
flag2=1;
}
if(search_txt_XLogP2!=null&&!search_txt_XLogP2.equals("")){
if(flag2==1)
sql1=sql1+" and XLogP<="+search_txt_XLogP2;
else
{
sql1=" where XLogP<="+search_txt_XLogP1;
flag2=1;
}
}
if(search_txt_bonds1!=null&&!search_txt_bonds1.equals("")){
if(flag2==1)
sql1=sql1+" and RotatableBonds>="+search_txt_bonds1;
else{
sql1=" where RotatableBonds>="+search_txt_bonds1;
flag2=1;
}
}
sql3=sql1;
}
sql3=sql2+sql3;
rs=statement.executeQuery(sql3);
rs.next();
RowCount=rs.getInt(1);
PageCount=(RowCount+9)/10;
%>
<center>
<table border="0">
<tr>
<td>
<font size="6">查询结果</font> </td>
</tr>
<tr>
<td> 以下是搜索到的所有信息:
共:
<FONT color="blue">
<%=RowCount%>
</FONT>
条 共
<FONT COLOR="blue">
<%=PageCount%>
</FONT>
页 当前页为第
<FONT color="red">
<%=ShowPage%>
</FONT>
页
<%
if(PageCount>1){
if(ShowPage-3>0){
m=ShowPage-3;
}
else{
m=1;
}
if(ShowPage+3<PageCount){
n=ShowPage+3;
}
else{
n=PageCount;
}
%> 转到页码:[
<%
for(p=m;p<=n;p++){
if(ShowPage==p){
%> <font color="red"><b><%=p%></b></font>
<%
}
else{
%>
<a
<%
if(ShowPage==p){
%>
style="color:red"
<%
}
%>
href="index.jsp?ToPage=<%=p%>&i=<%=p+(p-1)*9%>&search_txt_id=<%=search_txt_id%>&search_txt_XLogP1=<%=search_txt_XLogP1%>&search_txt_XLogP2=<%=search_txt_XLogP2%>search_txt_bonds1=<%=search_txt_bonds1%>search_txt_bonds2=<%=search_txt_bonds2%>">
<%=p%>
</a>
<%
}
}
%>
]
<%
}
%>
</td>
</tr>
</table>
<table border="1" width="800">
<tr>
<td width="150" align="center">ID</td>
<td width="200" align="center">INFORMATION</td>
</tr>
<%
sql1=sql+sql1+"limit "+i+"10";
rs=statement.executeQuery(sql1);
while(rs.next()){
out.println("<tr align=center>");
out.println("<td align=center width=150 rowspan=2>"+rs.getInt("ID")+"</td>");
out.println("<td align=center width=150>"+"XLogP="+rs.getDouble("XLogP")+"</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td align=center width=200>"+"RotatableBonds="+rs.getInt("RotatableBonds"));
out.println("</td>");
out.println("</tr>");
out.println("</tr>");
}
%>
</table>
<TABLE BORDER="0" WIDTH="700">
<tr valign="baseline" align="center">
<td width="500">
</td>
<%
if(ShowPage!=1){
%> <td width="50">
<a href="index.jsp?ToPage=<%=ShowPage-1%>&i=<%=i-10%>&search_txt_id=<%=search_txt_id%>&search_txt_XLogP1=<%=search_txt_XLogP1%>&search_txt_XLogP2=<%=search_txt_XLogP2%>search_txt_bonds1=<%=search_txt_bonds1%>search_txt_bonds2=<%=search_txt_bonds2%>">
上一页
</a>
</td>
<%
}
if(ShowPage!=PageCount){
%>
<td width="50">
<a href="index.jsp?ToPage=<%=ShowPage+1%>&i=<%=i+10%>&search_txt_id=<%=search_txt_id%>&search_txt_XLogP1=<%=search_txt_XLogP1%>&search_txt_XLogP2=<%=search_txt_XLogP2%>search_txt_bonds1=<%=search_txt_bonds1%>search_txt_bonds2=<%=search_txt_bonds2%>">
下一页
</a>
</td>
<%
}
%>
</tr>
</TABLE>
<% rs.close();
statement.close();
connection.close();
sql1="";
sql3="";
}catch(Exception e){
System.out.println("Exception:"+e.getMessage());
}
%>
</center>
</body>
太长了没法看,但你的sql应该会有问题,看见很多没有加空格的,另外你的sql拼来拼去的话。初始化可以这样:
String sql = "select * from tble where 1=1 ";
现在再加入其他条件就直接
if(***不空){
sql += " and **=** "; //双引号前后加空格。
}另外就是拼好的sql在执行之前,最好吧sql 打印出来放数据库里面去执行看看对不对。