JDBC Connection with EasyPHP's mySQL

I'm a noob with JDBC, i cant seem to figure out the url or maybe the Class.forName args.
import java.sql.*;
 
public class DBTester {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Connection conn = null;
 
        try
        {
            String userName = "root@localhost";
            String password = "";
            String url = "jdbc:mysql://localhost:3306";
            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 */ }
            }
        }
 
 
	}
 
}

Open in new window

etotte1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
>             String url = "jdbc:mysql://localhost:3306";

connection string needs the db name

            String url = "jdbc:mysql://localhost:3306/mydb";

0
etotte1Author Commented:
Now it says:



Error

MySQL said: Documentation
#1045 - Access denied for user 'root'@'localhost' (using password: NO)
Connection for controluser as defined in your configuration failed.
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.
Open new phpMyAdmin window
0
Mick BarryJava DeveloperCommented:
           String userName = "root@localhost";
            String password = "";

you need to set those to a mysql user that has access to the databsee
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

etotte1Author Commented:
where can i do that? this is showing up when i try and open myphpadmin on Xampp localhost
0
Mick BarryJava DeveloperCommented:
>            String userName = "root@localhost";

that should probably be:

           String userName = "root";


0
etotte1Author Commented:
This is aside from my java program now, i cannot even get into mySql on my localhost
0
Mick BarryJava DeveloperCommented:
think you wouldn't have been prompted for the root password when you installed it

0
etotte1Author Commented:
ok well mySql works now but my code Still Does Not Work... any suggestions???
0
Mick BarryJava DeveloperCommented:
use the same user/pass in youtr java code

0
etotte1Author Commented:
Had to install library to use com.mysql.jdbc.Driver... Thanks for your time.
0
Mick BarryJava DeveloperCommented:
and you also had to make the changes I suggested :)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.