?
Solved

Accessing a MySQL database

Posted on 2004-11-24
26
Medium Priority
?
311 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:petepalmer
  • 14
  • 8
  • 2
  • +2
26 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12665217
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12665225
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665238
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:petepalmer
ID: 12665273
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
 
LVL 1

Author Comment

by:petepalmer
ID: 12665281
What is the difference between using J Connector and the MM driver?  
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665315
I think it is not alone please find and add the other jar
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665398
ok this is wrong:

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

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

0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665420
please drop mny comment regarding the jars in classpath and just apply the last comment
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665475
>>please drop mny comment ....
I mean:
please drop my comment ....
0
 
LVL 1

Author Comment

by:petepalmer
ID: 12665515
I tried doing it that way too - same problem - "No Suitable Driver"

0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665544
please at ur command line run this:

echo %CLASSPATH% if u r windows and
echo $CLASSPATH if linux
0
 
LVL 35

Expert Comment

by:girionis
ID: 12665549
> The jar file provided by MySQL is definately in the path

It should be in your *classpath* not path.
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665553
and offcourse post the result
0
 
LVL 1

Author Comment

by:petepalmer
ID: 12665586
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665623
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665656
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
 
LVL 1

Author Comment

by:petepalmer
ID: 12665707
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12665728
can u please post the Test class code?
0
 
LVL 1

Author Comment

by:petepalmer
ID: 12665762
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
 
LVL 13

Accepted Solution

by:
petmagdy earned 2000 total points
ID: 12665920
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12666123
also i am using:

        Class.forName("com.mysql.jdbc.Driver").newInstance();
0
 
LVL 1

Author Comment

by:petepalmer
ID: 12666532
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
 
LVL 3

Expert Comment

by:drjustin
ID: 12667922
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12672858
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
 
LVL 1

Author Comment

by:petepalmer
ID: 12673073
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12673131
thanks :-)

and welcome any time ;-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question