这是我以前写的代码,希望对你有所帮助,这是最基础的,以后学习hibernate框架的时候,代码就更加简单了,你也可以去学习下
/
数据库
/
public class BaseDao {
public Connection connection() {
String forname = "oraclejdbcdriverOracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:uework";
String user = "epwork";
String pwd = "passowrd";
Connection conn = null;
try {
ClassforName(forname);
} catch (Exception e) {
Systemerrprint(egetMessage());
}
try {
conn = DriverManagergetConnection(url, user, pwd);
} catch (SQLException e) {
Systemerrprint(egetMessage());
}
return conn;
}
/
这里是关闭
/
public void closeAll(Connection conn, ResultSet rs, PreparedStatement stmt) {
if (rs != null) {
try {
rsclose();
} catch (SQLException e) {
Systemerrprint(egetMessage());
}
}
if (stmt != null) {
try {
stmtclose();
} catch (SQLException e) {
Systemerrprint(egetMessage());
}
}
if (conn != null) {
try {
connclose();
} catch (SQLException e) {
Systemerrprint(egetMessage());
}
}
}
}
/
添加
/
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_nnextval,,,to_date(,'yyyy-mm-dd hh24:mi:ss'),)";
try {
stmt = connprepareStatement(sql);
stmtsetInt(1, pingluninfogetDiscussUserid());
stmtsetString(2, pingluninfogetMsg());
stmtsetString(3, sdfformat(pingluninfogetMsgtime()));
stmtsetInt(4, pingluninfogetRbid());
rows = stmtexecuteUpdate();
if (rows <= 0) {
return -1;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
} 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,tfrom" +
"(select pingluninfofrom pingluninfo " +
"where Rbid= order by Msgtime desc) t" +
" where rownum <="+pageSizepageIndex+") where r>"+pageSize(pageIndex-1)+"";
try {
stmt = connprepareStatement(sql);
stmtsetInt(1, Rbid);
rs = stmtexecuteQuery();
while (rsnext()) {
pingluninfo pingluninfo = new pingluninfo();
//List<Bifm> bList= new ArrayList<Bifm>();
Bifm bifm =new Bifm();
pingluninfosetMsgid(rsgetInt("Msgid"));
pingluninfosetDiscussUserid(rsgetInt("discussUserid"));
pingluninfosetMsg(rsgetString("Msg"));
pingluninfosetMsgtime(rsgetDate("Msgtime"));
pingluninfosetRbid(rsgetInt("Rbid"));
String username=username(pingluninfogetDiscussUserid());
bifmsetUsername("<a href=''>"+username+"</a>");
pingluninfosetBifminfo(bifm);
pListadd(pingluninfo);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
} 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 = connprepareStatement(sql);
stmtsetString(1, null);
stmtsetInt(2, userid);
stmtsetInt(3, fansuserid);
rows = stmtexecuteUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
} finally {
closeAll(conn, null, stmt);
}
return rows;
}
create table t_class
(
cno int primary key, --教室的主键
)
create table t_schedule
(
sno int primary key,
scno int , --对应教室table中的cno
foreign key(scno) references t_class(cno) --在课程表中设置教室的主键为它的外键
)
这样就把课程表和教室关联起来了
用 scriptlet 就可以了
<%
这里是你的 CRUD(增删改查) 的代码
%>
建议你把这些 CRUD 的 *** 作写到 Servlet 内 会比较 方便`
额,这个说出来就太长了。而且还需要代码。
1,在JSP页面上点击添加后,进入action,action进入逻辑处理,然后进入dao,由hibernate进行持久化数据库,在数据库添加一条数据后,返回,最后逻辑处理后,把数据显示在页面上。你说不懂数据库 *** 作部分,还要用hibernate,那么你必须把hibernate调好了才好调用。调好了就调用简单了。你看下别人的项目吧。
下载个连接数据库的驱动
加载驱动、连接数据库、增删改查方法
调用方法、实现功能
再简单点百度jsp 连接数据库,代码实例一大堆,大部分还连ajax也一起教你了
以上就是关于求jsp+javabean实现增删改查等 *** 作全部的内容,包括:求jsp+javabean实现增删改查等 *** 作、如何使用JSP进行课程表的增删改查关键是Mysql数据库里面表的关系如何建立、如何在jsp页面的迭代器下,根据id对一条数据进行增删查改等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)