This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JDBC and the fly likes mysql_connect doesnt work :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "mysql_connect doesnt work :(" Watch "mysql_connect doesnt work :(" New topic
Author

mysql_connect doesnt work :(

Tim Tock
Greenhorn

Joined: Jan 15, 2004
Posts: 15
hey all, im just editing this , cos ive got some of it working, now when i type in javap com.mysql.jdbc.Driver i get this, is this correct?
C:\>c:\j2sdk1.4.2_03\bin\javap com.mysql.jdbc.Driver
Compiled from "Driver.java"
public class com.mysql.jdbc.Driver extends com.mysql.jdbc.NonRegisteringDriver{
public com.mysql.jdbc.Driver();
throws java/sql/SQLException
static {};
}

Ive wrote a small program to connect to a database with the line
con = DriverManager.getConnection"jdbc:mysql:C:\\mysql\\data\\login", "tock","tick");
the first bit is the path is to my login folder with my mysql db in, the username and password are what ive put in my my.ini file, can anyone see where im going wrong? as i keep getting this message:
Exception: Invalid authorization specification, message from server: " Access denied for user: 'root@localhost' (Using password: YES)"
previously, my my.ini file didnt have a username and password, but i seem to connect fine using my php script with this script,
$conn = mysql_connect('localhost','root','') or die("Could not connect to mysql");
even now, now that i've put a username and password into my.ini

please help im confused

Thanks
Timmy
[ February 19, 2004: Message edited by: Tim Tock ]
[ February 19, 2004: Message edited by: Bear Bibeault ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I wasn't aware of that form of URL for the MySQL connection. I'd say it was wrong, but then you wouldn't be getting the error we're seeing.
Have you tried something more standard like jdbc:mysql://localhost/database ?
Tim Tock
Greenhorn

Joined: Jan 15, 2004
Posts: 15
i dont understand it, i've just tried connecting to the test db with the following combinations:
con = DriverManager.getConnection("jdbc:mysql:C:\\mysql\\data\\test",
"root", "secret");
con = DriverManager.getConnection("jdbc:mysql://test",
"root", "secret");
and still i get the same error

just seen this, The root user has of course acces to all databases, while the nameless user has access to the predefined database "test" and can create and manage other databases beginning with a name beginning with "test_". If you don't want to fight with user names, access rights and passwords, then a login without user name and password and databases named test_XYZ should get you going.
so in theory i cud connect to the test db without a user name or password, again, this is what i've tried to no avail
con = DriverManager.getConnection("jdbc:mysql:C:\\mysql\\data\\test");
con = DriverManager.getConnection("jdbc:mysql:///test");
please help!!!
[ February 20, 2004: Message edited by: Tim Tock ]
Tim Tock
Greenhorn

Joined: Jan 15, 2004
Posts: 15
ok all i wanna do now is connect to the test database, that doesnt require a username or password, and it still doesnt work, this is the code im using, please please help, im so stressed out, keep getting the same error message

package com.stardeveloper.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcExample2 {
public static void main(String args[]) {

try {java.sql.Connection con;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql:C:\mysql\data\test");
if(!con.isClosed())
System.out.println("Successfully connected to MySQL server using TCP/IP...");
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
}
}
}
[ February 20, 2004: Message edited by: Tim Tock ]
[ February 20, 2004: Message edited by: Tim Tock ]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You need to make sure that the "mySQL" service is running, and then the URL to use is one of the following:
"jdbc:mysql://localhost"
"jdbc:mysql://localhost/db-name"
The second one connects to a specific database (replace "db-name" with whater your database is). If the database is not running on your local machine, then replace "localhost" with the ip-address of the machine on which it's running.
You can pass in the username and password, and additional parameters on the URL if you need, but the above will be suffient for a basic connection.
Notice that this is exactly what David suggested in the first reply. In your code examples, you're missing the "localhost/" part of the URL. It's sort'a important ...
[ February 20, 2004: Message edited by: Wayne L Johnson ]
Tim Tock
Greenhorn

Joined: Jan 15, 2004
Posts: 15
hi wayne,
many thanks for replying, ive tried what you said, using this line in my code
con = DriverManager.getConnection("jdbc:mysql://localhost:8080/cc301/test");
but it still doesnt work, i've got mysqladmin running, and apache as well, but i dont think that matters, still get the same error, the only thing im not too sure about is, there isnt like a main file for a mysql db, just a folder with files for the tables made within the db, unlike for example if it was a access db i wud have put test.mdb,
any other ideas?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Did you try:
con = DriverManager.getConnection("jdbc:mysql://localhost/test");
as David and I suggested? This assumes that the database is named "test".
Tim Tock
Greenhorn

Joined: Jan 15, 2004
Posts: 15
yep tried that
same ol error:
Exception: Invalid authorization specification, message from server: "Access denied for user: 'root@localhost' (Using password: YES)"
[ February 21, 2004: Message edited by: Tim Tock ]
Jason Steele
Ranch Hand

Joined: Apr 25, 2003
Posts: 100

The fact that you are getting the access denied error says that the server is up. However, you are not supplying login credentials within your url. You need to add:
con = DriverManager.getConnection("jdbc:mysql://localhost/test?user=username&password=password");


An egg is a chicken's house!
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Just out of curiousity, can you log on to the server locally?
>mysql -u root -p.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mysql_connect doesnt work :(
 
Similar Threads
need help with mysqldump...
mySQL driver problem jvm WindowsXP, tried everything!! - please help!!!
j_security_check question
MySQL DataBase
Don't know how to connect to MySql