import com.alibaba.druid.pool.DruidDataSourceFactory;
时间: 2023-12-16 13:26:00 浏览: 163
`import com.alibaba.druid.pool.DruidDataSourceFactory;`是Java中导入Druid连接池的工厂类。Druid连接池是阿里巴巴开源的一个高效、可靠的数据库连接池。通过使用Druid连接池,可以大大提高数据库连接的效率和稳定性。以下是一个使用Druid连接池获取数据库连接的例子:
```java
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class Test {
public static void main(String[] args) throws SQLException, IOException {
// 加载配置文件
Properties properties = new Properties();
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(inputStream);
// 获取数据源
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
// 获取连接
Connection connection = dataSource.getConnection();
// 执行查询
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM user");
// 处理结果集
while (resultSet.next()) {
System.out.println(resultSet.getString("name"));
}
// 关闭连接
resultSet.close();
statement.close(); connection.close();
}
}
```
阅读全文