java如何通过Dao接口取得数据库数据【谢谢】

现在Dao接口中提供了query、update等方法,但是返回值怎么都是List啊。简单说一下如何通过方法获取数据并持久化呢。我只会语法,是个对象盲。我觉得好想是写个类继... 现在Dao接口中提供了query、update等方法,但是返回值怎么都是List啊。
简单说一下如何通过方法获取数据并持久化呢。

我只会语法,是个对象盲。我觉得好想是写个类继承Dao接口,然后就不知道了。一共需要几个类呢

您的回答将对我有很大影响,我在此谢过了
展开
 我来答
117168999
2007-09-20 · 超过31用户采纳过TA的回答
知道答主
回答量:152
采纳率:0%
帮助的人:0
展开全部
import java.util.List;

import com.icss.xxx.domain.User;

public interface UserDAO {
public int login(User user);

public User queryUser(String userId) throws Exception;

public int updateUser(User user);

public int addUser(User user);

public int deleteUser(String userId) throws Exception;

public List getAllUsers();

public User getUser(String userName) throws Exception;

}

---------------------------------------
下面是实现类

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.xxx.xxx.common.ConnectionFactory;
import com.xxx.xxx.common.DatabaseUtils;
import com.xxx.xxx.dao.UserDAO;
import com.xxx.xxx.domain.User;

public class UserDAOImpl implements UserDAO {

public List getAllUsers() {
Connection conn=ConnectionFactory.getConnection();
List users=new ArrayList();
Statement stmt=null;
ResultSet rs=null;
try {
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from training");
User user=null;
while(rs.next()){
user=new User();
user.setUserId(rs.getString("userid"));
user.setUserName(rs.getString("username"));
user.setUserPass(rs.getString("userpass"));
users.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DatabaseUtils.release(rs, stmt, conn);
}
return users;
}

public int login(User user) {
int flag=0;
Connection conn=ConnectionFactory.getConnection();
PreparedStatement pstm=null;
ResultSet rs=null;
try {
pstm=conn.prepareStatement("select * from train_user where user_name=? and user_pass=?");
pstm.setString(1, user.getUserName());
pstm.setString(2, user.getUserPass());
rs=pstm.executeQuery();
if(rs.next()){
if(rs.getString("admin").equals("1")){
flag=1;
}else if(rs.getString("admin").equals("0")){
flag=2;
}else{
flag=0;
};
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DatabaseUtils.release(rs, pstm, conn);
}
return flag;
}

public User queryUser(String userId) {

Connection conn = ConnectionFactory.getConnection();
PreparedStatement pstm = null;
ResultSet rs = null;
User user = null;

try {

pstm = conn.prepareStatement("select * from test where userid=?");
pstm.setString(1, userId);
rs = pstm.executeQuery();

if(rs.next()){
user = new User();
user.setUserId(rs.getString("userid"));
user.setUserName(rs.getString("username"));
user.setUserPass(rs.getString("userPass"));

}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

DatabaseUtils.release(rs, pstm, conn);
}

return user;
}

public int updateUser(User user) {

Connection conn = ConnectionFactory.getConnection();
PreparedStatement pstm = null;
ResultSet rs = null;

int flag = 0;
try {

pstm = conn.prepareStatement("update test set username=?, userpass=? where userid=?");

pstm.setString(1, user.getUserName());

pstm.setString(2, user.getUserPass());

pstm.setString(3, user.getUserId());

flag = pstm.executeUpdate();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

DatabaseUtils.release(rs, pstm, conn);
}

return flag;
}

public int addUser(User user) {
Connection conn=ConnectionFactory.getConnection();
PreparedStatement pstm=null;
int flag=0;
try {
pstm=conn.prepareStatement("insert into train_user(user_id,user_name,user_pass,train_id,train_nature,admin) values(?,?,?,?,?,0)");
pstm.setString(1,user.getUserId());
pstm.setString(2,user.getUserName());
pstm.setString(3,user.getUserPass());
pstm.setString(4,user.getTrain_id());
pstm.setString(5,user.getTrain_nature());
pstm.executeUpdate();
flag=1;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

DatabaseUtils.release(null, pstm, conn);
}
return flag;
}

public int deleteUser(String userId) throws Exception {

Connection conn = ConnectionFactory.getConnection();
PreparedStatement pstm = null;
int flag = 0;
try {

pstm = conn.prepareStatement("delete from test where userid=?");

pstm.setString(1, userId);

flag = pstm.executeUpdate();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

DatabaseUtils.release(null, pstm, conn);
}
return flag;

}

public User getUser(String userName) throws Exception {
// TODO Auto-generated method stub
return null;
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式