💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
~~~ package cn.itcast.datasource.druid; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.util.Properties; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com */ public class DruidDemo1 { public static void main(String[] args) throws Exception { //1.导入jar包 //2.定义配置文件 //3.加载配置文件 Properties pro = new Properties(); InputStream is = DruidDemo1.class.getClassLoader().getResourceAsStream("druid.properties"); pro.load(is); //4.获取连接池对象 DataSource ds = DruidDataSourceFactory.createDataSource(pro); //5.获取连接 Connection conn = ds.getConnection(); System.out.println(conn); } } ~~~ ***** ~~~ package cn.itcast.datasource.druid; /** * @author: hcf * @qq: 46914685 * @email: 46914685@qq.com */ import cn.itcast.datasource.utils.DruidUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; /** * 使用工具类 DruidUtils */ public class DruidDemo2 { public static void main(String[] args) { /** * 完成添加操作:给account表添加一条记录 * */ Connection conn = null; PreparedStatement pstmt = null; try { //1.获取连接 conn = DruidUtils.getConnection(); //2.定义sql String sql = "insert into account(name,balance) values(?,?)"; //3.获取执行sql对象 pstmt = conn.prepareStatement(sql); //4.给?赋值 pstmt.setString(1, "乔峰"); pstmt.setDouble(2, 1856.56); //5.执行sql int count = pstmt.executeUpdate(); System.out.println(count); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { //释放资源 DruidUtils.close(pstmt, conn); } } } ~~~