UserTable.java
/**
* UserTable entity.
*
* @author MyEclipse Persistence Tools
*/public class UserTable implements java.io.Serializable { // Fields private Integer userId;
private String userName;
private String userPass;
private String email; // Constructors /** default constructor */
public UserTable() {
} /** minimal constructor */
public UserTable(String userName, String userPass) {
this.userName = userName;
this.userPass = userPass;
} /** full constructor */
public UserTable(String userName, String userPass, String email) {
this.userName = userName;
this.userPass = userPass;
this.email = email;
} // Property accessors public Integer getUserId() {
return this.userId;
} public void setUserId(Integer userId) {
this.userId = userId;
} public String getUserName() {
return this.userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getUserPass() {
return this.userPass;
} public void setUserPass(String userPass) {
this.userPass = userPass;
} public String getEmail() {
return this.email;
} public void setEmail(String email) {
this.email = email;
}}
执行操作的类UserOperate.java
import java.util.List;import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import sun.text.CompactShortArray.Iterator;public class UserOperate { private Session session=null;
public UserOperate(){
//操作数据库的Session
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
this.session=factory.openSession();
}
public List queryAll(){
List l=null;
String hql="From UserTable as u";
Query q=this.session.createQuery(hql);
l=q.list();
return l;
}
}测试的类public class TestUser { /**
* @param args
*/
public static void main(String[] args) {
//实例化UserOperate对象
UserOperate up=new UserOperate();
UserTable u=new UserTable();
up.queryAll();
System.out.println(u.getUserName());
System.out.println(u.getUserPass());
System.out.println(u.getEmail());
}}我想问的是想把查询到的数据显示出来 该怎么写代码 在TestUser.java或者UserOperate.java中该怎么样写代码执行的时候让数据显示出来 不会写代吗 哪位帮帮忙
/**
* UserTable entity.
*
* @author MyEclipse Persistence Tools
*/public class UserTable implements java.io.Serializable { // Fields private Integer userId;
private String userName;
private String userPass;
private String email; // Constructors /** default constructor */
public UserTable() {
} /** minimal constructor */
public UserTable(String userName, String userPass) {
this.userName = userName;
this.userPass = userPass;
} /** full constructor */
public UserTable(String userName, String userPass, String email) {
this.userName = userName;
this.userPass = userPass;
this.email = email;
} // Property accessors public Integer getUserId() {
return this.userId;
} public void setUserId(Integer userId) {
this.userId = userId;
} public String getUserName() {
return this.userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getUserPass() {
return this.userPass;
} public void setUserPass(String userPass) {
this.userPass = userPass;
} public String getEmail() {
return this.email;
} public void setEmail(String email) {
this.email = email;
}}
执行操作的类UserOperate.java
import java.util.List;import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import sun.text.CompactShortArray.Iterator;public class UserOperate { private Session session=null;
public UserOperate(){
//操作数据库的Session
Configuration config=new Configuration().configure();
SessionFactory factory=config.buildSessionFactory();
this.session=factory.openSession();
}
public List queryAll(){
List l=null;
String hql="From UserTable as u";
Query q=this.session.createQuery(hql);
l=q.list();
return l;
}
}测试的类public class TestUser { /**
* @param args
*/
public static void main(String[] args) {
//实例化UserOperate对象
UserOperate up=new UserOperate();
UserTable u=new UserTable();
up.queryAll();
System.out.println(u.getUserName());
System.out.println(u.getUserPass());
System.out.println(u.getEmail());
}}我想问的是想把查询到的数据显示出来 该怎么写代码 在TestUser.java或者UserOperate.java中该怎么样写代码执行的时候让数据显示出来 不会写代吗 哪位帮帮忙
System.out.println(u.getUserName());
System.out.println(u.getUserPass());
System.out.println(u.getEmail());
} 替换为:List List = up.queryAll(); for (int i = 0; i < list.size(); i++) {
u = (UserTable) list.get(i);
System.out.println(u.getUserName());
System.out.println(u.getUserPass());
System.out.println(u.getEmail()); }
<table border="1" bgcolor="red" >
<tr bgcolor="green"><th>用户名</th><th>密码</th><th>邮箱</th>tr>
<% List l=(List)request.getAttribute("list");
UserTable b=new UserTable();
for(int i=0;i<l.size();i++){
b=(UserTable)l.get(i);
%>
<tr>
<td><%=b.getUserName()%></td>
<td><%=b.getUserPass()%></td>
<td><%=b.getEmail()%></td><%
}
%>
</table>
for(Iterator it = list.iterator();list.hasNext();){
u = (UserTable)it.next();
System.out.println(u.getUserName());
System.out.println(u.getUserPass());
System.out.println(u.getEmail());
}
注意导包
System.out.println(list);