Answer the question
In order to leave comments, you need to log in
Why is the database not connected?
Hello,
in a web application (java) on the page, the list is filled from the database. DB-in postgres, server -glassfish
Created jdbc connection pool "CP_ISS" (resource-"jdbc/ISS") - ping works. It seems that everything was registered as in the documentation, but there is no connection with the database. Debugging doesn't work either, throws an error, I can't find what's wrong (org.netbeans.api.debugger.jpda.DebuggerStartException: Connection refused: connect). I think both problems are related. Maybe I'm writing something wrong all the time :( Please help
JDBC.java
import......
public class JDBC {
private static final Logger log=Logger.getLogger(JDBC.class.getName());
private DataSource dataSource;
public JDBC(){
}
public void init(String dataSourceName){
try{
InitialContext initContext = new InitialContext();
dataSource=(DataSource) initContext.lookup(dataSourceName);
} catch (NamingException e){
log.log(Level.SEVERE, "JNDIException: {0}", e.getMessage());
}
}
public Connection getConnection() throws SQLException {
if (dataSource == null) {
throw new SQLException("DataSource is null.");
}
return dataSource.getConnection();
}
}
public class DataAccessFactory {
private static final DataAccessFactory instance = new DataAccessFactory();
private JDBC jdbcUtil;
private DataAccessFactory() {
}
public static DataAccessFactory getInstance() {
return instance;
}
private JDBC prepareJDBCUtils() {
if (jdbcUtil == null) {
jdbcUtil = new JDBC();
jdbcUtil.init("jdbc/ISS");
}
return jdbcUtil;
}
public static synchronized JDBC getJDBCUtils() {
return getInstance().prepareJDBCUtils();
}
}
import....
public class ConnectToDB {
private static final JDBC jdbc = DataAccessFactory.getJDBCUtils();
public static List<String> getData(String param1, String param2) throws Exception {
List<String> data = new ArrayList<String>();
Connection connection = null;
PreparedStatement ps = null;
try {
Class.forName("org.postgresql.Driver");
System.out.println("Драйвер подключен");
connection = jdbc.getConnection();
System.out.println("Соединение установлено");
ps = connection.prepareStatement("SELECT "+ param1 +" FROM "+ param2);
ResultSet result = ps.executeQuery();
ResultSetMetaData md = result.getMetaData();
int cnt = md.getColumnCount(); // получаем кол-во колонок (1..cnt)
while (result.next()) {
for (int i = 1; i <= cnt; i++) {
String name = md.getColumnName(i); // получим имя колонки
data.add(result.getString(i)); // получаем значение
}
}
} catch (Exception ex) {
//выводим наиболее значимые сообщения
Logger.getLogger(CheckboxView.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException ex) {
Logger.getLogger(CheckboxView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return data;
}
}
import.....
public class CheckboxView {
private String[] selectedCities;
private ConnectToDB cities;
private String[] selectedModesOfTransport;
private List<String> modesOfTransport;
private String[] selectedRoute;
private List<String> route;
private String[] selectedOffense;
private List<String> offense;
private String cart;
private List<String> tableCity;
public void init() throws Exception {
cities = new ConnectToDB();
modesOfTransport = new ArrayList<String>();
route = new ArrayList<String>();
offense = new ArrayList<String>();
this.cart = "http://www.openstreetmap.org/export/embed.html?bbox=54.12689208984376%2C53.075877572693564%2C57.19207763671876%2C54.06583577161281&layer=mapnik";
cities.getData("*", "city");
}
public String cart() {
this.cart = "http://www.openstreetmap.org/#map=9/54.0279/53.4677";
return cart;
}
public String[] getSelectedCities() {
return selectedCities;
}
public void setSelectedCities(String[] selectedCities) {
this.selectedCities = selectedCities;
}
public ConnectToDB getCities() {
return cities;
}
[
.........
]
public String getCart() {
return cart;
}
public void setCart(String cart) {
this.cart = cart;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view contentType="text/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<link rel="stylesheet" type="text/css" href="style.css"></link>
</h:head>
<h:body>
<div class="block">
<!--Заголовок-->
<div class="header" align="center">Система информационного обеспечения органов государственной власти </div>
<!--Меню навигации-->
<ul class="css-menu-3">
<li><a href="index.xhtml" class="selected">Текущая обстановка</a></li>
<li><a href="directory.xhtml">Справочники</a></li>
<li><a href="reports.xhtml">Отчеты</a></li>
<li><a href="offense.xhtml">Нарушения</a></li>
<li><a href="#">Еще раздел</a></li>
</ul>
<div class="content">
<!--Карта-->
<h:form>
<div id="cart" >
<iframe src="#{checkboxView.cart}"></iframe>
</div>
</h:form>
<!--Параметры-->
<div class="left">
<p:accordionPanel>
<p:tab title="Районы и города">
<p:selectManyCheckbox id="grid2" value="#{checkboxView.selectedCities}" layout="grid" columns="2" style="text-align: left">
<f:selectItems value="#{checkboxView.cities}"/>
</p:selectManyCheckbox>
</p:tab>
[
.........
]
</p:accordionPanel>
<p:commandButton id="clean" value="Очистить" > </p:commandButton>
</div>
</div>
<div class="clear"></div>
<!--Подвал - Footer-->
<div class="footer">
<p>Главная | <a href="#">О нас</a> | <a href="#">Техническая поддержка</a></p>
</div>
</div>
</h:body>
</f:view>
</html>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/ISS</res-ref-name>
<res-type>javax.sql.ConnectionPoolDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Answer the question
In order to leave comments, you need to log in
In general, the base is opened through the "elephant", i.e. pgadmin?
If it opens, you need to deal with the initialization of the connection and, in general, with the connection settings, but if not, then check the ports on the database server and pg_hba.conf to see if there is permission to connect to the database from your network.
I remember Spring giving me a huge sheet of bugs that I had to look through thoughtfully to see where the propagation came from.
I have already double-checked everything, the database opens through the elephant.
When loading the page and clicking on the panel, it gives out such an error
which resource it cannot find, I can’t understand - which one was registered in the pool or in the database itself is a problem.
Sorry for stupid questions :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question