java.sql.SQLInvalidAuthorizationSpecException: invalid authorization specification - not found - java

I am trying to run a basic java desktop application using hibernate with an hsqldb in a standalone mode
when I running my app I got this error
java.sql.SQLInvalidAuthorizationSpecException: invalid authorization specification - not found: SA
Any help please
NB: I use netbeans IDE
I try this example hibernate annotation
and this is my hibernate conf file:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.url">
jdbc:hsqldb:C:\Users\Desktop\testdb</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>

Give username and password you will not get that error.

Related

Getting this Error : -" java.sql.SQLException: No suitable driver found for". When I use Hibernate Config for reverse eng. my DB

I'm trying to reverse Engeneer my simple MYSQL DB using Hibernate Configuration with eclipse IDE.But getting below error.
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/client1_db
I went through similar issues. This is a Marven Web Project and I can see the MYSQL_connector_java_5.1.34.jar is there under Marven Dependencies library. Some had suggested "No suitable driver found" means the drivers are loading but an issue with the DB connection. I have tried to connect to the localhost and the DB was created using MySQL workbench and it is accessible. I even tried using myphp admin and the DB is accessible.
hibernate.cfg.xml file
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bookshop</property>
<property name="connection.username">root</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.MySQLDialect</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 the existing tables and create new one -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Then I tried using different server port 3307 as below. Still the same error.
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3307/client1_db</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
The full Error Message
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/client1_db
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getConnection(AbstractMetaDataDialect.java:121)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getMetaData(AbstractMetaDataDialect.java:60)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.caseForSearch(AbstractMetaDataDialect.java:163)
at org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect.getTables(JDBCMetaDataDialect.java:22)
at org.hibernate.cfg.reveng.JDBCReader.processTables(JDBCReader.java:476)
at org.hibernate.cfg.reveng.JDBCReader.readDatabaseSchema(JDBCReader.java:74)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.tools.hibernate.runtime.common.Util.invokeMethod(Util.java:43)
at org.jboss.tools.hibernate.runtime.common.AbstractJDBCReaderFacade.readDatabaseSchema(AbstractJDBCReaderFacade.java:44)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$2.execute(LazyDatabaseSchemaWorkbenchAdapter.java:132)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:107)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.readDatabaseSchema(LazyDatabaseSchemaWorkbenchAdapter.java:124)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:64)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:104)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:219)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Hibernate Configuration screenshot
Package Explore
Please visit the below page :
https://javarevisited.blogspot.com/2016/09/javasqlsqlexception-no-suitable-driver-mysql-jdbc-localhost.html
Instead of doing the reverse engineering directly I first added the Database to the Eclipse providing JDBC driver path. After testing the connection was successful then performed the reverse engineering and it was successful.
Steps >
01. Go to Data Source Explore
Add the DB connection
Make sure you map the JDBC drivers
04.Test at the end. Make sure your server is running..

Hibernate Authentication Error with SQL Server

I am having a difficult time trying to set a connection with hibernate to a SQL Server. I should create a connection to the following data source DataSource=server,port. It seem strange that the port must be specify with a comma instead of :
When I connect to it through Visual Studio 2012 this is how it looks:
Data Source=server,17001;Initial Catalog=database;User ID=username
In order to connect it I create the following hibernate configuration file:
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://server.fqdn</property>
<property name="hibernate.connection.port">port</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.databasename">database</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<property name="connection.pool_size">1000</property>
</session-factory>
</hibernate-configuration>
But I get the following Exception when the connection is attempted:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'username'. ClientConnectionId:c619c1dc-2c6c-4c6e-bc81-7d3619ee9ff1
I know for sure that the user and password are correct as I am using them to connect to the database through Visual Studio 2012 and they works fine.
Any idea of how should I face it?
I forgot to mark this as resolved. The issue was that I was specifying the port as a separate property:
<property name="hibernate.connection.port">port</property>.
When I add it to the connection string parameter
<property name="hibernate.connection.url">jdbc:sqlserver://server.fqdn:port</property>
it works

Existing code throws this error [Hibernate + c3p0 + Oracle11g]: a resourcepool could not acquire a resource from its primary factory or source

when i am trying to connect to a test db which settings as per shown, i am faced all the errors as per mentioned.
most of the config is taken from working production environment but i am not able to connect to my test db.
i have tried various methods and it just fails to run properly.
Im able to connect test db via other methods such as PLSQL developer or SQLPLUS.
The errors i am getting in a bunch are :
org.hibernate.exception.GenericJDBCException: Cannot open connection
....
....
....
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
....
....
....
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.j‌​ava:1422)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResour‌​cePool.java:606)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool‌​.java:526)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutAndMarkConnectionInUse‌​(C3P0PooledConnectionPool.java:756) at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0P‌​ooledConnectionPool.java:683)
this is my config file for hibernate :
<session-factory>
<property name="hibernate.connection.url">jdbc:oracle:thin:#ipaddress:1521:dbname</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
<property name="connection.autoReconnect">true</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">1</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">50000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.validate">false</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource etc>
</session-factory>
Can anyone help me on this issue?
It happen for many reason like
The database configuration details you have entered are incorrect or have changed, e.g. the database name or database username/password for your Confluence database.
1)The URL for the database is incorrect.
2)The database server is not running. If your database server is down, restart it and check the logs to see why it had failed or stopped.
3)The network is down (or there is a firewall in between confluence and the database server which is interfering) and the connection to the database cannot be established.
4)Your database password has expired.
5)Check your driver,if its present or not in your class-path.

