求jsp+javabean实现增删改查等操作 5

 我来答
Win8社区
2012-10-14 · TA获得超过412个赞
知道小有建树答主
回答量:151
采纳率:0%
帮助的人:141万
展开全部
这是我以前写的代码,希望对你有所帮助,这是最基础的,以后学习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;
}
逍遥儍
2012-10-11
知道答主
回答量:62
采纳率:0%
帮助的人:9万
展开全部
网上有好多的呀 ,你可以去找找 ,很多的java书上也有的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式