Accessing a MySQL database

Hi,
  I imagine I'm doing something fairly stupid but I've followed and looked at several tutorials and have still come up short. I've been at this a long time so I imagine that I've missed something :)

 Whenever I try and connect to the MySQL database using J Connector, I get an SQLException with "No suitable driver".  The jar file provided by MySQL is definately in the path and I've got an import statement for it. Here's the code I'm trying :


Connection conn;

DriverManager.registerDriver(new Driver());

conn = DriverManager.getConnection("jdbc:mysql://192.168.1.111/acr","testuser","*****");


The import statement is :


import com.mysql.jdbc.Driver;


and I'm sure that it's importing it okay.


Any ideas?
LVL 1
petepalmerAsked:
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.

zzynxSoftware engineerCommented:
0
zzynxSoftware engineerCommented:
0
petmagdyCommented:
make sure u have libraries like this:
mm.mysql-2.0.7-bin.jar and mysql-connector-java-3.0.8-stable-bin.jar

r in ur classpath

if highter version numbers in the file name it is ok
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

petepalmerAuthor Commented:
hi,
  The one I've added is  :


mysql-connector-java-3.0.16-ga-bin.jar

  I got it from the MySQL website this morning as the latest stable 3.0 driver.... supposedly :)
0
petepalmerAuthor Commented:
What is the difference between using J Connector and the MM driver?  
0
petmagdyCommented:
I think it is not alone please find and add the other jar
0
petmagdyCommented:
ok this is wrong:

>>DriverManager.registerDriver(new Driver());

try:
     Class.forName("com.mysql.jdbc.Driver").newInstance();

0
petmagdyCommented:
please drop mny comment regarding the jars in classpath and just apply the last comment
0
petmagdyCommented:
>>please drop mny comment ....
I mean:
please drop my comment ....
0
petepalmerAuthor Commented:
I tried doing it that way too - same problem - "No Suitable Driver"

0
petmagdyCommented:
please at ur command line run this:

echo %CLASSPATH% if u r windows and
echo $CLASSPATH if linux
0
girionisCommented:
> The jar file provided by MySQL is definately in the path

It should be in your *classpath* not path.
0
petmagdyCommented:
and offcourse post the result
0
petepalmerAuthor Commented:
I'm using J IDEA at present which handles all that for me so it's not actually in the classpath but specified one the command line  as follows :


C:\j2sdk1.4.2_06\bin\java -Didea.launcher.port=7542 -Didea.launcher.library=C:\IntelliJ-IDEA-4.5\bin\breakgen.dll -Dfile.encoding=windows-1252 -classpath "C:\j2sdk1.4.2_06\jre\lib\charsets.jar;C:\j2sdk1.4.2_06\jre\lib\jce.jar;C:\j2sdk1.4.2_06\jre\lib\jsse.jar;C:\j2sdk1.4.2_06\jre\lib\plugin.jar;C:\j2sdk1.4.2_06\jre\lib\rt.jar;C:\j2sdk1.4.2_06\jre\lib\sunrsasign.jar;C:\j2sdk1.4.2_06\jre\lib\ext\dnsns.jar;C:\j2sdk1.4.2_06\jre\lib\ext\ldapsec.jar;C:\j2sdk1.4.2_06\jre\lib\ext\localedata.jar;C:\j2sdk1.4.2_06\jre\lib\ext\sunjce_provider.jar;C:\ojdbc14.jar;C:\pdflib.jar;C:\mysql-connector-java-3.0.16-ga-bin.jar;C:\Documents and Settings\Pete Palmer\IdeaProjects\Achievement\classes;C:\IntelliJ-IDEA-4.5\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Test
0
petmagdyCommented:
if the driver mysql-connector-java-3.0.16-ga-bin.jar is not in ur classpath then when u run ur Java program do this:

java -cp .;c:\pathToMySQLJar\mysql-connector-java-3.0.16-ga-bin.jar MyJavaApp
0
petmagdyCommented:
please simply try this:
java -cp C:\Documents and Settings\Pete Palmer\IdeaProjects\Achievement\classes;C:\mysql-connector-java-3.0.16-ga-bin.jar Test
0
petepalmerAuthor Commented:
Same thing :


Exception in thread "main" java.sql.SQLException: No suitable driver
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at MIS.<init>(MIS.java:25)
        at Test.main(Test.java:12)
0
petmagdyCommented:
can u please post the Test class code?
0
petepalmerAuthor Commented:
the test code basically instantiates a Hierarchy object which is where the problem lies. Here's the code for that object :


import org.gjt.mm.mysql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;

/**
 * Created by IntelliJ IDEA.
 * User: Pete Palmer
 * Date: 24-Nov-2004
 * Time: 11:52:59
 * To change this template use File | Settings | File Templates.
 */
public class Hierarchy {

    Connection conn;

    public Hierarchy(String url,String username,String password) throws Exception{


    DriverManager.registerDriver(new Driver());

     conn = DriverManager.getConnection("jdbc:mysql://192.168.1.111/acr","testuser","*****");



    }


}



I have tried various things and this is how the file is at present. I've tried various imports both the one the docs say and the MM driver - still no luck. I've also tried both ways of loading the driver....
0
petmagdyCommented:
now it works on my machine, but what happened is that I downloaded a file called:

mysql-connector-java-3.0.16-ga.zip

from http://dev.mysql.com/downloads/connector/j/3.0.html

I imagine that u just renamed it to jar and added to ur classepath, if u did so Wrong!!

u will simply unzip the zip file contents and in the unzipped root directory u will find the jar file at ( mysql-connector-java-3.0.16-ga\mysql-connector-java-3.0.16-ga-bin.jar)

this is the one that should be in ur classpath!!
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
petmagdyCommented:
also i am using:

        Class.forName("com.mysql.jdbc.Driver").newInstance();
0
petepalmerAuthor Commented:
I did extract the correct jar, not just rename the zip file.  If it's working okay for you, I'll change the line to match yours and see if that helps :)
0
drjustinCommented:
Try getting connection this way

  public static String createFullDBUrl(String db_server, String db_database, String username, String password)
  {
    MessageFormat mf = new MessageFormat("jdbc:mysql://{0}/{1}?user={2}&password={3}&useUnicode=true&characterEncoding=windows-1250");
    Object[] args = {db_server, db_database, username, password};

    return mf.format(args);
  }

String dbURL = createFullDBUrl(<server>, <db>, <username>, <password>);
return DriverManager.getConnection(dbURL);

and not with

DriverManager.getConnection("jdbc:mysql://192.168.1.111/acr","testuser","*****");

You probably have library created in IDEA for MySQL connector. And if it is checked to be used in this project, there should be no problem not finding the appropriate driver.

0
petmagdyCommented:
drjustin,

this question specific problem is not related to the database url, the problem is still in an earlier phase which is "loading the suitable mySQL JDBC driver"
0
petepalmerAuthor Commented:
I tried that change to my source code and it worked. I really don't know why as I have definately tried it before. It seems quite happy now and as petmagdy has been constantly working on this I'm going to give him the 500 points
0
petmagdyCommented:
thanks :-)

and welcome any time ;-)
0
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
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.