使用druid资源池访问GBase8c

1、导入druid包,然后进行加载

2、pom.xml中加载依赖 (使用哪种模块需要在导入对应依赖)

<dependency>

<groupId>org.postgresql</groupId>

<artifactId>postgresql</artifactId>

<scope>runtime</scope>

</dependency>

3、druid.properties:文件配置 

jdbcUrl = jdbc:postgresql://172.16.5.102:5432/postgres?currentSchema=jdbcuser

username = jdbcuser

password = GBase_123

initialSize = 5

maxActive = 20

minIdle = 5

maxWait = 3000

4、java链接样例:(通过ddl、dml、dql执行)

package com.example.demo_druid;

import com.alibaba.druid.pool.DruidDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.ResultSet;

public class DruidTest {
public static void main(String[] args) throws IOException, SQLException {
//1.创建连接池
 //1.加载配置文件
 Properties pro = new Properties();
 ResultSet rs=null;
 pro.load(DruidTest.class.getClassLoader().getResourceAsStream("druid.properties"));
 //2.创建数据源对象
 /*DataSource:是SUN公司声明的接口 。DruidDataSource:是阿里巴巴对应实现的类*/
 DataSource dataSource = new DruidDataSource();
 //3.设置属性
 DruidDataSource ds = (DruidDataSource)dataSource;
 ds.setUrl(pro.getProperty("jdbcUrl"));
 ds.setUsername(pro.getProperty("username"));
 ds.setPassword(pro.getProperty("password"));
 ds.setInitialSize(new Integer(pro.getProperty("initialSize")));
 ds.setMaxActive(new Integer(pro.getProperty("maxActive")));
 ds.setMinIdle(new Integer(pro.getProperty("minIdle")));
 ds.setMaxWait(new Long(pro.getProperty("maxWait")));
 //4.拿连接。直接通过数据源获取可用的连接
 Connection conn = ds.getConnection();
 System.out.println(conn);//检验是否能够成功拿到一个数据库连接池中的连接

 //2.编写sql语句
 String sql1 = "create table aaaa(a int,b varchar(20))"; 
 String sql = "insert into aaaa values (2,'druiftest')";
 String sql2 = "select * from v_userrole limit 10";
 String sql3 = "select * from users";

 //3.PreparedStatement
 PreparedStatement pst = conn.prepareStatement(sql1);
 //System.out.println(pst);
 //4.执行sql
 pst.executeUpdate();
 //pst.executeUpdate();
 //rs=pst.executeQuery();
 while (rs.next()){ //判断是否有数据
 //有数据便获取数据然后输出
 String userid=rs.getString("username");
 String useralias=rs.getString("password");
 String roleid=rs.getString("enabled");
 System.out.println(userid+"|"+" "+useralias+" |"+roleid);
 }
//5.直接关闭资源即可,
 conn.close();
 pst.close();
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Druid是阿里巴巴开源的一个数据库连接,它具有连接的基本功能,同时还提供了监控、防御SQL注入攻击、缓存等高级功能。下面是使用Druid连接进行JDBC操作的步骤: 1. 引入Druid的依赖 ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> ``` 2. 配置Druid连接 可以通过properties文件、xml文件或者代码配置Druid连接,这里以代码配置为例: ``` import com.alibaba.druid.pool.DruidDataSource; import java.sql.Connection; import java.sql.SQLException; public class DruidUtils { private static DruidDataSource dataSource = null; static { dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/test"); dataSource.setUsername("root"); dataSource.setPassword("123456"); } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } } ``` 3. 使用Druid连接获取数据库连接 ``` Connection conn = DruidUtils.getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE id = ?"); ps.setInt(1, 1); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getString("name")); } rs.close(); ps.close(); conn.close(); ``` 4. 关闭连接 使用完数据库连接后,需要关闭连接,释放资源。 ``` if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } ``` 以上就是使用Druid连接进行JDBC操作的基本步骤,可以有效提高数据库连接的使用效率和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值