JSPservlet删除,和数据库交互

2025-05-07 03:03:32

1、import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class DeleteEmpServlet extends HttpServlet{ @Override

JSPservlet删除,和数据库交互

3、 PreparedStatement pstm=null; int id=Integer.parseInt(request.getParameter("id")); try {数据库的链接,DBUtil是之前建立的链接数据库的类,这里直接使用con=DBUtil.getConnection();使用PreparedStatement可以预编译sql语言,减少频繁的访问数据库,使性能降低

JSPservlet删除,和数据库交互

5、public class DBUt坡纠课柩il { private static String driver; privat髫潋啜缅e static String url; private static String username; private static String password; private static String initialSize; private static String maxActive; private static String maxIdle; private static String minIdle; private static String maxWait; private static BasicDataSource dataSource=null; private static Properties db=null; public static void init(){ db=new Properties(); try { db.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties")); driver=db.getProperty("driver"); url=db.getProperty("url"); username=db.getProperty("username"); password=db.getProperty("password"); initialSize=db.getProperty("initialSize"); maxActive=db.getProperty("maxActive"); maxIdle=db.getProperty("maxIdle"); minIdle=db.getProperty("minIdle"); maxActive=db.getProperty("maxWait"); //通过DataSource加载相关参数值 dataSource=new BasicDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); if (initialSize!=null) {dataSource.setInitialSize(Integer.parseInt(initialSize));} if (maxActive!=null) {dataSource.setMaxActive(Integer.parseInt(maxActive));} if (maxIdle!=null) {dataSource.setMaxIdle(Integer.parseInt(maxIdle));} if (minIdle!=null) {dataSource.setMinIdle(Integer.parseInt(minIdle));} if (maxWait!=null) {dataSource.setMaxWait(Integer.parseInt(maxWait));} } catch (IOException e) {System.out.println("读取文件失败");throw new RuntimeException(e);}}

JSPservlet删除,和数据库交互
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