The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
JDBC : MySQL Connection
Discuss JDBC : MySQL Connection in the Java Help forum on Dev Shed. JDBC : MySQL Connection Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 24th, 2001, 06:03 PM
|
|
Contributing User
|
|
Join Date: May 2001
Location: New Zealand
Posts: 638
Time spent in forums: 22 m 10 sec
Reputation Power: 13
|
|
|
JDBC : MySQL Connection
Greetings All,
I'm trying to use the mm.mysql.Driver to connect from JDBC to MySQL. I load the driver okay, but then get the following error message:
PHP Code:
SQLException: Cannot connect to MySQL Server on localhost:3306 Is there a MySQL Server running on the machine/port you are trying to connect to?
What port should I be connecting on? Do I need to configure JDBC or MySQL in someway? Any suggestions are very welcome.
Thanks,
Z.
|

August 25th, 2001, 08:11 AM
|
 |
Feelin' Groovy
|
|
Join Date: Aug 2001
Location: WDSMIA
|
|
|
How are you trying to connect (i.e. what commands are you using)?
__________________
Yawmark
class Sig{public static void main(String...args){\u0066or(int
\u0020$:"vÌÈÊ\"¤¾Àʲ¬Æ\"v¤Î¤\"²¤¨¸¬Æ".to\u0043h\u0061rArray()
)System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
+(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
|

August 25th, 2001, 06:21 PM
|
|
Contributing User
|
|
Join Date: May 2001
Location: New Zealand
Posts: 638
Time spent in forums: 22 m 10 sec
Reputation Power: 13
|
|
Greetings All,
I've included my basic class below, but I know it works because I tried it locally! So if anyone's want to know how to connect MySQL with JDBC here's some basic code, I'm using the MM driver, which you need to install and copy to the /jdk/jre/lib/ext directory. My problem is definately from the MySQL end, I cannot get a remote connection going and I really need to
Any help is appreciated,
Z.
java class connect
(I get the error just after connecting to MySQL hits the screen)
PHP Code:
import java.sql.*;
public class connect
{
public static void main(String[] Args)
{
System.out.println("Loading Driver.");
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try {
String url="jdbc:mysql://localhost/weblearn";
System.out.println("Connecting to MySQL.");
Connection Conn = DriverManager.getConnection (url, "root", "dalek$");
System.out.println("connected!");
Statement Stmt = Conn.createStatement();
ResultSet RS = Stmt.executeQuery("SELECT Name from test");
while (RS.next()) {
System.out.println(RS.getString(1));
}
// Clean up after ourselves
RS.close();
Stmt.close();
Conn.close();
}
catch (SQLException E) {
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}
}
}
|

August 29th, 2001, 12:09 AM
|
|
Junior Member
|
|
Join Date: Jun 2001
Location: Singapore
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Do you set up user on mysql table?
If so you may need to use
"jdbc:mysql://localhost/databasename?user=username"
|

August 31st, 2001, 04:34 PM
|
|
Contributing User
|
|
Join Date: Jun 2001
Location: NE Pa.
Posts: 96

Time spent in forums: 1 h 29 m 32 sec
Reputation Power: 12
|
|
|
NOt sure but this might help
I think your problem may be with the way your presenting the connection to the db....I had quite a few problems when i first started but then settled on this config & havent had a problem since....of course this is single connection stuff which should be avoided in favor of connection pooling if you expect mulitple db connections. Anyways, here's how I do it:
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/[dbName]?user=[dbUserName]&password=[dbPassWord]");
Statement stmt = conn.createStatement();
Hope this helps
Also your gonna have to catch 3 exceptions here or the compiler screams. They are InstantiationException, ClassNotFoundException & IllegalAccessException
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|