求jsp+javabean实现增删改查等操作 5
2个回答
展开全部
这是我以前写的代码,希望对你有所帮助,这是最基础的,以后学习hibernate框架的时候,代码就更加简单了,你也可以去学习下
/**
* 数据库
* **/
public class BaseDao {
public Connection connection() {
String forname = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:uework";
String user = "epwork";
String pwd = "passowrd";
Connection conn = null;
try {
Class.forName(forname);
} catch (Exception e) {
System.err.print(e.getMessage());
}
try {
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
System.err.print(e.getMessage());
}
return conn;
}
/**
*这里是关闭
**/
public void closeAll(Connection conn, ResultSet rs, PreparedStatement stmt) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
}
}
/**
* 添加
* **/
public int addPingLun(pingluninfo pingluninfo) {
PreparedStatement stmt = null;
int rows = 0;
Connection conn = connection();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sql = "insert into pingluninfo(Msgid,discussUserid,Msg,Msgtime,Rbid)values(seq_n.nextval,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?)";
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, pingluninfo.getDiscussUserid());
stmt.setString(2, pingluninfo.getMsg());
stmt.setString(3, sdf.format(pingluninfo.getMsgtime()));
stmt.setInt(4, pingluninfo.getRbid());
rows = stmt.executeUpdate();
if (rows <= 0) {
return -1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll(conn, null, stmt);
}
return rows;
}
/**
* 查询
* **/
public List<Object> selPingLun(int Rbid, int pageSize,int pageIndex) {
PreparedStatement stmt = null;
ResultSet rs = null;
Connection conn = connection();
List<Object> pList = new ArrayList<Object>();
String sql =" select Msgid,discussUserid,Msg,Msgtime,Rbid from (select rownum as r,t.*from" +
"(select pingluninfo.*from pingluninfo " +
"where Rbid=? order by Msgtime desc) t" +
" where rownum <="+pageSize*pageIndex+") where r>"+pageSize*(pageIndex-1)+"";
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, Rbid);
rs = stmt.executeQuery();
while (rs.next()) {
pingluninfo pingluninfo = new pingluninfo();
//List<Bifm> bList= new ArrayList<Bifm>();
Bifm bifm =new Bifm();
pingluninfo.setMsgid(rs.getInt("Msgid"));
pingluninfo.setDiscussUserid(rs.getInt("discussUserid"));
pingluninfo.setMsg(rs.getString("Msg"));
pingluninfo.setMsgtime(rs.getDate("Msgtime"));
pingluninfo.setRbid(rs.getInt("Rbid"));
String username=username(pingluninfo.getDiscussUserid());
bifm.setUsername("<a href=''>"+username+"</a>");
pingluninfo.setBifminfo(bifm);
pList.add(pingluninfo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll(conn, rs, stmt);
}
return pList;
}
/**
* 修改
* **/
public int delFans(int userid, int fansuserid) {
PreparedStatement stmt = null;
Connection conn = connection();
int rows = 0;
String sql = "update receiveinfo set fansUserid=? where userid=? and fansuserid=?)";
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, null);
stmt.setInt(2, userid);
stmt.setInt(3, fansuserid);
rows = stmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll(conn, null, stmt);
}
return rows;
}
/**
* 数据库
* **/
public class BaseDao {
public Connection connection() {
String forname = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:uework";
String user = "epwork";
String pwd = "passowrd";
Connection conn = null;
try {
Class.forName(forname);
} catch (Exception e) {
System.err.print(e.getMessage());
}
try {
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException e) {
System.err.print(e.getMessage());
}
return conn;
}
/**
*这里是关闭
**/
public void closeAll(Connection conn, ResultSet rs, PreparedStatement stmt) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
System.err.print(e.getMessage());
}
}
}
}
/**
* 添加
* **/
public int addPingLun(pingluninfo pingluninfo) {
PreparedStatement stmt = null;
int rows = 0;
Connection conn = connection();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sql = "insert into pingluninfo(Msgid,discussUserid,Msg,Msgtime,Rbid)values(seq_n.nextval,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'),?)";
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, pingluninfo.getDiscussUserid());
stmt.setString(2, pingluninfo.getMsg());
stmt.setString(3, sdf.format(pingluninfo.getMsgtime()));
stmt.setInt(4, pingluninfo.getRbid());
rows = stmt.executeUpdate();
if (rows <= 0) {
return -1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll(conn, null, stmt);
}
return rows;
}
/**
* 查询
* **/
public List<Object> selPingLun(int Rbid, int pageSize,int pageIndex) {
PreparedStatement stmt = null;
ResultSet rs = null;
Connection conn = connection();
List<Object> pList = new ArrayList<Object>();
String sql =" select Msgid,discussUserid,Msg,Msgtime,Rbid from (select rownum as r,t.*from" +
"(select pingluninfo.*from pingluninfo " +
"where Rbid=? order by Msgtime desc) t" +
" where rownum <="+pageSize*pageIndex+") where r>"+pageSize*(pageIndex-1)+"";
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, Rbid);
rs = stmt.executeQuery();
while (rs.next()) {
pingluninfo pingluninfo = new pingluninfo();
//List<Bifm> bList= new ArrayList<Bifm>();
Bifm bifm =new Bifm();
pingluninfo.setMsgid(rs.getInt("Msgid"));
pingluninfo.setDiscussUserid(rs.getInt("discussUserid"));
pingluninfo.setMsg(rs.getString("Msg"));
pingluninfo.setMsgtime(rs.getDate("Msgtime"));
pingluninfo.setRbid(rs.getInt("Rbid"));
String username=username(pingluninfo.getDiscussUserid());
bifm.setUsername("<a href=''>"+username+"</a>");
pingluninfo.setBifminfo(bifm);
pList.add(pingluninfo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll(conn, rs, stmt);
}
return pList;
}
/**
* 修改
* **/
public int delFans(int userid, int fansuserid) {
PreparedStatement stmt = null;
Connection conn = connection();
int rows = 0;
String sql = "update receiveinfo set fansUserid=? where userid=? and fansuserid=?)";
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, null);
stmt.setInt(2, userid);
stmt.setInt(3, fansuserid);
rows = stmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
closeAll(conn, null, stmt);
}
return rows;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询