Hibernate + embedded database - setup

I have created Java application using Hibernate with this configuration:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306 /bee</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.username">root</property>
<property name="connection.password"/>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
<mapping resource="DatabaseMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Everything works fine when Iam using jdbc:mysql://localhost..., but now I need to have the database embedded in my application. Which database should I use? I need to have all my data stored and load it after start the application, update data, save, delete. I use HQL query or SQL query.
What is the simplest way to make the database embedded? I don´t want to change my queries. It would be fine to change only hibernate configuration and set it to the embedded database, is it possible?
One of the advantages of ORM's like Hibernate is to shield you from DB differences. You can use any one of the below as an embedded DB solution. Just change the dialect, driver and URL in hibernate cfg file.
H2
SQLite
HSQLDB
Hava a look at using the H2 database in embedded mode
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">jdbc:h2:~/test</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<property name="hibernate.connection.charSet">UTF-8</property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.default_schema">PUBLIC</property>
<mapping resource="DatabaseMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
If you don't want to change your queries, consider mysql-mxj (embedded mysql) http://dev.mysql.com/doc/connector-mxj/en/connector-mxj.html
It is not under active development (thanks Oracle). But is prefectly usable and the connector is open source. It is trivial to embed different versions of mysql, but the latest mxj connector embeds 5.5.9

how to configure hibernate config file for sql server

Here is the config file for MySQL:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">zgy01</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="model.hbm.xml"/>
</session-factory>
</hibernate-configuration>
What to specify for SQL Server 2005? I did it like this:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">lal</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="show_sql">false</property>
<!-- Mapping files -->
<mapping resource="model.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My question more precisely is how to specify the database that I have to connect to?
In MySQL I used to do like this:
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
Properties that are database specific are:
hibernate.connection.driver_class: JDBC driver class
hibernate.connection.url: JDBC URL
hibernate.connection.username: database user
hibernate.connection.password: database password
hibernate.dialect: The class name of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database.
To change the database, you must:
Provide an appropriate JDBC driver for the database on the class path,
Change the JDBC properties (driver, url, user, password)
Change the Dialect used by Hibernate to talk to the database
There are two drivers to connect to SQL Server; the open source jTDS and the Microsoft one. The driver class and the JDBC URL depend on which one you use.
With the jTDS driver
The driver class name is net.sourceforge.jtds.jdbc.Driver.
The URL format for sqlserver is:
jdbc:jtds:sqlserver://<server>[:<port>][/<database>][;<property>=<value>[;...]]
So the Hibernate configuration would look like (note that you can skip the hibernate. prefix in the properties):
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://<server>[:<port>][/<database>]</property>
<property name="connection.username">sa</property>
<property name="connection.password">lal</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
...
</session-factory>
</hibernate-configuration>
With Microsoft SQL Server JDBC 3.0:
The driver class name is com.microsoft.sqlserver.jdbc.SQLServerDriver.
The URL format is:
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
So the Hibernate configuration would look like:
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://[serverName[\instanceName][:portNumber]];databaseName=<databaseName></property>
<property name="connection.username">sa</property>
<property name="connection.password">lal</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
...
</session-factory>
</hibernate-configuration>
References
Hibernate Core Reference Documentation
3.3. JDBC connections
3.4. Optional configuration properties
jTDS Documentation
Microsoft SQL Server JDBC Driver 3.0 Documentation
Microsoft SQL Server JDBC Driver 2.0
Support Matrix for Microsoft SQL Server JDBC Driver
The connection URL should look like this for SQL Server:
jdbc:sqlserver://serverName[\instanceName][:port][;databaseName=your_db_name]
Examples:
jdbc:sqlserver://localhost
jdbc:sqlserver://127.0.0.1\INGESQL:1433;databaseName=datatest
...
We also need to mention default schema for SQSERVER: dbo
<property name="hibernate.default_schema">dbo</property>
Tested with hibernate 4
Don't forget to enable tcp/ip connections in SQL SERVER Configuration tools
Finally this is for Hibernate 5 in Tomcat.
Compiled all the answers from the above and added my tips which works like a charm for Hibernate 5 and SQL Server 2014.
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.connection.url">
jdbc:sqlserver://localhost\ServerInstanceOrServerName:1433;databaseName=DATABASE_NAME
</property>
<property name="hibernate.default_schema">theSchemaNameUsuallydbo</property>
<property name="hibernate.connection.username">
YourUsername
</property>
<property name="hibernate.connection.password">
YourPasswordForMSSQL
</property>
Keep the jar files under web-inf lib incase you included jar and it is not able to identify .
It worked in my case where everything was ok but it was not able to load the driver class.

Categories