小弟刚学到jsp和javaBean这里,下面的程序也能运行,可只能读到一条数据,不知道用什么方法可以循环读取到更多的数据,希望大侠们帮帮我,谢谢package test; //处理数据库的Bean
import java.sql.*;
public class UserSql
{
public static UserInfo userSql()
{
UserInfo ui = new UserInfo();
try
{
Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=127.0.0.1;uid=sa;pwd=123456;Database=TestJsp");
String sql="select * from userinfo";
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(sql);
while (rs.next())
{
ui.setUsername(rs.getString("username"));
ui.setPassword(rs.getString("password"));
}
}
catch(Exception e){
System.out.println(e.toString());
}
return ui;
}
}
package test; //赋值Beanpublic class UserInfo
{
String username = null;
String password = null;
public UserInfo()
{ }
public void setUsername(String username)
{
this.username=username;
}
public void setPassword(String password)
{
this.password=password;
}
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
public void init()
{
UserInfo ui = UserSql.userSql();
this.username = ui.username;
this.password = ui.password;
}
}<%@page import="test.UserInfo"%>
<% UserInfo userinfo = new UserInfo();%>
<html>
<Title>Test</Title>
<%userinfo.init();%>
<body>
<div> <%=userinfo.getUsername()%></div>
<div> <%=userinfo.getPassword()%></div>
</body>
</html>
import java.sql.*;
public class UserSql
{
public static UserInfo userSql()
{
UserInfo ui = new UserInfo();
try
{
Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=127.0.0.1;uid=sa;pwd=123456;Database=TestJsp");
String sql="select * from userinfo";
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery(sql);
while (rs.next())
{
ui.setUsername(rs.getString("username"));
ui.setPassword(rs.getString("password"));
}
}
catch(Exception e){
System.out.println(e.toString());
}
return ui;
}
}
package test; //赋值Beanpublic class UserInfo
{
String username = null;
String password = null;
public UserInfo()
{ }
public void setUsername(String username)
{
this.username=username;
}
public void setPassword(String password)
{
this.password=password;
}
public String getUsername()
{
return username;
}
public String getPassword()
{
return password;
}
public void init()
{
UserInfo ui = UserSql.userSql();
this.username = ui.username;
this.password = ui.password;
}
}<%@page import="test.UserInfo"%>
<% UserInfo userinfo = new UserInfo();%>
<html>
<Title>Test</Title>
<%userinfo.init();%>
<body>
<div> <%=userinfo.getUsername()%></div>
<div> <%=userinfo.getPassword()%></div>
</body>
</html>
while (rs.next())
{
ui.setUsername(rs.getString("username"));
ui.setPassword(rs.getString("password"));
}
都在ui上进行set,当然只有一个结果.应该用集合(数组或list)来保存多个ui实例最后也只打印了一个结果而已:
<body>
<div> <%=userinfo.getUsername()%></div>
<div> <%=userinfo.getPassword()%></div>
</body>
用循环
{
ui.setUsername(rs.getString("username"));
ui.setPassword(rs.getString("password"));
}
你这样ui里只保存了一条记录阿
你应该把你每查询出来的一条记录保存到List中就可以了
你可以这样做:
List<UserInfo> list=new ArrayList<UserInfo>();
UserInfo ui = new UserInfo();
while (rs.next())
{
ui.setUsername(rs.getString("username"));
ui.setPassword(rs.getString("password"));
list.add(ui);
}
最后在你的页面把这个list循环显示出来就可以了
List<UserInfo> list = userSql();
for (UserInfo ui : list) {
System.out.println("username=" + ui.getUsername() + ",password=" + ui.getPassword());
}
} public static List<UserInfo> userSql() { // 这里返回用户信息的列表
try {
Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};Server=127.0.0.1;uid=sa;pwd=123456;Database=TestJsp");
String sql = "select * from userinfo";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
UserInfo ui = null;
List<UserInfo> list = new ArrayList<UserInfo>();
while (rs.next()) {
ui = new UserInfo(); // 在这里每次构造一个新的对象
ui.setUsername(rs.getString("username"));
ui.setPassword(rs.getString("password"));
list.add(ui); // 保存到列表中
}
} catch (Exception e) {
System.out.println(e.toString());
}
return list;
}
<html>
<Title>Test</Title>
<body>
<%
List<UserInfo> list = UserSql.userSql();
for(UserInfo userinfo : list){
%>
<div> <%=userinfo.getUsername()%></div>
<div> <%=userinfo.getPassword()%></div>
<%
}
%>
</body>
</html>