Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stuck in making connection to database

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to set up a database connection for java program running on desktop computer,
so that it can connect to database on a web server (my website)

I'm using netbeans, and am trying to set up the config file using the wizard, but its not making a connection.

A screenshot is shown here:


My website is www.colin-java.co.uk, and the database name is colinjava
But I'm unsure about if I need to write localhost or use port 8081 or 3306.

The message in red, shows its half working, but not totally.

Let me know if any advice, thanks.
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What database are you using? port 3306 looks like MySQL.

What is your JDBC connection string look like? Does your app have the JDBC driver.

Is your app a website or GUI? If web, usually the app server has data source XML files to configure this and your web app points to this configured name (not the xml file). If GUI pure JDBC.
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use "localhost" if the database server is running on your computer, i.e. the computer where the code will be running. If the database is running on some other computer then you should use the name of that computer.

As for the port number, you should use the number of the port where the database server is listening. Typically a MySQL server will be listening on port 3306, that's the default, but it can be configured to listen on any port. If you don't know what that is then ask the owner of the server.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replies...

I'm not sure what the JDBC connection string is, I haven't used hibernate for ages.

I have had a Hibernate project working correctly on a war file on desktop connecting to MySql on desktop,
and also a war file on the server connecting to MySql on the server.
So this time, its a little different, so I guess I don't need to write "localhost" as you've mentioned.

I am using MySQL here
I've added the Hibernate Library to project just by rightclicking in Netbeans and adding Hibernate, so theres 14 hibernate jar files, so thats okay I think.

I've tried it without localhost, and with using port 3306, but I still get the same message shown in red.

Let me know if any thoughts, thanks



EDIT, the app is a GUI, actually a JAVA-FX project, so I could use the basic jdbc stuff, with the Connection object.
I just thought Hibernate would be nicer to use, as its object oriented.


 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing at all to do with Hibernate. It's just plumbing.

Okay. Is the database server running? What's the name (or address) of the computer where it's running? What port is it listening on? Do you have direct network access to that computer from your computer? What about to that port?

I should say at this point that exposing a database server to the Internet is generally considered to be a bad idea, for security reasons.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks,

I tried using the JDBC connection thing this time, and I get the same message..

"The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."

The connection object is coming out as null.


I just wrote a php script to read from a table on the database, it doesn't complain about a connection, but its not reading anything in.
MySql admin says "Your PHP MySQL library version 5.1.72 differs from your MySQL server version 5.5.33. This may cause unpredictable behavior."

So I've messaged the hosting administrators to see what they say.
I'll keep you informed, thanks.

EDIT, so you think I would be better off calling a php script on server (from my desktop) that reads the database, or perhaps
a webapp on the server that uses hibernate to read the database [A bit more complicated though], just in terms of security issues?

Thanks
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
colin shuker wrote:EDIT, so you think I would be better off calling a php script on server (from my desktop) that reads the database, or perhaps
a webapp on the server that uses hibernate to read the database [A bit more complicated though], just in terms of security issues?


Yes.

You mentioned web hosting administrators... it's very likely that they have a firewall set up which prevents people from connecting to the database from outside. That would be what's preventing you from connecting to it.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think its that, cause I've managed to do it before from my pc.

Anyway, I assumed the database was working fine, as its always worked before..
But I checked them from what you advised, and I can't even get a php page to read data from a table.

I converted from mysql, to mysqli as mysql is deprecated now,
but its still not working, I get some error about a non-object.

I asked the administrators, they said its nothing to do with there being 2 differenet versions like I mentioned earlier,
and I should check the php logs, but there's nothing new in there.

So I'm a bit stumped now, I don't think I have to import anything like I do in java.

Oh well, thanks anyway
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How come we are talking about PHP all of a sudden? Back to the problem. You can't connect through java or php yet you once able to get into php's mysqladmin?

If say mysql admin works then the DB is working fine. Everything is your java app or php app.

FYI the jdbc for mysql looks like:

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I can get into phpMyAdmin right now,

But tried a few ways of reading data from database (with php), and its not happening:

http://www.colin-java.co.uk/php/phptest4.php
The output is just jibberish, should be a list of about 7 animals.

And http://www.colin-java.co.uk/php/phptest5.php
It shows
Fatal error: Call to undefined method mysqli_result::execute()

And then with the connection object you mentioned I just get null,
I've contacted the administrators again, and they are looking into it.

Thanks for your messages.
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the PHP I see that testphp5.php page shows the phpinfo(). Try enabling the mysqlnd library because I got it in mine phpinfo(). Also check in phpmyadmin the PHP extension. mysql or mysqli


Back to java. Did you have the JDBC library? Did you enter the information correctly? For the user/password, try using the one you used for logging to mysqladmin.

Another thing is is that database port 3306 even listening for connections?
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They told me mysqli was also deprecated, but the pdo should be used.

I'm able to read my table now with pdo from a php page, so that's part of it resolved.

I'll take another look at the JDBC thing, but I think all the info is correct.
Will let you know shortly.

On another note, I'm looking into a miniVPS hosting, so hopefully can run my servlets and stuff more easily on the web with it.
http://www.minivps.co.uk/

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic