package demo06; import java.io.InputStream; import java.sql.Connection; import java.util.Properties; import javax.sql.DataSource; import org.junit.jupiter.api.Test; import com.alibaba.druid.pool.DruidDataSourceFactory; public class DruidTest { @Test public void test() throws Exception { Properties pros = new Properties(); InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties"); //加载配置文件 pros.load(is); DataSource source = DruidDataSourceFactory.createDataSource(pros); Connection conn = source.getConnection(); System.out.println(conn); } }
配置文件druid.properties:
url=jdbc:mysql:///girls username=root password=root driverClassName=com.mysql.jdbc.Driver
也可以将其写在JDBCUtils工具类中,作为常规工具使用