java-使用DriverManager进行数据库连接
BeanHandler<T>是返回一个实体对象,BeanListHandler<T>是返回一个list实体集合,MapHandler是返回不确定的单个实体对象,MapListHandler是返回不确定的list实体集合。
PreparedStatement statement = connection.prepareStatement(sqlBuilder.toString());ResultSet resultSet = statement.executeQuery();
BeanListHandler<LocalPerson> listHandler = new BeanListHandler<>(LocalPerson.class);
List<LocalPerson> localPersonList = listHandler.handle(resultSet);
/*** 连接数据库
* @param ip
* @param port
* @param name
* @param user
* @param password
* @return
*/
public Connection connectionDataBase(String ip,String port,String name,String user,String password){
Connection connection = null;
try {
String url = "jdbc:postgresql://" + ip + ":" + port + "/" + name;
connection = DriverManager.getConnection(url,user,password);
logger.info("数据库连接成功,url: " + url);
} catch (Exception e) {
logger.error("connectionDataBase error: ",e);
}
return connection;
}
/**
* 关闭数据库
* @param connection
*/
public void closeDataBase(Connection connection){
try {
if (connection != null){
connection.close();
logger.info("数据库关闭连接");
}
} catch (SQLException e) {
logger.error("closeDataBase error: ",e);
}
}
以上是 java-使用DriverManager进行数据库连接 的全部内容, 来源链接: utcz.com/z/390198.html