Results 1 to 19 of 19
  1. #1
    Kelly is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default Problem connecting to mysql database

    Hey, Im pretty new to java and have been trying to connect to a sql database not on localhost. I keep geting the error

    jdbc:mysql://websiteurl.com:3306/
    java.sql.SQLException: Access denied for user 'myTESTusername'@'d58-110-200-85.mas9.nsw.optusnet.com.au' (using password: YES)

    Process completed.


    Ive changed the varibale which stores the username to "myTESTusername@websiteurl.com" but it keeps showing up as "'myTESTusername@'d58-110-200-85.mas9.nsw.optusnet.com.au" in the error message, does my internet provider optusnet blocking me or something?

    Here is a copy of my code.



    Java Code:
    import java.sql.*; 
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class websiteurlJDBCtest {
        public static void main(String[] args) throws Exception {
            websiteurlJDBCtest.useConnection();
        }
      	public static void useConnection() throws Exception {
            Class.forName("com.mysql.jdbc.Driver");
    	 	String URL = "jdbc:mysql://websiteurl.com:3306/";
    	 	System.out.println(URL);
    	 	String Database = "testDATABASE";
    	 	String UserName = "myTESTusername@websiteurl.com";
    	 	String Password = "myTESTpassword";  
    	    Connection conn = null; // connection object
    	    Statement stmt = null; // statement object
    	    ResultSet rs = null; // result set object
    	   	try {
    	      	conn = DriverManager.getConnection(URL+Database,UserName,Password); 
    			System.out.println("Connection Established");
    	   	}
    	   	catch(Exception e) {
    	   		System.out.println(e);	
    	   	}
      	}
    }

    I would really appriciate any help or ideas
    thanx

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That message is coming from the mysql, not your client, and you changing that "value" doesn't help as mysql will use the username and the actual hostname from which the connection was attemtpted from. You need to read the MySQL manual paying attention to the User Administration portions and, in particualr, the GRANT statement portion.

  3. #3
    Kelly is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    I used cpanel to create the database, the user has all privledges to that database...

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    366
    Rep Power
    6

    Default

    Are you using some shared hosting for this?

    Are you trying to connect from home PC to some remote
    hosted MySQL?

    Or both MySQL and your Java code are on same machine?

  5. #5
    Kelly is offline Member
    Join Date
    Mar 2010
    Posts
    7
    Rep Power
    0

    Default

    the code and the mysql are not on the same machine, the database is hosted remotely, im just compiling the code on my home pc

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The user has the rights, yes, I never said he didn't, but you also need to have user@host granted those rights, and seemingly you have them granted for one host, but the user is coming from another. That is what that message means. Read that portion of the manual again.

    Edit: And don't say that "you know that's not the case", because you're wanting to change the username to "username@somehost" tells me that you don't understand it, at all.

  7. #7
    Md_Kayesh is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default Problem with connection with mysql

    I made the following code. I have jdbc driver in mysql connector. i have added the directory to classpath variable. Then ran the following code with jcreator which shows the error:
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver.
    Then i tried with netbeans and it worked well. But i want to handle it manually. Anybody have any idea?? plz help...

    import java.sql.*;

    public class Connect
    {
    public static void main (String[] args)
    {
    Connection conn = null;

    try
    {
    Class.forName ("com.mysql.jdbc.Driver");
    String connectionUrl = "jdbc:mysql://localhost/mydatabasename" +
    "user=root&password=mysecretpassword";
    conn = DriverManager.getConnection (connectionUrl);
    System.out.println ("Database connection established");
    }
    catch (Exception e)
    {
    System.err.println ("Cannot connect to database server");
    e.printStackTrace();
    }
    finally
    {
    if (conn != null)
    {
    try
    {
    conn.close ();
    System.out.println ("Database connection terminated");
    }
    catch (Exception e) { /* ignore close errors */ }
    }
    }
    }
    }

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do you mean handle manually? Do you mean run from comamnd line?
    If so then you should provide the -cp flag pointing to the mysql driver.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Start your own thread. That post is not related to the rest of this thread in any way, shape, or form. As hint "Classpath, Classpath, Classpath".

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Oh God, not again. Stop posting this trash all over the place.

  11. #11
    hitesh is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Default

    how can i provide -cp flag pointing to the mysql driver??????????????

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    java -cp nameofjarfilewithmysqldriver.jar .....

  13. #13
    hitesh is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Default

    Yes I have the jar file. But i don't know how to install or where to copy that file
    I use the code as follows-

    public void Connect()

    {

    try

    {

    // Load the JDBC driver

    String driverName = "com.mysql.jdbc.Driver";



    Class.forName(driverName);



    // Create a connection to the database

    String url = "jdbc:mysql://127.0.0.1:3306/abc";

    String username = "root";

    String pass = "xyz";

    con1=DriverManager.getConnection(url, username, pass);

    }

    catch (ClassNotFoundException e)

    {



    System.out.println("Connection Failed");

    System.out.println(e);

    }

    catch(SQLException e)

    {

    System.out.println("Connection Failed1");

    }



    }

    When I run this code I get the error-
    Connection Failed
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    When I run this code I
    Can you show the command line you use when you "run this code"?
    The java program can not find a class definition. The jar file containing that class definition must be on the classpath.

  15. #15
    hitesh is offline Member
    Join Date
    Jul 2010
    Posts
    28
    Rep Power
    0

    Arrow

    Quote Originally Posted by Norm View Post
    Can you show the command line you use when you "run this code"?
    The java program can not find a class definition. The jar file containing that class definition must be on the classpath.
    I use the redhat Linux.
    After running this code the messages are as follows

    [root@server bhabani_press]# java NDitem
    GTK Accessibility Module initialized
    Connection Failed
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    What jar file is the com.mysql.jdbc.Driver class in? Add that file to the classpath when using the java command:

    java -classpath THEJARFILENAME.jar NDitem

  17. #17
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Default

    set classpath in envoronment variable
    path since mysqlconnector.jar
    mysqlconnector.jar put into the lib folder

  18. #18
    Join Date
    Jul 2010
    Posts
    38
    Rep Power
    0

    Default

    set classpath in envoronment variable
    path since mysqlconnector.jar
    mysqlconnector.jar put into the lib folder

  19. #19
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    No, do not "put it into the lib folder" (edit: if by that you mean the jre/lib/ext directory, if not, then expand on that, as alone, it means nothing, then). And don't try with the environment variable, that is used by almost nothing, anymore.
    Last edited by masijade; 07-22-2010 at 07:45 AM.

Similar Threads

  1. problem with connecting database
    By ravidasineni in forum AWT / Swing
    Replies: 1
    Last Post: 11-24-2009, 07:04 AM
  2. Problem in connecting to database..
    By Preethi in forum New To Java
    Replies: 10
    Last Post: 06-26-2008, 06:53 AM
  3. problem in connecting to mysql database
    By nancyv in forum Java Servlet
    Replies: 6
    Last Post: 04-02-2008, 12:33 PM
  4. Problem connecting to my Database using Java
    By javaneed in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 10:35 AM
  5. connecting to mysql database
    By javagal in forum NetBeans
    Replies: 2
    Last Post: 08-04-2007, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •