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

 我来答
time飞啊飞的
推荐于2017-05-20 · 超过32用户采纳过TA的回答
知道答主
回答量:79
采纳率:0%
帮助的人:62.1万
展开全部
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
这个是按分层写的
小孤阿9
2018-04-12 · TA获得超过2.8万个赞
知道小有建树答主
回答量:90
采纳率:100%
帮助的人:1.2万
展开全部

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();

}

}

}

本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式