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); } }