Thank You Kitty
I had a typo. in the com.mysql.jdbc.driver it was supposed to be com.mysql.jdbc.*D*river instead I had com.mysql.jdbc.driver. ~Thanks again, Kat 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 -~----------~----~----~----~------~----~------~--~---
