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

2024-11-25 21:27:41
推荐回答(2个)
回答1:

这是我以前写的代码,希望对你有所帮助,这是最基础的,锋颂扰以后学习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 selPingLun(int Rbid, int pageSize,int pageIndex) {
PreparedStatement stmt = null;
ResultSet rs = null;
Connection conn = connection();
List pList = new ArrayList();
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 bList= new ArrayList();
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(""+username+"");
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;
}

回答2:

网上有好多的呀 ,你可以去找找 ,很多的java书上也有的