Loading class `com.mysql.jdbc.Driver'. This is deprecated - java

In a maven-java-sql project using hibernate, when I try to run the project this error message appears. The MySQL server is running but the the maven-java project is not able to comunicate with the MySQL server. I am using the Apache NetBeans IDE and Workbench as the server tool. Here is my persistence class
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Persistence" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/technikon"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
the error message

The error message tells you that:
Caused by: java.sql.SQLSyntaxErrorException: Unknown database 'technikon'
So, you will need to connect to your RDBMS via Workbench (or CLI or whatever) and run
show databases;
See what databases you are finding in the list (beware db names like Technikon) and change your settings accordingly.

Related

Hibernate JPA - No Persistence provider for EntityManager error

I am trying to connect to sql server using JPA and hibernate with an Eclipse Maven project. I've tried moving the persistence.xml file to various locations but it doesn't seem to help.
My persistence.xml file is
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="Trades" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver:/Trades\SQL00;databaseName=Trades" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="dev" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="show_sql" value="true"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties>
</persistence-unit>
</persistence>
and my code is as follows and is located in src/main/resources/META-INF
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Trades");
System.out.println();
}
I have attached a screenshot which shows my eclipse set up
I solved this, turned out that Maven must have downloaded corrupt libraries - I was alerted this when I was unable to view the source for some classes in the Hibernates jars. I deleted the jars in my .m2 directory and when the jars were refreshed, it worked fine. A bit of tricky one.

How to populate a DB with my JPA entities in IntelliJ

Is there a way in IntelliJ to create tables in a database from my entity classes? I mean without ER diagram. The IDE allows me to create entities from the DB, but not the other way around.
I used to use netbeans and it just lets you add entities to your persistense.xml and select mode (drop and create, create, etc), but I can't find these options in IntelliJ.
I have configures de DB connection and it works, it just doesn't allow me to populate it with my entities.
Here's my persistence.xml from NetBeans (works perfect)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="ClubPU2" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>baseclub.entidades.Actividad</class>
<class>baseclub.entidades.Comercio</class>
<class>baseclub.entidades.Contacto</class>
<class>baseclub.entidades.Cuota</class>
<class>baseclub.entidades.DatosInstitucionales</class>
<class>baseclub.entidades.DatosPersonales</class>
<class>baseclub.entidades.Descuento</class>
<class>baseclub.entidades.Identificacion</class>
<class>baseclub.entidades.Miembro</class>
<class>baseclub.entidades.NotaEntrada</class>
<class>baseclub.entidades.Participacion</class>
<class>baseclub.entidades.Rol</class>
<class>baseclub.entidades.Secretaria</class>
<class>baseclub.entidades.Sesion</class>
<class>baseclub.entidades.Tutor</class>
<class>baseclub.entidades.Ubicacion</class>
<class>baseclub.entidades.Usuario</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/club_universitario?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="XXXXXX"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="club"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
And here's persistence.xml from IntelliJ:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="persistenceUnit">
</persistence-unit>
</persistence>
Is there any particular reason you want IntelliJ to create the database?
if you have selected Hibernate as ORM , you can simply add in your persistence.xml the following code:
<property name="hibernate.hbm2ddl.auto" value="update" />
Start your application server, deploy your app and the database will be created.

How to embed java database server into distributed jar?

I am working on a project that uses JDBC through derby.jar, and I am trying to make it usable without the database server running inside NetBeans. I already have a running database (wich is not embedded) and a complete code. Here is my xml that i use.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="boltPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entity.Termek</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/bolt;create=true"/>
<property name="javax.persistence.jdbc.password" value="asd"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.user" value="asd"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Any suggestion for its modification? I tried using EmbeddedDriver instead of ClientDriver, but i got an exception that it cannot be found.
Edit:
I tried putting all required jar files in the classpath and they are all added to the libraries.
Did you try putting the derby jar and other required jars by derby in the classpath of the app?
More instructions here
http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
Can you edit your question to include stacktrace of errors you get after trying above link

Hsqldb driver being randomly null

I am involved in writing a project which requires servlet->database connection. I am collaborating with another person who have designed a database using HyperSQL (hsqldb), I am now trying to merge my project with his by adding his code to mine.
Further to my issue though. When I copy the code, it works, usually. I have few methods that use the data from the database and compare them with user input.
When attempting to connect to the database, I would randomly succeed or fail, getting the following error ;
Unable to acquire a connection from driver [null]
I of course initialise a driver ;
Class.forName("org.hsqldb.jdbcDriver").newInstance();
Now, when running my method, it sometimes succeeds, and sometimes fails, here is the XML file for the DB;
<?xml version="1.0" encoding="UTF-8"?>
<!--
Creates both the HyperSQL databases using hibernate. No password or username is set.
-->
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="monsters" transaction-type="RESOURCE_LOCAL">
<class>databaseManagement.Monster</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:monsters"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
<persistence-unit name="users" transaction-type="RESOURCE_LOCAL">
<class>databaseManagement.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:users"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Probably an attempt is made to open the database in a readonly directory.
You need to specify a username for both persistence units. The default username is "SA".
The file path of the database that you specify in the URL is relative. It resolves to the execution directory. As you are developing a web application, you need to specify a directory that can be written to.
One way of doing this is by including a variable in the path, such as "jdbc:hsqldb:file:{$directorypath}/monsters" where the directorypath is the name of the web application's data dirctory, as specified in your web.xml file.

How to define an non-JTA datasource for JPA-Hibernate? org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource:

I am trying to change my web-app's JDBC code to JPA using Hibernate as provider. I am using Eclipse IDE. In that i have defined a MySQL data source. I added it in the persistence.xml.
But, I am getting the below error.
6640 [30289364#qtp-7494106-7] ERROR org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: tamSql
javax.naming.NameNotFoundException; remaining name 'tamSql'
My persistence.xml looks like,
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="ExpensePersistentUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>tamSql</non-jta-data-source>
<class>com.pricar.JPAInteg.Role</class>
<class>com.pricar.JPAInteg.User</class>
<class>com.pricar.JPAInteg.Userdetail</class>
<class>com.pricar.JPAInteg.Category</class>
<class>com.pricar.JPAInteg.Expens</class>
<class>com.pricar.JPAInteg.Leavetable</class>
<class>com.pricar.JPAInteg.Permissiontoken</class>
<class>com.pricar.JPAInteg.Roletokenassociation</class>
<class>com.pricar.JPAInteg.UserPK</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/officemgmt"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
Any Suggestions!!!
Thanks in Advance!
You don't need <non-jta-data-source> when your datasource is configured in <properties>. <non-jta-data-source> is needed when datasource is configured in application server configuration and obtained via JNDI.

Categories

Resources