您的位置:首页 > 编程语言 > Java开发

java链接数据库以及对数据库的操作

2016-05-24 22:40 381 查看
package cn.edu.xbmu.dao;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import cn.edu.xbmu.models.UserModel;

public class UserDao {

private DBHelper helper=new DBHelper();

/**

*

* 添加用户

* @param model

* @return

* @throws Exception

*/

public int save(UserModel model)throws Exception{

int i=0;

try {

String sql=”insert into
user
(userName,userPass) values(?,?)”;

Object values[]=new Object[]{model.getUserName(),model.getUserPass()};

i=helper.executeUpdate(sql, values);

} catch (Exception e) {

e.printStackTrace();

throw e;

}finally{

helper.close();

}

return i;

}

/**

*

* 修改用户信息

* @param model

* @return

* @throws Exception

*/

public int update(UserModel model)throws Exception{

int result=0;

try {

String sql=”update
user
set userName=?,userPass=? where id=?”;

result=helper.executeUpdate(sql,model.getUserName(),model.getUserPass(),model.getId());

} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
helper.close();
}
return result;
}
/**
*
* 删除用户信息
* @param id
* @return
* @throws Exception
*/
public int delete(int id)throws Exception{
int result=0;
try {
String sql="delete from `user` where id=?";
result=helper.executeUpdate(sql, id);
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
helper.close();
}
return result;
}

/**
*
* 用户登录
*/
public  UserModel login(UserModel model)throws Exception{
UserModel result=null;
try {
String sql="select * from `user` where userName=? and userPass=?";
ResultSet rs=helper.executeQuery(sql,model.getUserName(),model.getUserPass());
if(rs.next()){
result =new UserModel();
result.setUserName(rs.getString("userName"));
result.setUserPass(rs.getString("userPass"));
result.setId(rs.getInt("id"));
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
finally{
helper.close();
}
return result;
}
/**
*
* 在数据库中查找
* @param userName
* @return
* @throws Exception
*/
public boolean findByName(String userName)throws Exception{
try {
String sql="select count(1) from `user` where userName=? ";
int i=helper.executeScalar(sql, userName);
if(i>0){
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
helper.close();
}
}
/**
* 查找所有用户
* @return
* @throws Exception
*/
public List<UserModel> findAll()throws Exception{
List<UserModel> list =new ArrayList<UserModel>();
try {
String sql="select * from `user`";
ResultSet rs=helper.executeQuery(sql);
while(rs.next()){
UserModel m=new UserModel();
m.setId(rs.getInt("id"));
m.setUserName(rs.getString("userName"));
m.setUserPass(rs.getString("userPass"));
list.add(m);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
helper.close();
}
return list;
}
/**
*
* 查找某个人
* @param id
* @return
* @throws Exception
*/
public UserModel findById(int id)throws Exception{
ResultSet rs=null;
UserModel model=null;
try {
String sql="select * from `user` where id=?";
rs=helper.executeQuery(sql, id);
if(rs.next()){
int id1=rs.getInt("id");
String userName=rs.getString("userName");
String userPass=rs.getString("userPass");
model=new UserModel(id1,userName,userPass);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
helper.close();
}
return model;
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: