怎么在监听器中用德鲁伊写一个数据库连接的功能此功能可以调用德鲁伊的配置文件druidproperties然后在Userdao中调取方法获取连接
在监听器中使用德鲁伊连接池并调用配置文件的步骤如下:
- 首先,在项目的classpath下创建一个名为
druid.properties的配置文件,并在其中配置数据库连接相关的属性,例如:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
username=root
password=123456
initialSize=5
maxActive=20
...
- 在项目中引入德鲁伊的依赖,例如使用Maven的话,可以在
pom.xml中添加以下依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.1</version>
</dependency>
- 创建一个监听器类,实现
ServletContextListener接口,并重写contextInitialized方法,在该方法中获取德鲁伊连接池对象,并将其保存到ServletContext中,供其他类使用。示例代码如下:
import com.alibaba.druid.pool.DruidDataSource;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class DruidListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
// 读取druid.properties配置文件
Properties properties = new Properties();
try {
properties.load(servletContext.getResourceAsStream("/WEB-INF/classes/druid.properties"));
} catch (IOException e) {
e.printStackTrace();
}
// 创建Druid数据源
DruidDataSource dataSource = new DruidDataSource();
dataSource.configFromPropety(properties);
// 将数据源保存到ServletContext中
servletContext.setAttribute("dataSource", dataSource);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
DruidDataSource dataSource = (DruidDataSource) servletContext.getAttribute("dataSource");
if (dataSource != null) {
dataSource.close();
}
}
}
- 在
UserDao类中,通过ServletContext获取德鲁伊数据源对象并获取数据库连接。示例代码如下:
import com.alibaba.druid.pool.DruidDataSource;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
public void getUser() {
ServletContext servletContext = getServletContext();
DruidDataSource dataSource = (DruidDataSource) servletContext.getAttribute("dataSource");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
String sql = "SELECT * FROM user";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
// 处理查询结果
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
注意事项:
druid.properties文件应放置在WEB-INF/classes目录下。- 监听器通过
ServletContext对象将数据源保存在ServletContext中,其他类需要获取数据源时,通过ServletContext对象获取
原文地址: https://www.cveoy.top/t/topic/iBJF 著作权归作者所有。请勿转载和采集!