/**
* 添加
*/
public boolean add(Msg obj){
Connection con=DBConnection.getConnction();
PreparedStatement ps = null ;
boolean bret=false;
try{
ps=con.prepareStatement("insert into msgInfo (privyName,telphone,msgContent,)values(?,?,?)");
//System.out.print(obj.getPrivyName());
ps.setString(1,obj.getPrivyName());
ps.setString(2, obj.getTelphone());
ps.setString(3,obj.getMsgContent());
int i = ps.executeUpdate();
if(i>0){
bret=true;
}
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
try{
if(ps!=null)ps.close();
if(con!=null)con.close();
}catch(Exception e2){
System.out.println(e2.getMessage());
}
}
return bret;
}
/**
* 查询所有
*/
public List getAllMsg(String userID){//用util的List
Connection con=DBConnection.getConnction();
ArrayList al = new ArrayList();
PreparedStatement ps = null ;
try {
ps=con.prepareStatement("select * from msgInfo where userID=? order by msgID desc");
ps.setString(1,userID);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Msg bean=new Msg();
bean.setMsgID(Integer.toString(rs.getInt(1)));
bean.setPrivyName(rs.getString(2));
bean.setTelphone(rs.getString(3));
bean.setMsgContent(rs.getString(4));
al.add(bean);
}
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
if (con != null)
try {
ps.close();
con.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
return al;
}
/**
* 查询一条记录
*/
public List getOneMsg(String msgID){//用util的List
Connection con=DBConnection.getConnction();
ArrayList al = new ArrayList();
PreparedStatement ps = null ;
try {
ps=con.prepareStatement("select * from msgInfo where msgID=?");
ps.setString(1,msgID);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Msg bean=new Msg();
bean.setPrivyName(rs.getString(2).trim());
bean.setTelphone(rs.getString(3).trim());
bean.setMsgContent(rs.getString(4).trim());
al.add(bean);
}
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
if (con != null)
try {
ps.close();
con.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
return al;
}
/**
* 删除
*/
public boolean removeMsg(String msgID){
Connection con=DBConnection.getConnction();
PreparedStatement ps = null ;
boolean bret=false;
try{
ps=con.prepareStatement("delete msgInfo where msgID=?");
ps.setString(1,msgID);
int i = ps.executeUpdate();
if(i>0){
bret=true;
}
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
try{
if(ps!=null)ps.close();
if(con!=null)con.close();
}catch(Exception e2){
System.out.println(e2.getMessage());
}
}
return bret;
}
/**
* 修改
*/
public boolean ModifyMsg(Msg obj){
Connection con=DBConnection.getConnction();
PreparedStatement ps = null ;
boolean bret=false;
try{
ps=con.prepareStatement("Update msgInfo set privyName=?,telphone=?,msgContent=?");
ps.setString(1, obj.getPrivyName());
ps.setString(2, obj.getTelphone());
ps.setString(3, obj.getMsgContent());
int i = ps.executeUpdate();
if(i>0){
bret=true;
}
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
try{
if(ps!=null)ps.close();
if(con!=null)con.close();
}catch(Exception e2){
System.out.println(e2.getMessage());
}
}
return bret;
}
/**
* 得到总记录条数
*/
public int getRows(String userID){
Connection con=DBConnection.getConnction();
PreparedStatement ps = null ;
int row=0;
try {
ps=con.prepareStatement("select count(*) from msgInfo where userID=?");
ps.setString(1,userID);
ResultSet rs=ps.executeQuery();
if(rs.next()){
row=rs.getInt(1);
}
//System.out.println(row);
}
catch(Exception e){
row=0;
}
return row;
}