wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Can't connect to MySQL database with Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Can Watch "Can New topic
Author

Can't connect to MySQL database with Hibernate

Adam Compton
Greenhorn

Joined: May 13, 2011
Posts: 5
Hi, I'm trying to get my program to connect to my database, I've got the connection details here:

MySQL database name: a1847282_animals
MySQL user name: a1847282_adam
Password for MySQL user: passw0rd

Table name: Animals
Field 1: animalID - int size 3
Field 2: animalName - text size 20
Field 3: animalType - text size 20

My Hibernate Config File:


My Hibernate Mapping file


My Mapping Class


Here's my code to save data to the database from a List<Animal>


Now when I try and connect I get these errors:
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Hi! Welcome to the Ranch!

Adam Compton wrote:


That doesn't look like a Hibernate problem to me. Can you reach your database by other means?
Adam Compton
Greenhorn

Joined: May 13, 2011
Posts: 5
I'm not sure how else to do it, the only way I can check to see if it worked is to use PHPMyAdmin on the web host site.

I have these namespaces on the class file that handles the saving and loading but I can't get the getSession() method to create queries:




Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
PHPMyAdmin runs on the server, so it is not helpful in this case.

You could try to connect with the MySql Query Browser from your local machine. Or setup the database connection in your IDE (which is handy anyways, especially when you use the JBoss Hibernate tools later). All IDEs have some means of database browser.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Adam Compton wrote:


Hm.... and you have to be sure your web host allows connections from outside (i.e. applications not running on the same server) to the database. For security reasons this typically will not be the case. Then you have to setup a local database for development.
Adam Compton
Greenhorn

Joined: May 13, 2011
Posts: 5
I'm quite new to using MySQL in Java and Hibernate, can you tell me how to do it in Netbeans 7?
Adam Compton
Greenhorn

Joined: May 13, 2011
Posts: 5
Okay if I can't link to it from the outside is there a way I can upload the .jar file of the program to the web host and run it from there? It's a console based program.

Also can you tell me what namespace I need to implement to get getSession() to work as well for my loadDatabase method?
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
You can find a tutorial at the Netbeans site.
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
Okay if I can't link to it from the outside is there a way I can upload the .jar file of the program to the web host and run it from there?


I doubt it. You need a shell access and Java installed. Then it is just as any other system, if you want to use it as console tool and not as a web app.

Do you have a dedicated or virtual server? Then yes, you can do whatever you want. Within the typical (cheap) web package? Next to no chance. Most probably you can run PHP there...
Adam Compton
Greenhorn

Joined: May 13, 2011
Posts: 5
I've changed to using a local database within NetBeans and I've almost got it to work but I'm having some issues either regarding the mapping or the way I'm using the sessionfactory.

Configuration file, I think it's working 100% now.


The database is called AnimalsDatabase:
Table: Animals
PK: id - integer
name - varchar
type - varchar

Now the thing I'm confused about is when I created the table the names were all lowercase but on the Services database explorer they come up as full CAPS.

The Mapping File:


The save method in my program:



Here are the errors:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't connect to MySQL database with Hibernate