A simple JDBC program

I am testing a simple JDBC program, but doesn’t show any result.(just got prompt back) Am I missing something? All database specifications are correct.
-----------
CREATE TABLE BOOKS (
 TITLE VARCHAR(255),
 AUTHOR VARCHAR(255)
 );

---------------
import java.sql.*;
import java.util.*;

public class JDBCLibrary{

  private static final String DRIVER= "com.mysql.jdbc.Driver";
  private static final String DB_URL = "jdbc:mysql://webdev.apl.jhu.edu/library";
  private static final String USER = "lib_user";
  private static final String PASSWORD = "jhu481";

  private Connection conn;

  public void JDBCConnector() throws ClassNotFoundException, SQLException{
    conn = createConnection();
  }

  private Connection createConnection() throws ClassNotFoundException, SQLException{
    Class.forName(DRIVER) ;
    Connection conneciton = DriverManager.getConnection(DB_URL, USER, PASSWORD) ;

    return conneciton;
  }

  public synchronized void excuteQuery() throws SQLException{
    final String QUERY =
        "SELECT TITLE, AUTHOR FROM BOOKS";
    Statement statement = conn.createStatement() ;
    ResultSet results = statement.executeQuery(QUERY);
    System.out.println("Books: ");

    while(results.next()){
      System.out.println("Book: "+results.getString(1) );
      System.out.println("Author: "+results.getString(2));
    }
    results.close();
    statement.close();
  }
  public synchronized void close(){
    if(conn != null){
      try{
        conn.close();
      }catch(SQLException e){
        System.out.println(e);
      }
    }
  }

  public static void main(String[] args){
    JDBCLibrary application = new JDBCLibrary();
  }
}
------------------
When I run this, do I need any args?
dkim18Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
> When I run this, do I need any args?

no
0
objectsCommented:
you never call the mewthod:


public static void main(String[] args){
    JDBCLibrary application = new JDBCLibrary();
    application.executeQuery();
  }
0
objectsCommented:
>  public void JDBCConnector() throws ClassNotFoundException, SQLException{
>    conn = createConnection();
>  }

that should be your constructor:

  public JDBCLibrary() throws ClassNotFoundException, SQLException{
    conn = createConnection();
  }
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

objectsCommented:
which also means you'll need to handle exception in main, easiest is:

  public static void main(String[] args) throws Exception {
    JDBCLibrary application = new JDBCLibrary();
  }
0
dkim18Author Commented:
Sorry for not catchinig that.
I changed my main like below, but I am not having any result.

public static void main(String[] args){
    JDBCLibrary application = new JDBCLibrary();
   try{
      application.JDBCConnector();
      application.excuteQuery();
    }catch (SQLException e){}
    catch (ClassNotFoundException e){}
  }
}
0
objectsCommented:
>    }catch (SQLException e){}
>    catch (ClassNotFoundException e){}

Don't ignore exceptions, or you won't know when something goes wrong :)

    }catch (SQLException e){ e.printStackTrace(); }
    catch (ClassNotFoundException e){ e.printStackTrace(); }
0
dkim18Author Commented:
I changed all and now I have "ClassNotFoundException:com.mysql.jdbc.Driver"
Is that mean there is no mysql driver?
------------------------------
import java.sql.*;
import java.util.*;

public class JDBCLibrary{

  private static final String DRIVER= "com.mysql.jdbc.Driver";
  private static final String DB_URL = "jdbc:mysql://webdev.apl.jhu.edu/library";
  private static final String USER = "lib_user";
  private static final String PASSWORD = "jhu481";

  private Connection conn;

  public JDBCLibrary() throws ClassNotFoundException, SQLException{
    conn = createConnection();
  }

  private Connection createConnection() throws ClassNotFoundException, SQLException{
    Class.forName(DRIVER) ;
    Connection conneciton = DriverManager.getConnection(DB_URL, USER, PASSWORD) ;

    return conneciton;
  }

  public synchronized void excuteQuery() throws SQLException{
    final String QUERY =
        "SELECT TITLE, AUTHOR FROM BOOKS";
    Statement statement = conn.createStatement() ;
    ResultSet results = statement.executeQuery(QUERY);
    System.out.println("Books: ");

    while(results.next()){
      System.out.println("Book: "+results.getString(1) );
      System.out.println("Author: "+results.getString(2));
    }
    results.close();
    statement.close();
  }
  public synchronized void close(){
    if(conn != null){
      try{
        conn.close();
      }catch(SQLException e){
        System.out.println(e);
      }
    }
  }

  public static void main(String[] args)throws Exception {
    JDBCLibrary application = new JDBCLibrary();
      application.excuteQuery();
  }
}
0
objectsCommented:
> Is that mean there is no mysql driver?

yes, you need to add the driver jar to your classpath (or copy it into your 'ext' directory).
0
dkim18Author Commented:
I downloaded MySql and under “mysql-connector-java-3.0.9-stable” folder there was a “mysql-connector-java-3.0.9-stable-bin” jar file and I changed the name to “mysql.jar”

Now, I have JDBCLibrary.java under home_dir/src/dkim18/jdbc/ and my JDBCLibrary.class is under home_dir/classes/dkim18/jdbc/.
I placed mysql.jar file under home_dir/classes/ and this is my run.bat file.

java -classpath "classes/mysql.jar" classes.dkim18.jdbc.JDBCLibrary dkim18/classes/dkim18/jdbc/ (all in one line)

Now, I have NoClassDefFoundError.

0
objectsCommented:
you need to append the jar file to your classpath, not replace the classpath with it.
0
dkim18Author Commented:
I tried many different way, but wan't succesful.(I have trouble with classpath all the time.)
How do I do it?
0
objectsCommented:
I'd suggest just copy the jar into your 'ext' file.

0
objectsCommented:
that should have said 'ext' folder :)
0
objectsCommented:
java -classpath %classpath%;classes/mysql.jar  .....
0
dkim18Author Commented:
I tried this:

java -classpath %classpath%;classes/mysql.jar classes dkim18.jdbc.JDBCLibrary  

but didn't work.
I have the following error msg.

classes/mysql.jar: cannot excute

Is that mean there is something wrong with mysql.jar or location is not right?
0
objectsCommented:
specify the absolute location of the jar instead of the relative.

also check the contents of the jar with:

jar tvf mysql.jar
0
dkim18Author Commented:
classes dir is under my home_dir.
mysql.jar contains a whole bunch of .class files
0
objectsCommented:
> java -classpath %classpath%;classes/mysql.jar classes dkim18.jdbc.JDBCLibrary  

not sure what classes is intended for before the classname but it shopuldn't be there:

java -classpath %classpath%;classes/mysql.jar dkim18.jdbc.JDBCLibrary

This command will have to be run from your home irectory unless you specify absolute locations.
0
dkim18Author Commented:
java -classpath %classpath%;classes/mysql.jar dkim18.jdbc.JDBCLibrary
didn't work and my run.bat file that contains above line is under my home_dir.
0
objectsCommented:
what error ?
0
objectsCommented:
is your classes directory included in your classpath?

if not try:
java -classpath %classpath%;classes;classes/mysql.jar dkim18.jdbc.JDBCLibrary
0
dkim18Author Commented:
java -classpath %classpath%;classes;classes/mysql.jar dkim18.jdbc.JDBCLibrary
didn't work either.

I have the following error msg.

classes/mysql.jar: cannot excute
classes/mysql.jar: cannot excute
dkim18.jabc.JDBCLibrary: not found
0
objectsCommented:
try:

java -classpath "%classpath%;classes;classes/mysql.jar" dkim18.jdbc.JDBCLibrary

and can you post the output of the following:

echo %classpath%

actually better check what OS you are using :)
0
dkim18Author Commented:
java -classpath "%classpath%;classes;classes/mysql.jar" dkim18.jdbc.JDBCLibrary

I have "NoClassDefFoundError: dkim18/jabc/JDBCLibrary

the output of the echo %classpath% is:
%classpath%

I  am using sun solaris.
p.s.
java -classpath %classpath%;classes;classes/mysql.jar dkim18.jdbc.JDBCLibrary

for this try, I had
classes: cannot excute
classes/mysql.jar: cannot excute
0
objectsCommented:
> I  am using sun solaris.

Many apogogies, been dealing with too many clients using windows.

try this:

java -classpath classes;classes/mysql.jar dkim18.jdbc.JDBCLibrary
0
dkim18Author Commented:
I am sorry...the following didn't work either.
java -classpath classes;classes/mysql.jar dkim18.jdbc.JDBCLibrary


I got "classes/mysql.jar: cannot excute" error

0
objectsCommented:
Sorry my brain still stuck in Windoze rut :-(

java -classpath classes:classes/mysql.jar dkim18.jdbc.JDBCLibrary
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkim18Author Commented:
Finally, it worked!!!
Thank you your continuous help.
Classpath is always confusing to me. I want to know this subject for sure...
0
objectsCommented:
0
objectsCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.