Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JDBC-MySQL unable to establish connection.

Status
Not open for further replies.

azrael808

Programmer
Jan 21, 2004
6
GB
Hello,

I have written a DatabaseManager class that controls access to a locally stored database, implemented in mysql. In theory, client request should be passed to the DatabaseManager through RMI, but I cannot even create a connection to the database yet!

Here's my connect statement:


con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/media", "mysql", "mysql");



When this statement is executed, the following exception is produced:


java.sql.SQLException: Unable to connect to any hosts due to exception: java.security.AccessControlException: access denied (java.util.PropertyPermission file.encoding read)



Can somebody explain to me how I successfully create a connection to the database? I would be extremely grateful. Also, could somebody explain how I could connect to the database from a remote machine using the following connect statement:


con = DriverManager.getConnection("jdbc:mysql://server:3306/media", "mysql", "mysql");



I have a feeling I need to grant a certain PropertyPermission in my policy file, but I'm unsure of what values I should be using.

Thank you in advance.

Pete
 
//try the following code
Code:
class myjdbc
      {
      public static void main(String args[])
       {   
    String username;
    String password;
    String url;
    String dropString;
    String createString;

    // ---- configure START
    username = "root";
    password = "";
    // The URL that will connect to TECFA's MySQL server
        // Syntax: jdbc:TYPE:machine:port/DB_NAME
        //url = "jdbc:mysql://localhost:1433/testdb";
        url = "jdbc:mysql://localhost:3306/ptest";
    // ---- configure END

    // INSTALL/load the Driver (Vendor specific Code)
        
    try {
        Class.forName("org.gjt.mm.mysql.Driver");
    } catch(java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }  
        
 Connection  con;
 
         try {    
        // Establish Connection to the database at URL with usename and password
con = DriverManager.getConnection(url, username, password);

Statement stmt = con.createStatement ();

ResultSet rset = stmt.executeQuery ("select store_name from geography;");
while (rset.next ())
   {
   System.out.println (rset.getString (1));
   }
        stmt.close();
            con.close();        
        } 

    catch(SQLException ex) {
            System.err.println("==> SQLException: ");
        while (ex != null) {
        System.out.println("Message:   " + ex.getMessage ());
        System.out.println("SQLState:  " + ex.getSQLState ());
        System.out.println("ErrorCode: " + ex.getErrorCode ());
        ex = ex.getNextException();
        System.out.println("");
        }
        } 
 }// end main
}// end class
//you should have the following library from mysql-connector-java-3.0.9-stable.zip
//org.gjt.mm.mysql.Driver
//com.mysql.jdbc.Driver
//this is my reply to a previous post
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top