Java Web 开发:使用 PreparedStatement 执行 SQL 语句
这段代码演示了如何在 Java Web Servlet 中使用 PreparedStatement 执行 SQL 语句,实现了添加、删除、修改机器人信息的功能。以下是代码的主要步骤:
-
创建 PreparedStatement 对象
使用
getConnection()方法从DBConnected对象获取数据库连接,然后调用prepareStatement()方法来准备 SQL 语句。PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql);其中,
sql是要执行的 SQL 语句。 -
设置参数值
使用
set方法设置 SQL 语句中的参数值。例如,setString()方法用于设置字符串类型的参数值。pstmt.setString(1, robot.getNAME()); pstmt.setString(2, robot.getweight()); pstmt.setString(3, robot.getsize());根据参数类型,可以使用
setInt()、setDouble()等方法来设置其他类型的参数值。 -
执行 SQL 语句
调用
executeUpdate()方法执行 SQL 语句,并将结果保存在一个变量中。int rs = pstmt.executeUpdate();executeUpdate()方法用于执行INSERT、UPDATE、DELETE等类型的 SQL 语句,并返回受影响的行数。 -
关闭资源
关闭
PreparedStatement对象和数据库连接,释放资源。pstmt.close(); dbc.close();使用 PreparedStatement 执行 SQL 语句可以有效地防止 SQL 注入攻击,并提高代码的可读性和可维护性。
代码示例:
@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() ;
}
}
}
}
这段代码展示了如何使用 PreparedStatement 执行三种不同的 SQL 语句:插入、删除和更新数据。代码中使用 try-catch-finally 块来处理异常,并确保在操作完成后关闭资源。
希望这些信息能帮助您更好地理解如何使用 PreparedStatement 在 Java Web Servlet 中执行 SQL 语句。
原文地址: https://www.cveoy.top/t/topic/pcYg 著作权归作者所有。请勿转载和采集!