Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

A simple JDBC program

Posted on 2003-12-07
30
Medium Priority
?
405 Views
Last Modified: 2008-02-01
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?
0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 19
  • 11
30 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9893286
> When I run this, do I need any args?

no
0
 
LVL 92

Expert Comment

by:objects
ID: 9893290
you never call the mewthod:


public static void main(String[] args){
    JDBCLibrary application = new JDBCLibrary();
    application.executeQuery();
  }
0
 
LVL 92

Expert Comment

by:objects
ID: 9893300
>  public void JDBCConnector() throws ClassNotFoundException, SQLException{
>    conn = createConnection();
>  }

that should be your constructor:

  public JDBCLibrary() throws ClassNotFoundException, SQLException{
    conn = createConnection();
  }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 9893302
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
 

Author Comment

by:dkim18
ID: 9893353
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
 
LVL 92

Expert Comment

by:objects
ID: 9893376
>    }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
 

Author Comment

by:dkim18
ID: 9893385
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
 
LVL 92

Expert Comment

by:objects
ID: 9893400
> 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
 

Author Comment

by:dkim18
ID: 9893711
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
 
LVL 92

Expert Comment

by:objects
ID: 9893747
you need to append the jar file to your classpath, not replace the classpath with it.
0
 

Author Comment

by:dkim18
ID: 9893836
I tried many different way, but wan't succesful.(I have trouble with classpath all the time.)
How do I do it?
0
 
LVL 92

Expert Comment

by:objects
ID: 9893843
I'd suggest just copy the jar into your 'ext' file.

0
 
LVL 92

Expert Comment

by:objects
ID: 9893846
that should have said 'ext' folder :)
0
 
LVL 92

Expert Comment

by:objects
ID: 9894134
java -classpath %classpath%;classes/mysql.jar  .....
0
 

Author Comment

by:dkim18
ID: 9896918
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
 
LVL 92

Expert Comment

by:objects
ID: 9899809
specify the absolute location of the jar instead of the relative.

also check the contents of the jar with:

jar tvf mysql.jar
0
 

Author Comment

by:dkim18
ID: 9899991
classes dir is under my home_dir.
mysql.jar contains a whole bunch of .class files
0
 
LVL 92

Expert Comment

by:objects
ID: 9900012
> 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
 

Author Comment

by:dkim18
ID: 9900099
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
 
LVL 92

Expert Comment

by:objects
ID: 9900130
what error ?
0
 
LVL 92

Expert Comment

by:objects
ID: 9900140
is your classes directory included in your classpath?

if not try:
java -classpath %classpath%;classes;classes/mysql.jar dkim18.jdbc.JDBCLibrary
0
 

Author Comment

by:dkim18
ID: 9900294
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
 
LVL 92

Expert Comment

by:objects
ID: 9900335
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
 

Author Comment

by:dkim18
ID: 9900419
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
 
LVL 92

Expert Comment

by:objects
ID: 9900434
> 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
 

Author Comment

by:dkim18
ID: 9900551
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
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 9900570
Sorry my brain still stuck in Windoze rut :-(

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

Author Comment

by:dkim18
ID: 9900640
Finally, it worked!!!
Thank you your continuous help.
Classpath is always confusing to me. I want to know this subject for sure...
0
 
LVL 92

Expert Comment

by:objects
ID: 9900671
0
 
LVL 92

Expert Comment

by:objects
ID: 9900672
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question