JDBC:MySQL problem

aodat2

Verified User
Joined
May 9, 2006
Messages
38
Hi there!

I'm not sure what's wrong but it seems that everytime I try to connect JDBC with MySQL, it couldn't connect.

I made a very simple script which goes something like this:

Code:
import java.sql.*;

   public class Connect
   {
       public static void main (String[] args)
       {
           Connection conn = null;
               String userName = "investmil_stock";
               String password = "xxxxxx";
               String url = "jdbc:mysql://127.0.0.1/investmil_stock";

           try
           {
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
               System.err.println ("Cannot connect to database server");
           }
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
       }
   }

It always says "Cannot Connect To Database Server".

Could anyone tell me what's wrong or what I did which is causing this error? Is the path to the database correct?

Please do help if you know how to solve this, I'm seriously confused.

Thanks for taking the time to look.


Ps. Error I'm getting is "java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:3306 connect,resolve)"
 
Last edited:
Hi,
I don't know your situation but anyway....in php...if any help to you.

I use "localhost" as beeing the hostname for my mysql server,
since php scripts run on the serverside....

And then connect to "mydatabase_WWW",
With "username_me" and "password"...

code looks like this:

$hostname_hostname = "localhost";
$database_hostname = "databasename_www";
$username_hostname = "username_me";
$password_hostname = "password";

$connect = mysql_connect($hostname_hostname, $username_hostname, $password_hostname) or trigger_error(mysql_error(),E_USER_ERROR);

Greetz
Wannes

:D
 
I'm not sure what you're trying to tell me but if you're looking at telling me about PHP, then we're in 2 different worlds as I'm talking about JDBC and MySQL. You seem to be talking about PHP and MySQL.

There's a world of difference between JDBC and PHP. One is Java and the other is PHP.

Does anyone else out there knows about this?


Then again, thank you for trying to help me out here.
 
it is related to the catalina.policy (tomcat 5) file in the tomcat conf directory (eg. /usr/local/tomcat/conf). for tomcat 4....its webapps.policy something (not sure)

you need to grant connect, resolve permission to the mysql-connector JAR file. otherwise you will get "access denied" exception.
 
Back
Top