永发信息网

JAVA servlet中怎么根据JSP页面传来的ID,用hql语句查询主键ID里的信息

答案:3  悬赏:20  手机版
解决时间 2021-02-06 04:41
JAVA servlet中怎么根据JSP页面传来的ID,用hql语句查询主键ID里的信息
最佳答案
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取请求参数
int id = Integer.parseInt(request.getParameter("id"));
//调用dao层将这个id的学生找到
StudentDao sd = new StudentDao();
Student s = sd.findById(id);
//将学生对象保存到request范围
request.setAttribute("s", s);
//使用请求转发,让修改页面展示将要被修改的学生信息
request.getRequestDispatcher("update.jsp").forward(request, response);
out.flush();
out.close();
这是servlet里面的内容
public Student findById(int id){
Student s = null;
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
String sql = "select * from student where stuid=?";
//获取连接
conn = BaseDao.getConn();
try {
pstm = conn.prepareStatement(sql);
pstm.setInt(1, id);
//执行查询
rs = pstm.executeQuery();
if(rs.next()){
int stuId = rs.getInt("stuid");
String stuName = rs.getString("stuname");
String stuSex = rs.getString("stusex");
int stuAge = rs.getInt("stuage");
String stuBid = rs.getString("stubid");
//先将数据封装到Student对象中
s = new Student(stuId, stuName, stuSex, stuAge, stuBid);
//将对象放入集合
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
BaseDao.closeAll(conn, pstm, rs);
}
return s;
}
//这是写在Dao里面的内容
//这个是BaseDao   加载驱动 获取链接的
public class BaseDao{
//加载驱动
static{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConn(){
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "130130");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭所有资源
public static void closeAll(Connection conn,Statement st,ResultSet rs){
try {
if(null!=rs){
rs.close();
}
if(null!=st){
st.close();
}
if(null!=conn){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
全部回答
request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //获取请求参数 int id = Integer.parseInt(request.getParameter("id")); //调用dao层将这个id的学生找到 StudentDao sd = new StudentDao(); Student s = sd.findById(id); //将学生对象保存到request范围 request.setAttribute("s", s); //使用请求转发,让修改页面展示将要被修改的学生信息 request.getRequestDispatcher("update.jsp").forward(request, response); out.flush(); out.close(); 这是servlet里面的内容 public Student findById(int id){ Student s = null; Connection conn = null; PreparedStatement pstm = null; ResultSet rs = null; String sql = "select * from student where stuid=?"; //获取连接 conn = BaseDao.getConn(); try { pstm = conn.prepareStatement(sql); pstm.setInt(1, id); //执行查询 rs = pstm.executeQuery(); if(rs.next()){ int stuId = rs.getInt("stuid"); String stuName = rs.getString("stuname"); String stuSex = rs.getString("stusex"); int stuAge = rs.getInt("stuage"); String stuBid = rs.getString("stubid"); //先将数据封装到Student对象中 s = new Student(stuId, stuName, stuSex, stuAge, stuBid); //将对象放入集合 } } catch (SQLException e) { e.printStackTrace(); }finally{ BaseDao.closeAll(conn, pstm, rs); } return s; } //这是写在Dao里面的内容 //这个是BaseDao 加载驱动 获取链接的 public class BaseDao{ //加载驱动 static{ try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //获取连接 public static Connection getConn(){ Connection conn = null; try { conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "130130"); } catch (SQLException e) { e.printStackTrace(); } return conn; } //关闭所有资源 public static void closeAll(Connection conn,Statement st,ResultSet rs){ try { if(null!=rs){ rs.close(); } if(null!=st){ st.close(); } if(null!=conn){ conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } 不知道楼主有没有学过java web 这个是按分层写的
from userinfo u where date bewteen ? and ? 求某月的第一天到最后一天或同一周的查询结果。 两个问号是区间的时期 我相信你都知道
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
缘通汽车租赁怎么去啊,我要去那办事
乐乐超市在什么地方啊,我要过去处理事情
彐x0是什么意思
美版翻版午夜凶铃 喜剧版
杨棕伟《想对你》歌曲的歌词
win8笔记本电脑怎么设置插入鼠标就自动关闭触
定倾的意思是什么啊?知道的请说下!
一个没有正确人生观的人,很难形成科学的世界
如何看待华中科技大学软件学院2017考研复试分
恒通汽车租赁地址在什么地方,想过去办事
科颜氏的产品16岁适合用吗?
NBA2K9的士气怎么提高
佛教是如何看待生死问题的
深圳龙华这边这个吉顺达驾校怎么样,能详细告
恒远汽车租赁(新华街)怎么去啊,我要去那办事
推荐资讯
农村淘宝大仁和社区服务站这个地址在什么地方
临床行下颌下区手术时,切口应在A.下颌下缘下
影色年华照相冲印地址好找么,我有些事要过去
护民村地址好找么,我有些事要过去
欢乐谷KTV量贩式歌城(东坝店)地址好找么,我
龙臧的意思是什么啊?知道的请说下!
停车场(华厦天宇南)地址在什么地方,我要处理
闻达英语教育地址好找么,我有些事要过去
集成灶维修价格高吗?
停车场(虹南大道)地址有知道的么?有点事想过
战舰少女r 天龙改需要练4条吗
自私的意思是什么啊?知道的请说下!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?