I am a novice programer :
It looks to me that the sql driver is in mysql-connector-java-5.0.8.zip
and my authors.class in
H:\javaCode\MySqlDB\src>
The error that I get is
H:\javaCode\MySqlDB\src>java -classpath .;
C:\mysql-connector-java-5.0.8\mysql-c
onnector-java-5.0.8-bin.jar authors
Exception in thread "main" java.lang.NoClassDefFoundError:
C:\mysql-connector-ja
va-5/0/8\mysql-connector-java-5/0/8-bin/jar
Caused by: java.lang.ClassNotFoundException:
C:\mysql-connector-java-5.0.8\mysql
-connector-java-5.0.8-bin.jar
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class:
C:\mysql-connector-java-5.0.8\mysql-connector-jav
a-5.0.8-bin.jar. Program will exit.
H:\javaCode\MySqlDB\src>java authors
java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at authors.main(authors.java:28)
java.lang.NullPointerException
at authors.main(authors.java:57)
H:\javaCode\MySqlDB\src>ls
authors.class authors.java
H:\javaCode\MySqlDB\src>java -classpath .;
C:\mysql-connector-java-5.0.8\mysql-c
onnector-java-5.0.8-bin.jar authors
On Fri, Oct 30, 2009 at 12:16 PM, kat Owen <[email protected]> wrote:
> Hi all,
>
> Can someone help me to with the ClassNotFoundException ? Thanks a bunch !!
>
>
> I am trying to write a simple program in JAVA that can connect to the db.
>
> 1. I want to first connect to MySql
> 2. Then create a db
> 3. create a table
> 4. write some data into the table and
> 5. finally retrieve them
>
> I am in fact stuck at step 1 itself.
>
> I downloaded the mysql package and isntalled it. I am able to login and run
> *.sql files and simple commands like SELECT are working. I am able to see
> the data in tables from command line.
>
> Then I wrote simple .java program where I try to connect to this db to do
> some work. I am running into ClassNotFoundException - seems like - my class
> path is not set correctly.
> This is what I have
>
>
> I ran books.sql to create a db - books
>
>
> mysql> show databases
> -> ;
> +--------------------+
> | Database |
> +--------------------+
> | information_schema |
> | books |
> | mysql |
> | test |
> +--------------------+
> 4 rows in set (0.00 sec)
> mysql>
>
>
> C:\mysql-connector-java-5.0.8\mysql-connector-java-5.0.8.zip
>
> my books.sql is in
>
>
> C:\Program Files\MySQL\MySQL Server 5.0\scripts\books.sql
>
>
>
>
> Then my authors.java is following
>
>
> *
>
> import
> *java.sql.Connection;*
>
> import
> *java.sql.Statement;*
>
> import
> *java.sql.DriverManager;*
>
> import
> *java.sql.ResultSet;*
>
> import
> **java.sql.ResultSetMetaData*;*
>
> import
> *java.sql.SQLException;
>
>
> *
>
> public
> **class* authors
>
> {
>
> *static* *final* String *DRIVER* = "com.mysql.jdbc.driver";
>
> *static* *final* String *DATABASE_URL* = "jdbc:mysql://localhost/books";
>
> *public* *static* *void* main(String args[])
>
> {
>
> Connection connection =
> *null*;
>
> Statement statement =
> *null*;
>
> ResultSet resultSet =
> *null*;
>
> *try* {
>
> // load the driver class
>
> Class.*forName*(
> *DRIVER*);
>
> // establish the connection
>
> connection = DriverManager.*getConnection*(
> *DATABASE_URL*,"root","root");
>
> // create statement for querying *databse*
>
> statement = connection.createStatement();
>
> // query database
>
> resultSet = statement.executeQuery(
> "SELECT authorID, firstName, lastName FROM authors");
>
> }
> *catch* (SQLException sqlException)
>
> {
>
> sqlException.printStackTrace();
>
> }
> *catch* (ClassNotFoundException classNotFound)
>
> {
>
> classNotFound.printStackTrace();
>
> }
>
> *finally*
>
> {
>
> *try* {
>
> resultSet.close();
>
> statement.close();
>
> connection.close();
>
> }
> *catch* (Exception e)
>
> {
>
> e.printStackTrace();
>
> }
>
> }
>
> }
>
> }
>
>
>
>
>
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---