JAVA servlet中怎么根据JSP页面传来的ID,用hql语句查询主键ID里的信息
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
这个是按分层写的
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();
}
}
}