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.

Mick BarryJava DeveloperCommented:
> When I run this, do I need any args?

no
Mick BarryJava DeveloperCommented:
you never call the mewthod:


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

that should be your constructor:

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

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Mick BarryJava DeveloperCommented:
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();
  }
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){}
  }
}
Mick BarryJava DeveloperCommented:
>    }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(); }
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();
  }
}
Mick BarryJava DeveloperCommented:
> 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).
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.

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

Mick BarryJava DeveloperCommented:
that should have said 'ext' folder :)
Mick BarryJava DeveloperCommented:
java -classpath %classpath%;classes/mysql.jar  .....
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?
Mick BarryJava DeveloperCommented:
specify the absolute location of the jar instead of the relative.

also check the contents of the jar with:

jar tvf mysql.jar
dkim18Author Commented:
classes dir is under my home_dir.
mysql.jar contains a whole bunch of .class files
Mick BarryJava DeveloperCommented:
> 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.
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.
Mick BarryJava DeveloperCommented:
what error ?
Mick BarryJava DeveloperCommented:
is your classes directory included in your classpath?

if not try:
java -classpath %classpath%;classes;classes/mysql.jar dkim18.jdbc.JDBCLibrary
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
Mick BarryJava DeveloperCommented:
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 :)
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
Mick BarryJava DeveloperCommented:
> 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
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

Mick BarryJava DeveloperCommented:
Sorry my brain still stuck in Windoze rut :-(

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

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...
Mick BarryJava DeveloperCommented:
Mick BarryJava DeveloperCommented:
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.