1.下载druid-1.1.9jar放在lib目录中和mysql-connector-java-8.0.26.jar
2.创建文件夹druid.properties
# ?????url?? url=jdbc:mysql://localhost:3306/test3
url=jdbc:mysql:/
# ???
username=root
#??
password=123456
3.连接
public class DataSource01Test {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
String sql = " select * from user ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String password = resultSet.getString("password");
System.out.println("id:" + id + " name :" + name);
}
resultSet.close();
preparedStatement.close();
connection.close();
}
}