File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Problem with connecting to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with connecting to database" Watch "Problem with connecting to database" New topic
Author

Problem with connecting to database

Joonas Järvinen
Greenhorn

Joined: Mar 30, 2009
Posts: 18
I'am building a java application that needs JDBC connection to the database. The problem is. It does not connect. I'am running the database on my localhost and using Vista. The strange part is that it will connect to the db without problems with my other computer, which uses Windows XP. Also using localhost db. Any idea what could possibly cause this kind of behaviour?
Heres the used code:


And here is the error that printStackTrace() gives. To me it doesn't make any sense, but maybe you could understand it better:


Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

A stack trace shows the cause at this line


so remove newInstance() while loading driver class.

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Joonas Järvinen
Greenhorn

Joined: Mar 30, 2009
Posts: 18
Uh, still not working. I have tried many ways to code this. So I'am starting to believe that this is happening because of the positions of the Mars and Venus or something like that.
Here's yet another way to establish JDBC connection with localhost database. Which of course work with XP but with Vista:

And here's the error message which is invoked by 'conn = DriverManager.getConnection(url, userid, password);'
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42923
    
  68
Vista is stricter about certain security issues; maybe it's a firewall issue?
Joonas Järvinen
Greenhorn

Joined: Mar 30, 2009
Posts: 18
Firewall can block connection even tho db is running from the localhost? Well. What ever it is. It's not fw.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Joonas Järvinen wrote:Connection refused

That roughly means that the desired host/IP is not accessible on the specified port. Check if the port number is correct, check if it actually listens on the port, check if not any hardware or software firewall or router is blocking the port.

Sagar Rohankar wrote:A stack trace shows the cause at this line


so remove newInstance() while loading driver class.

Can you please elaborate how you came up to this conclusion? I really, really don't see it .. I'd expect that you, with >1k posts, should know better about Java Basics.
Kalyan Ram
Greenhorn

Joined: Dec 29, 2003
Posts: 14
As the parent said, you have java.net.ConnectException very early in the stack.

-Are you able to connect to the local mysql server via command line?
-Do you have the appropriate permissions in mysql.user table?
-If mysql isn't listening on the standard port (3306), then the port must be part of the database URL

@Rohankar
20.4.5.1.1 of MySQL manual says

// The newInstance() call is a work around for some broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();

so newInstance() should not be the culprit?

Like Scholtz, I too am interested in the reason behind your advise. Have you heard of any instances where newInstance() broke the application?

regards
/k
Joonas Järvinen
Greenhorn

Joined: Mar 30, 2009
Posts: 18
I'am not that familiar with command line when connecting to the DB. I typed '-uabcuser -pabc' and clicked enter as the quide I was following said. But it just whiched line and made '->' mark. Then I tried ';' to mark as end of the sentence, but it didn't work either. So what am I doing wrong here?
DB sure works fine with MySQL Query browser.
Also, if I want to add port to the url. Is it like this?

If so, it didn't work either.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
I've mentioned more than one possible cause before. Please verify them all.
Joonas Järvinen
Greenhorn

Joined: Mar 30, 2009
Posts: 18
If you mean if that port is beign listened. Then I would guess yes. This is from 'netstat -a'
TCP 0.0.0.0:3306 My-PC:0 LISTENING
And MySQL query browser also connect to the db using that port. Also the abcuser has every access possible to give in my db server.
Kalyan Ram
Greenhorn

Joined: Dec 29, 2003
Posts: 14
Joonas Järvinen wrote:If you mean if that port is beign listened. Then I would guess yes. This is from 'netstat -a'
TCP 0.0.0.0:3306 My-PC:0 LISTENING
And MySQL query browser also connect to the db using that port. Also the abcuser has every access possible to give in my db server.

You missed Scholtz's advise.

Your java code is being executed by the run time (java.exe). The firewall will see java.exe trying to network. So check your firewall/s and see if java.exe (or equivalent for your OS) has permissions to network.

static String url = "jdbc:mysql://localhost:3306";
is fine. BTW you can make this string final too.
Victor Twomey
Greenhorn

Joined: Oct 19, 2008
Posts: 5
do you have antivirus software on the vista machine? is it blocking connection to mysql?
Joonas Järvinen
Greenhorn

Joined: Mar 30, 2009
Posts: 18
Well... I do have F-Secure internet security. But I have opened port 3306 and allowed all needed programs in it. I have also tried connecting to the db when all security programs are shut down. With no results what so ever.
Victor Twomey
Greenhorn

Joined: Oct 19, 2008
Posts: 5
try creating system dsn on vista machine. perhaps during the dsn creation it will give you a test connection
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Joonas Järvinen wrote:Firewall can block connection even tho db is running from the localhost?


Yes, firewalls can block local connections.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with connecting to database