发布网友 发布时间:2023-09-26 15:05
共1个回答
热心网友 时间:2024-12-11 21:17
第一步建连接:就是一java连接就行
第二步是建立一个类这类的与数据库字段对象用于java的面向对象importjava.util.Date;
//存放数据的javabean,属性和表中的字段是一一对应的publicclassUser{
privateintuserid;
privateStringusername;
privateStringpassword;
privateDatelogintime;
publicUser(){
}
publicvoidsetUserid(intuserid){
this.userid=userid;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicvoid(Datelogintime){
this.logintime=logintime;
}
publicintgetUserid(){
returnuserid;
}
publicStringgetUsername(){
returnusername;
}
publicStringgetPassword(){
returnpassword;
}
publicDate(){
returnlogintime;
}}
第三步是建立一个SQL访问类:importjava.sql.*;importjava.util.*;publicclassUserDAO{
//按条件查找多条记录
publicListfindAll(Stringfilter){
Connectionconn=Db.();
Listdatas=newArrayList();
if(filter!=null){
filter="where"filter;
}else{
filter="";
}
try{
ps=conn.(
"select*fromusertb"filter);
ResultSetrs=ps.();
while(rs.next()){
Useruser=newUser();
user.setUserid(rs.getInt(1));
user.setUsername(rs.getString(2));
user.setPassword(rs.getString(3));
user.(rs.getDate(4));
datas.add(user);
}
}catch(ex){
ex.();
}finally{
Db.closeConn(conn);
}
returndatas;
}}第四步是这在jsp上用<%@pagecontentType="text/html;charset=GBK"import="com.ap.User"import="java.util.List"%>然后用