Lacznie sie z baza danych, Cannot open connection SQLException,

0

Probuje polaczyc sie z baza danych:
klasa z main:

public class HibernateTest {
	
	
	public static void main(String[] args){
		
		UserDetails user = new UserDetails();
		user.setUserId(1);
		user.setUserName("Andrzej First User");
		
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		
		session.beginTransaction();
		session.save(user);
		session.getTransaction().commit();
		

	}
}

hibernate.cfg.xml :

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgres://localhost:5433/hibernatedb</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">password</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="org.mateusz.java.dto.UserDetails"/>

    </session-factory>

</hibernate-configuration>

zrzut PackageExplorer'a i pgAdmin
user image

czyli .jar dodany, scieżka w xml jest poprawna, dalej nie bangla, w wiekszosci przypadkow takie byly problemy tego bledu. Macie jakies sugestie co tutaj moze byc zle?

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
	at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
	at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
	at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
	at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
	at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
	at org.mateusz.hibernate.HibernateTest.main(HibernateTest.java:22)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:postgres://localhost:5433/hibernatedb
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
	at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
	... 5 more
0

No tu masz wskazówkę:

Caused by: java.sql.SQLException: No suitable driver found for jdbc//localhost:5433/hibernatedb

Wychodzi na to, że nie masz driver-a do PostgreSQL w classpath.

0

tak, tez to widzialem(moze zle to rozumiem) ale ten driver to Driver.class z tego .jar'a dodanego do classpath. Jesli tak to ten driver jest dodany, uzywam 1.8 javy, driver jest do javy 1.8 wiec tutaj wszystko sie zgadza.

zeby sprawdzic czy widzi tego jara i wszystko dziala, zmienialem wartosc tego property i wtedy wyskakuje inny error, wiec wychodzi na to ze jest dodany w sposob prawidlowy?

0

Ja już w javie nie piszę od lat, ale z tego co pamiętam do CLASSPATH wrzucasz całego jar-a.
Jeżeli chcesz przetestować to w 1 linijce programu: Class.forName("org.postgresql.Driver");

To samo wykonuje prawdopodobnie Hibernate.

1 użytkowników online, w tym zalogowanych: 0, gości: 1