Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

install mysql driver

Status
Not open for further replies.

rs51

Technical User
Oct 13, 2001
163
0
0
PT
hi
i'm very new to java (just curious)
i'm following this tutorial on databases and at certain point i've to use the driver

i went to mysql download this:
mysql-connector-java-3.0.9-stable wherein is the .jar file.
i googled a lot in forums and also read the readme and the sun One Studio 4 update 1 (the version i'm using). Cant find a hint on how to solve it, because when i try to compile i get this error:
"TesteJDBC.java [25:1] unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName("com.mysql.jdbc.Driver").newInstance();
^
TesteJDBC.java [25:1] unreported exception java.lang.InstantiationException; must be caught or declared to be thrown
Class.forName("com.mysql.jdbc.Driver").newInstance();
^
2 errors
Errors compiling TesteJDBC."

here's the relevant code:

try {
/*... */
Class.forName("com.mysql.jdbc.Driver").newInstance();
/*... */
}
i'm using winxp pro, jsdk1.4.1_02 and all the windows updates...
I tried to mount filesystem and also tried the tutorial on java where i define the classpath, in dos... but that didnt work too.
Can someone help me please?
remember i'm very new to this...
thanks in advance
 
Code:
class myjdbc
      {
      public static void main(String args[])
       {   
	String username;
	String password;
	String url;
	String dropString;
	String createString;

	// ---- configure START
	username = "root";
	password = "";
	// The URL that will connect to TECFA's MySQL server
        // Syntax: jdbc:TYPE:machine:port/DB_NAME
        //url = "jdbc:mysql://localhost:1433/testdb";
        url = "jdbc:mysql://localhost:3306/ptest";
	// ---- configure END

	// INSTALL/load the Driver (Vendor specific Code)
        
	try {
	    Class.forName("org.gjt.mm.mysql.Driver");
	} catch(java.lang.ClassNotFoundException e) {
	    System.err.print("ClassNotFoundException: ");
	    System.err.println(e.getMessage());
	}  
        
 Connection  con;
 
         try {    
	    // Establish Connection to the database at URL with usename and password
con = DriverManager.getConnection(url, username, password);

Statement stmt = con.createStatement ();

ResultSet rset = stmt.executeQuery ("select store_name from geography;");
while (rset.next ())
   {
   System.out.println (rset.getString (1));
   }
	    stmt.close();
            con.close();	    
        } 

	catch(SQLException ex) {
            System.err.println("==> SQLException: ");
	    while (ex != null) {
		System.out.println("Message:   " + ex.getMessage ());
		System.out.println("SQLState:  " + ex.getSQLState ());
		System.out.println("ErrorCode: " + ex.getErrorCode ());
		ex = ex.getNextException();
		System.out.println("");
	    }
        } 
 }// end main
}// end class
//you should have the following library from mysql-connector-java-3.0.9-stable.zip
//org.gjt.mm.mysql.Driver
//com.mysql.jdbc.Driver
// please mark this as for valuable post if the code suits you
 
thanks a lot for helping.
Well, i had my code imcomplete, so here it is complete.
It compiles ok, but when i try to run it says:

"java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at TesteJDBC.main(TesteJDBC.java:33)
"
line 33: "conn = DriverManager.getConnection(url);"
here's my code:
"
import java.sql.*;
import java.util.*;

public class TesteJDBC {

public static void main(String[] args) {
Connection conn = null;

try {
String url = "jdbc:mysql:localhost/Pedro?user=root&password=***";

String xt;
Statement st;

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

conn = DriverManager.getConnection(url);
st = conn.createStatement();
xt = "INSERT INTO Teste (id, valor) " + "VALUES(" + args[0] + ", " + args[1] + ")";
System.out.println("String exec:" + xt);
st.executeUpdate(xt);
}
catch (SQLException e ) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
System.out.println("Classe nao encontrada");
}
catch (InstantiationException e) {
System.out.println("Classe nao instanciada");
}
catch (IllegalAccessException e) {
System.out.println("Acesso Ilegal");
}
finally {
if(conn != null ) {
try { conn.close(); }
catch( Exception e ) {}
}
}
}

}
"
I guess my prob lies in installing the driver
Indeed i've downloaded mysql-connector-java-3.0.9-stable and even added it to classpath, i guess - look at my dos output:
"C:\j2sdk1.4.1_02\bin>java -classpath C:\j2sdk1.4.1_02\jre\lib\ext\driver
Usage: java [-options] class [args...]
(to execute a class)
or java -jar [-options] jarfile [args...]
(to execute a jar file)

where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-cp -classpath <directories and zip/jar files separated by ;>
set search path for application classes and resources
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-showversion print product version and continue
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
&quot;
inside &quot;driver&quot; folder i've those two folders: &quot;com&quot; and &quot;org&quot;. So, using Sun One Studio 4 update 1 i mounted filesystem, added the .jar to the ext folder of my java installation folder...dont know what else can i do...i also googled installing mysql drivers and read the readme driver and the help of the ide. I further read the classpath pages in the sun/java tut...

As to your code i get the following eerrors messages when compiling:
&quot;
myjdbc.java [30:1] cannot resolve symbol
symbol : class Connection
location: class myjdbc
Connection con;
^
myjdbc.java [34:1] cannot resolve symbol
symbol : variable DriverManager
location: class myjdbc
con = DriverManager.getConnection(url, username, password);
^
myjdbc.java [36:1] cannot resolve symbol
symbol : class Statement
location: class myjdbc
Statement stmt = con.createStatement ();
^
myjdbc.java [38:1] cannot resolve symbol
symbol : class ResultSet
location: class myjdbc
ResultSet rset = stmt.executeQuery (&quot;select store_name from geography;&quot;);
^
myjdbc.java [47:1] cannot resolve symbol
symbol : class SQLException
location: class myjdbc
catch(SQLException ex) {
^
5 errors

&quot;
Can you please give me a hand on this?
thanks in advance again
 
OK, get a new console window up.
Type &quot;edit setPaths.bat&quot;
Type the following into the edit file:
Code:
set JAVA_HOME=C:\j2sdk1.4.1_02&quot;
set PATH=%PATH%;%JAVA_HOME%\bin;
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%\jre\lib;%JAVA_HOME%\lib

rem point a variable at you mysql driver jar file
rem you'l  have to edit the path /drvier name as appropriate
set MYSQL_DRIVER=C:\mysql\where\ever\mysql-connector-java-3.0.9-stable.jar
set CLASSPATH=%CLASSPATH%;%MYSQL_DRIVER%;

Then save the file, return to the console and run the batch file so &quot;setPaths&quot;.

Then recompile your code, and try to run it again.
 
thanks for your help, sedj!
my bath file:
&quot;
set JAVA_HOME=C:\j2sdk1.4.1_02
set PATH=%PATH%;%JAVA_HOME%\bin
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%\jre\lib;%JAVA_HOME%\lib
set MYSQL_DRIVER=C:\Documents and Settings\Rafael\Os meus

documentos\DriverJavaMySql\mysql-connector-java-3.0.9-stable\mysql-connector-java-3.0.9-stable-bin.jar
set CLASSPATH=%CLASSPATH%;%MYSQL_DRIVER%
&quot;
note i didnt use the &quot;&quot;&quot;&quot; thing at the end of lines, but i also tried with that at every line end too.

then i get this message:
&quot;
C:\Documents and Settings\Rafael>set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\j2sdk1.4.1_02\bin;C:\j2sdk1.4.1_02
\bin;;C:\j2sdk1.4.1_02\bin;

C:\Documents and Settings\Rafael>set CLASSPATH=;C:\j2sdk1.4.1_02\jre\lib;C:\j2sdk1.4.1_02\lib;C:\Documents and Settings\Rafael\Os meus;
;C:\j2sdk1.4.1_02\jre\lib;C:\j2sdk1.4.1_02\lib

C:\Documents and Settings\Rafael>set MYSQL_DRIVER=C:\Documents and Settings\Rafael\Os meus

C:\Documents and Settings\Rafael>documentos\DriverJavaMySql\mysql-connector-java-3.0.9-stable\mysql-connector-java-3.0.9-stable-bin.jar

O sistema não conseguiu localizar o caminho especificado.

****Means system cant find the path&quot;****

C:\Documents and Settings\Rafael>set CLASSPATH=;C:\j2sdk1.4.1_02\jre\lib;C:\j2sdk1.4.1_02\lib;C:\Documents and Settings\Rafael\Os meus;
;C:\j2sdk1.4.1_02\jre\lib;C:\j2sdk1.4.1_02\lib;C:\Documents and Settings\Rafael\Os meus;
C:\Documents and Settings\Rafael>
&quot;

What am i doing wrong?
thanks again
 
i changed a few things:
my code:
&quot;import java.sql.*;
import java.util.*;

public class TesteJDBC {

public static void main(String[] args) {
Connection conn = null;

try {
// mysql-connector-j-2.0.14-bin.jar tem que estar na CLASSPATH
//String url = &quot;jdbc:mysql://localhost/pedro&quot;; // jdbc:mysql://localhost/Pedro?user=xxxx&password=yyyy caso
// usassemos controlo de acessos

String url = &quot;jdbc:mysql:localhost:8280/Pedro?user=root&password=***&quot;;


String xt;
Statement st;

Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance();

conn = DriverManager.getConnection(url);
st = conn.createStatement();
xt = &quot;INSERT INTO Teste (id, valor) &quot; + &quot;VALUES(&quot; + args[0] + &quot;, &quot; + args[1] + &quot;)&quot;;
System.out.println(&quot;String exec:&quot; + xt);
st.executeUpdate(xt);
}
catch (SQLException e ) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
System.out.println(&quot;Classe nao encontrada&quot;);
}
catch (InstantiationException e) {
System.out.println(&quot;Classe nao instanciada&quot;);
}
catch (IllegalAccessException e) {
System.out.println(&quot;Acesso Ilegal&quot;);
}
finally {
if(conn != null ) {
try { conn.close(); }
catch( Exception e ) {}
}
}
}

}

&quot;

now my b.bat:
&quot;
set JAVA_HOME=C:\j2sdk1.4.1_02;
set PATH=%PATH%;%JAVA_HOME%\bin;
set CLASSPATH=%CLASSPATH%;%JAVA_HOME%\jre\lib;%JAVA_HOME%\lib;
set MYSQL_DRIVER=C:\j2sdk1.4.1_02\jre\lib\ext\mysql-connector-java-3.0.9-stable-bin.jar;
set CLASSPATH=%CLASSPATH%;%MYSQL_DRIVER%;
&quot;

now i get no special msg from dos.
i recompiled and whe i run a get the very same msg:
&quot;java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at TesteJDBC.main(TesteJDBC.java:33)
&quot;
i'm sorry but i cant figure a solution...

ha... one more thing: i'm not sure about the batabase url and port number too...
thanks again
 
OK, a couple of things :

When setting paths in DOS, if there are any spaces, you must include quote marks so :

C:\java\&quot;my stuff&quot;\abc

or

&quot;C:\java\my stuff\abc&quot;

are both valid.


The MYSQL connection string should look like this (for the example test db that comes with mysql) :

&quot;jdbc:mysql://127.0.0.1:3306/test&quot;

 
i guess i got it!!
missing // in url:
now i've it like this:
String url = &quot;jdbc:mysql://localhost/Pedro?user=root&password=***&quot;;
no more missing driver :)))
thanks to everyone who helped me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top