Buras
Buras

Reputation: 3099

How to specify path to mySQL using localhost?

I have been having issues specifying the path (I have two files: comments.frm and db.opt in the following folder: C:\xampp\mysql\data\feedback)... I am using XAMPP and mySQL. I am not sure why I am having an error? Please, take a look this part of my code:

public void readDataBase() throws Exception {
   try {
        // This will load the MySQL driver, each DB has its own driver
      Class.forName("com.mysql.jdbc.Driver");
      // Setup the connection with the DB
    connect = DriverManager
         .getConnection("jdbc:mysql://localhost//feedback"
              + "user=root&password=1234");

PS: My password for localhost is 12345678

Upvotes: 0

Views: 4077

Answers (6)

Andrzej Bobak
Andrzej Bobak

Reputation: 2112

Try

 connect = DriverManager
     .getConnection("jdbc:mysql://localhost/feedback?user=root&password=1234")

(you forgot about the question mark after "feedback")

Upvotes: 2

Arvind Vishwakarma
Arvind Vishwakarma

Reputation: 11

Try this hope it can help you!!!

Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/feedback?"+"user=root&password=1234");

also see the URL Mistakes like:

jdbc:mysql://localhost//feedback?

// after localhost... check and try my code...

.

Upvotes: 1

Aliaksei Bulhak
Aliaksei Bulhak

Reputation: 6208

I think you need to use this:

DriverManager.getConnection("jdbc:mysql://localhost:3306/feedback", "root", "1234");

I check my old project and find that I don't use double slash here /feedback.

also you can specify encoding like this :

 DriverManager.getConnection("jdbc:mysql://localhost:3306/feedback?characterEncoding=UTF-8&characterEncoding=Cp1251", "root", "1234");

and also one more advice. Don't use hard code. Get url, password and user name from property files.

Upvotes: 1

Zelldon
Zelldon

Reputation: 5516

Many answers but everyone forgot the port of the database.

If you use mysql then try 3306 as db port.
http://www.petefreitag.com/articles/jdbc_urls/ - list of jdbc urls (Examples)

try 
{ 
    conn = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName, user, passwd); 
} 
catch(SQLException sqle) 
{ 
    System.out.println("Connection fails: " + sqle.getMessage()); 
}

Upvotes: 1

John Jai
John Jai

Reputation: 3723

For better clarity you can use the other overloaded method while getting connection.

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://<db_ip>/<db_name>", 
    "<username>", "<pwd>");

Upvotes: 2

Alexandre Jacob
Alexandre Jacob

Reputation: 3041

You should try

DriverManager.getConnection("jdbc:mysql://localhost/feedback", "root", "1234");

Upvotes: 2

Related Questions