wood burning stoves*
The moose likes JDBC and the fly likes how to connect to mysql database ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to connect to mysql database ?" Watch "how to connect to mysql database ?" New topic
Author

how to connect to mysql database ?

teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
hi friends i am new to jdbc .I want to connect my program to mysql5.0 database.
I compiled the below program from the c:\ drive. But at run time i am getting classnot found Exception:com.mysql.jdbc.Driver
and in the below program in the string dburl the hello is the database name which i created

import java.sql.*;
import javax.sql.*;
public class jdbcdemo
{

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String dbtime;
String dbUrl = "jdbc:mysql://localhost:3306/hello";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select * FROM users";

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while

con.close();
} //end try

catch(ClassNotFoundException e) {
e.printStackTrace();
}

catch(SQLException e) {
e.printStackTrace();
}


}

}

SCJP 5
srinivas srinivasmeenavalli
Ranch Hand

Joined: Jul 13, 2008
Posts: 65
Issue with com.mysql.jdbc.Driver classpath and Jar file.
MQSQL Driver class has to be in class path while executing your JDBC program.
Java databse connectivity jar contains interfaces like statement ,result set etc.
Each vendor will provide own implementation of JDBC . To execute oracle jdbc program you need to have jar file from oracle.
as i know above statment applies all of the databases.
here solutio is you have to include MQSQL Jar file in the classpath. Which IDE you are using ?
MYSQL Jar
All the Best
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
srinivas srinivasmeenavalli wrote:Issue with com.mysql.jdbc.Driver classpath and Jar file.
MQSQL Driver class has to be in class path while executing your JDBC program.
Java databse connectivity jar contains interfaces like statement ,result set etc.
Each vendor will provide own implementation of JDBC . To execute oracle jdbc program you need to have jar file from oracle.
as i know above statment applies all of the databases.
here solutio is you have to include MQSQL Jar file in the classpath. Which IDE you are using ?
MYSQL Jar
All the Best


I compiled the program by setting the class path like this
javac jdbcdemo.java -classpath "C:\Documents and Settings\teja\MyDocuments\mysql-connector-java-5.1.10\mysql-connector-java-5.1.10.jar"
I mean jarfile is present in C:\Documents and Settings\teja\MyDocuments\mysql-connector-java-5.1.10\mysql-connector-java-5.1.10.jar at compile time no error but at run time i am getting nullpoinerexception
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

teja dharma wrote: ...at compile time no error but at run time i am getting nullpoinerexception

Did you use the same set of required entries in the CLASSPATH when running as with compiling? Can you post the full stack trace?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
Vijitha Kumara wrote:
teja dharma wrote: ...at compile time no error but at run time i am getting nullpoinerexception

Did you use the same set of required entries in the CLASSPATH when running as with compiling? Can you post the full stack trace?

At compile time i used the command
javac jdbcdemo.java -classpath "C:\Documents and Settings\teja\MyDocuments\mysql-connector-java-5.1.10\mysql-connector-java-5.1.10.jar"

At runtime java jdbcdemo :
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

teja tej wrote:At runtime java jdbcdemo :

No, you should provide the same set of jars used to compile when running too ("java -cp <setofJARs> classname").
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
Vijitha Kumara wrote:
teja tej wrote:At runtime java jdbcdemo :

No, you should provide the same set of jars used to compile when running too ("java -cp <setofJARs> classname").

At runtime I gave the command java -cp "C:\Documents and Settings\teja\MyDocuments\mysql-connector-java-5.1.10\mysql-connector-java-5.1.10.jar" jdbcdemo
This time i got java.lang.NoClassDefFoundError:jdbcdemo
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

teja tej wrote:At runtime I gave the command java -cp "C:\Documents and Settings\teja\MyDocuments\mysql-connector-java-5.1.10\mysql-connector-java-5.1.10.jar" jdbcdemo
This time i got java.lang.NoClassDefFoundError:jdbcdemo

When you use classpath switch you have to point all the classes/jars which are required to run the application, because default/classpath entry in environment is ignored. So in this case you should append the path to the "jdbcdemo" (and any other classes required) to the "classpath" switch as well.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

teja tej wrote:

Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to connect to mysql database ?