• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to connect to mysql database ?

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}


}

}
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
teja dharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

teja tej wrote:


Please check your private messages for an important administrative matter
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic