Java Servlet Robot 数据操作示例
是的,HttpServletRequest 的 getParameter 方法是 Java Servlet API 中的方法,用于从 HTTP 请求中获取参数的值。
@WebServlet({"/welcome"}) public class Robot extends HttpServlet { protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String action = req.getParameter('action');
if ('addRobot'.equals(action)) {
RobotInfo robot =new RobotInfo();
robot.setNAME(req.getParameter('robotname'));
robot.setweight(req.getParameter('robotweight'));
robot.setsize(req.getParameter('robotsize'));
PreparedStatement pstmt = null ;
DBConnected dbc = null;
String sql ='INSERT INTO `javawebdb`.`robot` (`ID`, `NAME`, `WEIGHT`, `SIZE`, `IMAGE`) VALUES (NULL,?,?,?,NULL)';
try{
dbc = new DBConnected() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,robot.getNAME());
pstmt.setString(2,robot.getweight());
pstmt.setString(3,robot.getsize());
int rs = pstmt.executeUpdate();
pstmt.close() ;
res.sendRedirect("./welcome.jsp");
}catch (SQLException e){
System.out.println(e.getMessage());
}finally{
dbc.close() ;
}
} else if ('deleteRobot'.equals(action)) {
RobotInfo robot =new RobotInfo();
String ID =req.getParameter('robotID');
PreparedStatement pstmt = null ;
DBConnected dbc = null;
String sql ='DELETE FROM Robot WHERE id = ?';
; try{
dbc = new DBConnected() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,ID);
int rs = pstmt.executeUpdate();
pstmt.close() ;
res.sendRedirect("./welcome.jsp");
}catch (SQLException e){
System.out.println(e.getMessage());
}finally{
dbc.close() ;
}
} else if ('changeRobot'.equals(action)) {
RobotInfo robot =new RobotInfo();
String ID =req.getParameter('robotID1');
robot.setNAME(req.getParameter('robotname1'));
robot.setweight(req.getParameter('robotweight1'));
robot.setsize(req.getParameter('robotsize1'));
PreparedStatement pstmt = null ;
DBConnected dbc = null;
String sql ='UPDATE Robot SET NAME = ?, WEIGHT = ?, SIZE=? WHERE id =?';
; try{
dbc = new DBConnected() ;
pstmt = dbc.getConnection().prepareStatement(sql) ;
pstmt.setString(1,robot.getNAME());
pstmt.setString(2,robot.getweight());
pstmt.setString(3,robot.getsize());
pstmt.setString(4, ID);
int rs = pstmt.executeUpdate();
pstmt.close() ;
res.sendRedirect("./welcome.jsp");
}catch (SQLException e){
System.out.println(e.getMessage());
}finally{
dbc.close() ;
}
}
}
}这段代码中的增加操作中的HttpServletRequest的getParameter方法是java库中默认的方法吗
原文地址: https://www.cveoy.top/t/topic/pcPa 著作权归作者所有。请勿转载和采集!