views:

261

answers:

2

Hey, I'm getting this error:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Could not create connection to database server. 
Attempted reconnect 3 times. Giving up.

I'm just trying to connect to the database. With this code

<%@page import="java.sql.*"%>

<%
try{
//  Class.forName("com.mysql.jdbc.Driver");
    Class.forName("org.gjt.mm.mysql.Driver");

    out.println("found");
} catch (ClassNotFoundException ex){
    out.println("Erro<br/>");
    out.println(ex.toString());
} catch (Exception e){
    out.println(e.toString());

}

Connection ocon;

try{
ocon = DriverManager.getConnection("jdbc:mysql://localhost/cpjcoimbra?autoReconnect=true", "*****", "*****"); //password matches
out.print("connected");
} catch (Exception e){
    out.println(e.toString()+"<br/>");
}

%>

It does find the driver but I'm getting that error when i try to connect to the database.

I have this permission on catalina 50.local.policy

grant codeBase "file:/var/lib/tomcat6/WEB-INF/lib/-" {
  permission java.security.AllPermission;
};

Anyone has any idea why that error shows up?

Edit: service mysql status gives this:

 * /usr/bin/mysqladmin  Ver 8.42 Distrib 5.1.37, for debian-linux-gnu on i486
Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version   5.1.37-1ubuntu5
Protocol version    10
Connection   Localhost via UNIX socket
UNIX socket  /var/run/mysqld/mysqld.sock
Uptime:   1 hour 32 min 21 sec

Threads: 1  Questions: 103  Slow queries: 0  Opens: 171  Flush tables: 1  Open tables: 41  Queries per second avg: 0.18
+2  A: 

The driver is wrong - use the "com.mysql.jdbc.Driver" class.

Scriptlet code in a JSP? Oh, my - that's not good.

But those are not the cause of your problem.

Did you GRANT permissions to that username and password? Here are sample steps:

  1. Log in as root: mysql –h localhost –u root –p <ret>; password = <your admin password>
  2. “create database x;”
  3. “create user p identified by ‘q’;”
  4. “grant all on x.* to ‘x’@’%’;”

Give the username p and password q you used to create the user when you connect to the database.

Make sure the service is up and listening on port 3306. Open a command shell and type 'netstat -a' and look for a listener on port 3306.

This entry from the MySQL bug database might be pertinent as well.

For future reference, I find it helpful to paste whatever error message I get into Google. It's likely that I'm not the first person to run into a particular problem.

Even if you manage to solve this and get it to work, this is still a fatally flawed design. The JSP is connecting directly to the database - no security, except for the username and password you've entered in plain text. You really don't want to do this.

You're having a connection problem. Separate that from the JSP for starters.

  1. Can you connect using the MySQL admin tool in a command shell?
  2. Can you write a simple Java class to connect successfully to the database?
duffymo
The only i could find was this one:unix 2 [ ACC ] STREAM LISTENING 4787 /var/run/mysqld/mysqld.sockI triedchanging the connect to :4787 but still gives the same error. And yup i did create the users correctly
fmsf
What about the hosts file they cite in the link that I gave? Did you change that?
duffymo
And I can't find that driver inside the mysql-connector-java-5.1.6-bin.jar nor anywhere else :\ +1 for the help though
fmsf
yeah i had found that some hours ago and added it to the hosts.allow, but still did nothing, i'm starting to get desperated :S
fmsf
I just looked in mysql-connector-java-5.1.6-bin.jar for com.mysql.jdbc.Driver, and I can see it on my machine.
duffymo
ah ok lol you had tiped "org.mysql" that's why i couldn't find it :) and i have been googling for some hours now.
fmsf
It gives the same error with both drivers
fmsf
Sorry, my mistake.
duffymo
yes to question one and to question 2 that's why i'm getting desperate :S
fmsf
Which JDK are you using? Type "java -version" in a command shell.
duffymo
java version "1.6.0_15"Java(TM) SE Runtime Environment (build 1.6.0_15-b03)Java HotSpot(TM) Client VM (build 14.1-b02, mixed mode, sharing)
fmsf
A: 

I downgraded MySQL from 5.1 to 5.0 and everything is working now. Must be some sort of bug in the 5.1 version.

Well thanks for the help :)

fmsf