我的代码如下:
package com.dhee.test.db;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;public class UserinfoDAO {//对改表进行操作的
public boolean select(String name,String pass){//根据用户名和密码查询是否存在
Getconnn g = new Getconnn();
Connection con = g.createconn();//获取连接对象
Statement stam;
boolean b = false;
try {
String sql=null;
sql="select * from userinfo where name =? and pass=?";
PreparedStatement pstam = con.prepareStatement(sql);//有问题,要改
System.out.println("sql============"+sql);
pstam.setString(1,name);//该方法用来对问好赋值。第一个参数是问号的位子(对第几个问好赋值)第二个参数是要赋的值
pstam.setString(2,pass);
ResultSet rs =pstam.executeQuery();//执行sql语句
System.out.println("rss==============="+rs);
if(rs.next()){
//该用户合法
b=true;
}else{
//该用户不合法
b =false;
}
con.close();
pstam.close();
} catch (Exception e) {

}
return b;
}
public String selectpower(String name,String pass){//根据用户名和密码查询该用户的权限
Getconnn g = new Getconnn();
Connection con = g.createconn();//获取连接对象
String power ="";
Statement stam;
try {
stam = con.createStatement();
String sql = "select name from power where id in (select power_id from group_power where group_id in (select group_id from user_group where user_id = (select id from userinfo where name ='"+name+"' and pass='"+pass+"')))";
ResultSet rs = stam.executeQuery(sql);
while(rs.next()){
 power = rs.getString("name");
}
} catch (Exception e) {
System.out.println("查询用户权限的时候出错了");
}
return power;
}
// public static void main(String args[]){
// UserinfoDAO u = new UserinfoDAO();
// Map map = u.selectALL();
// System.out.println(map.size());
// }
public Map selectALL(){//查询表中的所有的用户持久化装到集合中。map集合。key部分存用户名。值部分存对象
Map m = new LinkedHashMap();
Getconnn g = new Getconnn();
Connection con = g.createconn();//获取连接对象
Statement stam;
boolean b = false;
try{
PreparedStatement pstam = con.prepareStatement("select * from userinfo");
ResultSet rs = pstam.executeQuery();
while(rs.next()){
Userinfo u = new Userinfo();
u.setId(rs.getInt("id"));
u.setName(rs.getString("name"));      
u.setPass(rs.getString("pass"));
u.setMail(rs.getString("mail"));
u.setSheng(rs.getString("pre"));
u.setAihao(rs.getString("aihao"));
u.setTime(rs.getTimestamp("ntime"));
u.setSex(rs.getString("sex"));
u.setYorn(rs.getInt("yorn"));
String p = selectpower(u.getName(),u.getPass());
u.setPower(p);
m.put(u.getName(),u);
}
}catch(Exception e){
System.out.println(e.getMessage());
}
return m;
}

public int selectMaxId(){
Getconnn g = new Getconnn();
Connection con = g.createconn();//获取连接对象
Statement stam;
int id = -1;
try{
stam = con.createStatement();
ResultSet rs = stam.executeQuery("select max(id) as mid from userinfo");
if(rs.next()){
id = rs.getInt("mid");
}
}catch(Exception e){

}
return id;
}
public boolean insert(Userinfo u){
Getconnn g = new Getconnn();
Connection con = g.createconn();//获取连接对象
boolean b = true;
try {
String sql = "insert into userinfo (id,name,pass,mail,ntime,sheng,aihao,sex,yorn) values(?,?,?,?,?,?,?,?,?)";
PreparedStatement pstam = con.prepareStatement(sql);
pstam.setInt(1,u.getId());
pstam.setString(2,u.getName());
pstam.setString(3,u.getPass());
pstam.setString(4,u.getMail());
pstam.setTimestamp(5,u.getTime());
pstam.setString(6,u.getSheng());
pstam.setString(7,u.getSex());
pstam.setString(8,u.getAihao());
pstam.setInt(9,u.getYorn());
int i = pstam.executeUpdate();

if(i == 0){
b = false;
}else{
Statement stam = con.createStatement();
ResultSet rs = stam.executeQuery("select max(id) as mid from user_group");
int u_gid = -1;
if(rs.next()){
u_gid = rs.getInt("mid");
}
PreparedStatement p2 = con.prepareStatement("insert into user_group (id,user_id,group_id) values(?,?,?)");
p2.setInt(1,u_gid+1);
p2.setInt(2,u.getId());
p2.setInt(3,2);
int i3 = p2.executeUpdate();
if(i3 == 0){
b = false;
}
}
} catch (SQLException e) {

}
return b;
}
public boolean update(Userinfo u){
Getconnn g = new Getconnn();
Connection con = g.createconn();//获取连接对象
PreparedStatement pstam = null;
int i = -1;
try {
pstam = con.prepareStatement("update userinfo set pass = ? , mail = ?, sheng = ?, aihao = ? ,sex = ? where name = ?");
pstam.setString(1,u.getPass());
pstam.setString(2,u.getMail());
pstam.setString(3,u.getSheng());
pstam.setString(4,u.getAihao());
pstam.setString(5,u.getSex());
pstam.setString(6,u.getName());
i = pstam.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
if(i > 0){
return true;
}else{
return false;
}
}
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主截止到2008-06-20 21:11:13的汇总数据:
    注册日期:2008-6-19
    上次登录:2008-6-20
    发帖数:1                  发帖分:20                 
    结贴数:0                  结贴分:0                  
    结贴率:0.00 %        结分率:0.00 %        
    如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html