java如何通过Dao接口取得数据库数据【谢谢】
现在Dao接口中提供了query、update等方法,但是返回值怎么都是List啊。简单说一下如何通过方法获取数据并持久化呢。我只会语法,是个对象盲。我觉得好想是写个类继...
现在Dao接口中提供了query、update等方法,但是返回值怎么都是List啊。
简单说一下如何通过方法获取数据并持久化呢。
我只会语法,是个对象盲。我觉得好想是写个类继承Dao接口,然后就不知道了。一共需要几个类呢
您的回答将对我有很大影响,我在此谢过了 展开
简单说一下如何通过方法获取数据并持久化呢。
我只会语法,是个对象盲。我觉得好想是写个类继承Dao接口,然后就不知道了。一共需要几个类呢
您的回答将对我有很大影响,我在此谢过了 展开
1个回答
展开全部
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;
}
}
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |