The Artima Developer Community
Sponsored Link

Java Answers Forum
Trouble setting up MySQL JDBC

4 replies on 1 page. Most recent reply: Nov 14, 2002 3:21 PM by Ryan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Ryan

Posts: 3
Nickname: tank1979
Registered: Nov, 2002

Trouble setting up MySQL JDBC Posted: Nov 14, 2002 1:37 PM
Reply to this message Reply
Advertisement
I am having problems getting the MySQL Connector/J set up. I have followed instructions and cannot connect. Can anyone see what I have done wrong?

This is my code:
-------------------------------------------------------
import java.sql.*;

class TestDBConnection {

public static void main(String[] arg) {
// My host computer is dara, the database I want
// is test
String url = "jdbc:mysql://dara/test";
String driver = "com.mysql.jdbc.Driver";

String query = "SELECT * FROM Test WHERE number = 136";

try {
//Load the jdbc-odbc driver
Class.forName(driver); //.newInstance();

// Attempt a connection to a driver
Connection con =
DriverManager.getConnection(url, "user", "password");

// Create a statement object so we can submit SQL statements
// the driver
Statement stmt = con.createStatement();

// Submit a query
ResultSet rs = stmt.executeQuery(query);

// Display all results from the result set
printResultSet(rs);

rs.close();
stmt.close();
con.close();
} catch(SQLException e) {
while(e != null) {
System.out.println("SQL Exception: " +
e.getMessage());
e.printStackTrace();
e = e.getNextException();
}
} catch(java.lang.Exception e) {
e.printStackTrace();
}
}

private static void printResultSet(ResultSet rs) throws SQLException {
int numCols = rs.getMetaData().getColumnCount();

while(rs.next()) {
for(int i = 1; i <= numCols; i++) {
System.out.print(rs.getString(i) + " | " );
}
System.out.println();
}
}
}

--------------------------------------------------
Here is the Exception and the stack trace I get:

SQL Exception: Driver not found for URL: jdbc:mysql://dara/test
java.sql.SQLException: Driver not found for URL: jdbc:mysql://dara/test
at 0x4027e15f: java.lang.Throwable.Throwable(java.lang.String) (/usr/lib/libgcj.so.3)
at 0x402710d2: java.lang.Exception.Exception(java.lang.String) (/usr/lib/libgcj.so.3)
at 0x40313294: java.sql.SQLException.SQLException(java.lang.String, java.lang.String, int) (/usr/lib/libgcj.so.3)
at 0x40313244: java.sql.SQLException.SQLException(java.lang.String) (/usr/lib/libgcj.so.3)
at 0x40313102: java.sql.DriverManager.getConnection(java.lang.String, java.util.Properties) (/usr/lib/libgcj.so.3)
at 0x4031303a: java.sql.DriverManager.getConnection(java.lang.String, java.lang.String, java.lang.String) (/usr/lib/libgcj.so.3)
at 0x4039a347: ffi_call_SYSV (/usr/lib/libgcj.so.3)
at 0x4039a307: ffi_raw_call (/usr/lib/libgcj.so.3)
at 0x40245528: _Jv_InterpMethod.continue1(_Jv_InterpMethodInvocation) (/usr/lib/libgcj.so.3)
at 0x40245e34: _Jv_InterpMethod.run(ffi_cif, void, ffi_raw, _Jv_InterpMethodInvocation) (/usr/lib/libgcj.so.3)
at 0x40243424: _Jv_InterpMethod.run_normal(ffi_cif, void, ffi_raw, void) (/usr/lib/libgcj.so.3)
at 0x4039a1bc: ?? (??:0)
at 0x40258308: gnu.gcj.runtime.FirstThread.call_main() (/usr/lib/libgcj.so.3) at 0x402c30b1: gnu.gcj.runtime.FirstThread.run() (/usr/lib/libgcj.so.3)
at 0x40264fdc: _Jv_ThreadRun(java.lang.Thread) (/usr/lib/libgcj.so.3)
at 0x4023178c: _Jv_RunMain(java.lang.Class, byte const, int, byte const, boolean) (/usr/lib/libgcj.so.3)
at 0x08048900: ?? (??:0)
at 0x420158d4: ?? (??:0)
at 0x080486c1: ?? (??:0)

Thanks


greg

Posts: 6
Nickname: sputnik
Registered: Sep, 2002

Re: Trouble setting up MySQL JDBC Posted: Nov 14, 2002 1:55 PM
Reply to this message Reply
> Here is the Exception and the stack trace I get:
>
> SQL Exception: Driver not found for URL:
> jdbc:mysql://dara/test
> java.sql.SQLException: Driver not found for URL:
> jdbc:mysql://dara/test
> at 0x4027e15f:

bada bing! an error message with a useful message shocker! ;-)

your mysql driver is not set up properly. the driver file needs to live in the /lib directory of your JDK installation. that location's off the top of my head so you're better off checking the documentation that came with your driver for the exact location.

Ryan

Posts: 3
Nickname: tank1979
Registered: Nov, 2002

Re: Trouble setting up MySQL JDBC Posted: Nov 14, 2002 2:18 PM
Reply to this message Reply
I put the driver jar file in $JAVA_HOME/jre/lib/ext, which is where the documentation says to put it and I have also tried to just put the driver jar in my classpath. Both give the same error.

greg

Posts: 6
Nickname: sputnik
Registered: Sep, 2002

Re: Trouble setting up MySQL JDBC Posted: Nov 14, 2002 2:59 PM
Reply to this message Reply
> I put the driver jar file in $JAVA_HOME/jre/lib/ext, which
> is where the documentation says to put it and I have also
> tried to just put the driver jar in my classpath. Both
> give the same error.

ah, try looking at the URL used to make the connection. you're using the typical ODBC format:

DriverManager.getConnection(URL, username, password).

MySQL connections need to be made with a single URL that includes the username and password. thus:

ConnURL="jdbc:mysql://machine/dbname?user=your_username&password=your_pas sword";
DriverManager.getConnection(ConnURL);

try that!

Ryan

Posts: 3
Nickname: tank1979
Registered: Nov, 2002

Re: Trouble setting up MySQL JDBC Posted: Nov 14, 2002 3:21 PM
Reply to this message Reply
I put everything as one url and still got the same error message.

Flat View: This topic has 4 replies on 1 page
Topic: Need help on another complicated question.. Previous Topic   Next Topic Topic: help i need to create the class

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use