- 论坛徽章:
- 0
|
MySql版本
User.class
/*
* Created on 2005-1-9
*/
package com.jilaninfo.user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.jilaninfo.app.Const;
import com.jilaninfo.appserver.server.DBUtil;
import com.jilaninfo.commons.DataTable;
import com.jilaninfo.commons.Tools;
/**
* @author elgs
*/
public class User implements DataTable
{
private int id;
private String user;
private String pass;
private String name;
private boolean check() throws SQLException
{
Connection conn = null;
try
{
conn = DBUtil.getConnection(Const.DB_CORE);
PreparedStatement ps = conn
.prepareStatement("select * from user where user=?");
ps.setString(1, getUser());
ResultSet rs = ps.executeQuery();
if (rs.next())
{
return false;
}
return true;
}
finally
{
if (conn != null)
conn.close();
}
}
public boolean load(int id) throws SQLException
{
Connection conn = null;
try
{
conn = DBUtil.getConnection(Const.DB_CORE);
PreparedStatement ps = conn
.prepareStatement("select * from user where id=?");
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next())
{
setId(rs.getInt("id"));
setUser(rs.getString("user"));
pass = rs.getString("pass");
setName(rs.getString("name"));
return true;
}
return false;
}
finally
{
if (conn != null)
conn.close();
}
}
public void create() throws SQLException
{
if (!check())
{
throw new RuntimeException("User already existed.");
}
Connection conn = null;
try
{
conn = DBUtil.getConnection(Const.DB_CORE);
conn.setAutoCommit(false);
PreparedStatement ps = conn
.prepareStatement("insert into user (user,pass,name) values(?,?,?)");
ps.setString(1, getUser());
ps.setString(2, getPass());
ps.setString(3, getName());
ps.executeUpdate();
ps.close();
ps = conn.prepareStatement("select LAST_INSERT_ID() from user");
ResultSet rs = ps.executeQuery();
if (rs.next())
{
setId(rs.getInt(1));
}
conn.commit();
}
catch (SQLException e)
{
conn.rollback();
throw e;
}
finally
{
if (conn != null)
conn.close();
}
}
public void edit(int id) throws SQLException
{
if (!check())
{
throw new RuntimeException("User already existed.");
}
Connection conn = null;
try
{
conn = DBUtil.getConnection(Const.DB_CORE);
PreparedStatement ps = conn
.prepareStatement("update user set user=?,pass=?,name=? where id=?");
ps.setString(1, getUser());
ps.setString(2, getPass());
ps.setString(3, getName());
ps.setInt(4, id);
ps.executeUpdate();
}
finally
{
if (conn != null)
conn.close();
}
}
public void remove(int id) throws SQLException
{
Connection conn = null;
try
{
conn = DBUtil.getConnection(Const.DB_CORE);
PreparedStatement ps = conn
.prepareStatement("delete from user where id=?");
ps.setInt(1, id);
ps.executeUpdate();
}
finally
{
if (conn != null)
conn.close();
}
}
public String getAllRights() throws SQLException
{
String sql0 = "select priv.id from priv,userpriv "
+ "where priv.id=userpriv.privid and userid=? "
+ "union select priv.id from priv,usergroups,groupspriv "
+ "where usergroups.userid=? and priv.id=groupspriv.privid "
+ "and usergroups.groupsid=groupspriv.groupsid";
StringBuffer sb = new StringBuffer();
sb.append(0);
sb.append(",");
Connection con = null;
int i = 0;
try
{
con = DBUtil.getConnection(Const.DB_CORE);
PreparedStatement ps = con.prepareStatement(sql0);
ps.setInt(1, getId());
ps.setInt(2, getId());
ResultSet rs = ps.executeQuery();
while (rs.next())
{
sb.append(rs.getInt(1));
sb.append(",");
}
sb.append(0);
return sb.toString();
}
finally
{
if (con != null)
{
con.close();
}
}
}
public boolean hasRight(int privno) throws SQLException
{
if (privno == 0)
{
return true;
}
if (privno
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4960/showart_19446.html |
|