package com.whut.lease.DAO;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.whut.lease.PO.Bogo;
public class BogoPAO {
private Statement state;
private Connection conn;
private ResultSet rs;
/**
* 新增留言
* @param l
*/
public void add(Bogo l){
try{
//得到连接
conn=DBtools.getConnection();
state= conn.createStatement();
String sql="INSERT INTO ofuserinfo VALUES( "+getMAX()+",'"+l.getSquare()+"','"+l.getPrice()+"',sysdate,'"+l.getBegintime()+"','"+l.getDeadline()+"')";
System.out.println(sql);
state.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
finally{
DBtools.cloaeConn(conn);
DBtools.cloaeState(state);
}
}
/**
* 得到列的最大值
* @return
*/
private int getMAX(){
Connection conn =null;
Statement state =null;
ResultSet rs =null;
int max = 0;
try{
conn = DBtools.getConnection();
state = conn.createStatement();
//执行SQL语句
String sql = "Select MAX(lid) max from ofuserinfo";
rs = state.executeQuery(sql);
//取值
if(rs.next()){
max = rs.getInt("max");
}
}catch(Exception e){
e.printStackTrace();
}
finally{
DBtools.cloaeResultSet(rs);
DBtools.cloaeState(state);
DBtools.cloaeConn(conn);
}
return max+1;
}
/**
* 根据sql语句查询数据库元素
* @return al
*/
public ArrayList getAllLogo(String sql)
{
ArrayList al = new ArrayList();
try{
conn = DBtools.getConnection();
state = conn.createStatement();
//执行sql语句
rs = state.executeQuery(sql);
while(rs.next()){
Bogo p = new Bogo();
p.setLid(_id);
p.setSquare(square);
p.setLname(uname);
p.setLsex(usex);
p.setLbirth(ubirth);
p.setLemail(uemail);
p.setLtel(utel);
p.setlID(uID);
p.setLaddress(uaddress);
al.add(p);
}
}catch(Exception e){
e.printStackTrace();
}
finally{
DBtools.cloaeResultSet(rs);
DBtools.cloaeState(state);
DBtools.cloaeConn(conn);
}
return al;
}
/**
* 通过ID查询
* @param sql
* @return
*/
public Bogo findlogoById(int id){
Bogo p = new Bogo();
try{
conn = DBtools.getConnection();
state = conn.createStatement();
String sql="select * from ofuserinfo where lid="+id;
rs = state.executeQuery(sql);
if(rs.next()){
p.setLid(id);
p.setSquare(square);
p.setLname(uname);
p.setLsex(usex);
p.setLbirth(ubirth);
p.setLemail(uemail);
p.setLtel(utel);
p.setlID(uID);
p.setLaddress(uaddress);
}
}catch(Exception e){
e.printStackTrace();
}
finally{
DBtools.cloaeResultSet(rs);
DBtools.cloaeState(state);
DBtools.cloaeConn(conn);
}
return p;
}
/**
* 通过ID删除
* @param id
*/
public void deleteById(int id){
try{
//得到连接
conn=DBtools.getConnection();
state= conn.createStatement();
String sql="delete from ofuserinfo where lid="+id;
state.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
finally{
DBtools.cloaeConn(conn);
DBtools.cloaeState(state);
}
}
/**
* 修改数据库中的信息
* @param p
*/
public void updataById(Bogo p){
try{
//得到连接
conn=DBtools.getConnection();
state= conn.createStatement();
String sql="update ofuserinfo t set user_name='"+p.getLname()+"', where t.lid="+p.getLid();
state.executeUpdate(sql);
}catch(SQLException e){
e.printStackTrace();
}
finally{
DBtools.cloaeConn(conn);
DBtools.cloaeState(state);
}
}
/**
* 获得总条数
* @return
*/
public int totalRoles(){
//设置数据库变量
Connection conn =null;
Statement state = null;
ResultSet rs = null;
int num = 0;
try{
conn = DBtools.getConnection();
state = conn.createStatement();
String sql ="select count(lid) nu from ofuserinfo";
rs = state.executeQuery(sql);
if(rs.next()){
num = rs.getInt("nu");
}
}catch(Exception e){
e.printStackTrace();
}
finally{
DBtools.cloaeResultSet(rs);
DBtools.cloaeState(state);
DBtools.cloaeConn(conn);
}
return num;
}
}