?
Solved

JDBC problem

Posted on 1999-12-09
18
Medium Priority
?
309 Views
Last Modified: 2006-11-17
I am trying to conect to one of my Access97 databases, and am getting this error:

java.lang.ClassNotFoundException" Sun.Jdbc.JdbcOdbcDriver

Exception in thread "main" java.lang.NullPointerException
     at EssentialJDBC.getResultsByColumnName(EssentialJDBC.Java, compiled code)

     at EssentialJDBC.main(EssentialJDBD.Java:10)

Can someone tell me what is going on here and what I have to do to get this simple thing to work?

Here is the code:


import java.sql.*;

public class EssentialJDBC
{
     
   public static void main(String[] args)
   {
      EssentialJDBC SQLExample = new EssentialJDBC();

      SQLExample.getResultsByColumnName();
      SQLExample.getResultsByColumnPosition();
      SQLExample.getAllColumns();
      SQLExample.closeConnection();
   }

   public EssentialJDBC()
   {
     try
      {
         Class.forName("sun.jdbc.JdbcOdbcDriver");
         connection = DriverManager.getConnection(sourceURL);
         statement = connection.createStatement();
      }
      catch(SQLException sqle)
      {
         System.err.println("Error creating connection");
      }
      catch(ClassNotFoundException cnfe)
      {
         System.err.println(cnfe.toString());
      }
   }

void getResultsByColumnName()
   {
      try
      {
         ResultSet assetResults = statement.executeQuery(queryWildCard);
         int row = 0;

         while(assetResults.next())
           System.out.println("Row " + (++row) + ") "+ assetResults.getString("IDNum")+ " " +
                               assetResults.getString("AssetName"));
         assetResults.close();
      }
      catch (SQLException sqle)
      {
         System.err.println ("\nSQLException----------\n");
         System.err.println ("SQLState : " + sqle.getSQLState());
         System.err.println ("Message : " + sqle.getMessage());
      }
   }

void getResultsByColumnPosition()
   {
      try
      {
         ResultSet assetResults = statement.executeQuery(queryIDandName);

         int row = 0;
         while (assetResults.next())
         {
            System.out.println("\nRow " + (++row) + ") ");
            for(int i = 1 ; i<=3 ; i++)
               System.out.print((i>1?", ":" ")+assetResults.getString(i));
         }
       assetResults.close();
       }
     catch (SQLException sqle)
     {
       System.err.println ("\nSQLException----------\n");
       System.err.println ("SQLState : " + sqle.getSQLState());
       System.err.println ("Message : " + sqle.getMessage());
     }
   }


   void getAllColumns()
   {
      try
      {
         ResultSet assetResults = statement.executeQuery(queryWildCard);

         ResultSetMetaData metadata = assetResults.getMetaData();
         int columns = metadata.getColumnCount();
         int row = 0;
         while (assetResults.next())
         {
            System.out.print("\nRow " + (++row)  + ") ");
            for(int i = 1 ; i<=columns ; i++)
               System.out.print((i>1?", ":" ")+assetResults.getString(i));
         }

         assetResults.close();
      }
      catch (SQLException ex)
      {
        System.err.println("\nSQLException----------------\n");
        System.err.println("SQLState: " + ex.getSQLState());
        System.err.println("Message: " + ex.getMessage());
      }
   }

   //Close the connection
   void closeConnection()
   {
      if(connection != null)
      try
      {
         connection.close();
         connection = null;
      }
      catch (SQLException ex)
      {
         System.out.println("\nSQLException------------------\n");
         System.out.println("SQLState: " + ex.getSQLState());
         System.out.println("Message : " + ex.getMessage());
      }
   }
   
   Connection connection;
   Statement statement;
   String sourceURL = "jdbc:odbc:JavaConnectionTest";
   String queryIDandName = "SELECT AssetName, IDNum FROM tblAssetInfo";
   String queryWildCard = "SELECT * FROM tblAssetInfo";

}


Thank you
berg
0
Comment
Question by:berg1375
  • 11
  • 7
18 Comments
 
LVL 7

Accepted Solution

by:
Ravindra76 earned 120 total points
ID: 2268974
Hi for berg1375


>>
 Class.forName("sun.jdbc.JdbcOdbcDriver");

It is wrong

Replace the above line with

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Best of luck
0
 
LVL 4

Author Comment

by:berg1375
ID: 2276668
Sorry it has taken me so long to reply, but I am swamped with extracurricular tasks  :(

I changed the code like you suggested, and now I am getting a different error:

Error creating connection
Exception in thread "main" Java.Lang.NullPointerException at EssentialJDBC.getResultsByColumnName(EssentialJDBC.java, Compiled code)

at EssentialJDBC.main(EssetialJDBC.java:10)

I am new to Java, so I don't know exactly what is causing this.

Thanks
berg
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2276722

Hi,

Your tablke field names may cause problem.

Eg: Spelling

Once check spellings and once check
table fields and your rs.getString(i) and i values for that staemnt.

It is simple.

BEst of luck

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 4

Author Comment

by:berg1375
ID: 2276797
I have checked everything you suggested, and changed a few things I thought maybe wrong, but it has not changed the output of the program.

berg
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2276910
Hi berg1375,

comment this line or delete

assetResults.close();

like

//assetResults.close();

Because alredy, resultset became null
0
 
LVL 4

Author Comment

by:berg1375
ID: 2276935
It still does not like the program. I am beginning to wonder if there is a class missing, or it doesn't like the SQL statement. I have tried to used MS-SQL, and basic SQL, but neither seem to want to work.

If you can help me get this working, or if you have any links or URL's to an example of lenking to an Access db, and running SQL against it I will up the points you are already receiving.

berg
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2276950
Hi berg1375,


            for(int i = 1 ; i<=3 ; i++)
               System.out.print((i>1?", ":" ")+assetResults.getString(i));


replace this with


            for(int i = 1 ; i<3 ; i++)
               System.out.print((i>1?", ":" ")+assetResults.getString(i));

Best of luck
0
 
LVL 4

Author Comment

by:berg1375
ID: 2276970
Still a no go.

It doesn't seem to like the first getResult statement. Is there something wrong with it? Or is java just being difficult?


berg
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2276989
HI berg1375,

It is your working code on my systyem.


import java.sql.*;

public class EssentialJDBC
{

public static void main(String[] args)
{
EssentialJDBC SQLExample = new EssentialJDBC();

SQLExample.getResultsByColumnName();
SQLExample.getResultsByColumnPosition();
SQLExample.getAllColumns();
SQLExample.closeConnection();
}

public EssentialJDBC()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(sourceURL);
statement = connection.createStatement();
}
catch(SQLException sqle)
{
System.err.println("Error creating connection");
sqle.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe.toString());
}
}

void getResultsByColumnName()
{
try
{
ResultSet assetResults = statement.executeQuery(queryWildCard);
int row = 0;

while(assetResults.next())
System.out.println("Row " + (++row) + ") "+ assetResults.getString("IDNum")+ " " +
assetResults.getString("AssetName"));
//assetResults.close();
}
catch (SQLException sqle)
{
System.err.println ("\nSQLException----------\n");
System.err.println ("SQLState : " + sqle.getSQLState());
System.err.println ("Message : " + sqle.getMessage());
}
}

void getResultsByColumnPosition()
{
try
{
ResultSet assetResults = statement.executeQuery(queryIDandName);

int row = 0;
while (assetResults.next())
{
System.out.println("\nRow " + (++row) + ") ");
for(int i = 1 ; i<=3 ; i++)
System.out.print((i>1?", ":" ")+assetResults.getString(i));
}
assetResults.close();
}
catch (SQLException sqle)
{
System.err.println ("\nSQLException----------\n");
System.err.println ("SQLState : " + sqle.getSQLState());
System.err.println ("Message : " + sqle.getMessage());
}
}


void getAllColumns()
{
try
{
ResultSet assetResults = statement.executeQuery(queryWildCard);

ResultSetMetaData metadata = assetResults.getMetaData();
int columns = metadata.getColumnCount();
int row = 0;
while (assetResults.next())
{
System.out.print("\nRow " + (++row) + ") ");
for(int i = 1 ; i<=columns ; i++)
System.out.print((i>1?", ":" ")+assetResults.getString(i));
}

assetResults.close();
}
catch (SQLException ex)
{
System.err.println("\nSQLException----------------\n");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
}
}

//Close the connection
void closeConnection()
{
if(connection != null)
try
{
connection.close();
connection = null;
}
catch (SQLException ex)
{
System.out.println("\nSQLException------------------\n");
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message : " + ex.getMessage());
}
}

Connection connection;
Statement statement;
String sourceURL = "jdbc:odbc:JavaConnectionTest";
String queryIDandName = "SELECT AssetName, IDNum FROM tblAssetInfo";
String queryWildCard = "SELECT * FROM tblAssetInfo";

}

Enjoy,

Best of luck.
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2276997

HI berg1375,

import java.sql.*;

public class EssentialJDBC
{

public static void main(String[] args)
{
EssentialJDBC SQLExample = new EssentialJDBC();

SQLExample.getResultsByColumnName();
SQLExample.getResultsByColumnPosition();
SQLExample.getAllColumns();
SQLExample.closeConnection();
}

public EssentialJDBC()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(sourceURL);
statement = connection.createStatement();
}
catch(SQLException sqle)
{
System.err.println("Error creating connection");
sqle.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe.toString());
}
}

void getResultsByColumnName()
{
try
{
ResultSet assetResults = statement.executeQuery(queryWildCard);
int row = 0;

while(assetResults.next())
System.out.println("Row " + (++row) + ") "+ assetResults.getString("IDNum")+ " " +
assetResults.getString("AssetName"));
//assetResults.close();
}
catch (SQLException sqle)
{
System.err.println ("\nSQLException----------\n");
System.err.println ("SQLState : " + sqle.getSQLState());
System.err.println ("Message : " + sqle.getMessage());
}
}

void getResultsByColumnPosition()
{
try
{
ResultSet assetResults = statement.executeQuery(queryIDandName);

int row = 0;
while (assetResults.next())
{
System.out.println("\nRow " + (++row) + ") ");
for(int i = 1 ; i<3 ; i++)
System.out.print((i>1?", ":" ")+assetResults.getString(i));
}
assetResults.close();
}
catch (SQLException sqle)
{
System.err.println ("\nSQLException----------\n");
System.err.println ("SQLState : " + sqle.getSQLState());
System.err.println ("Message : " + sqle.getMessage());
}
}


void getAllColumns()
{
try
{
ResultSet assetResults = statement.executeQuery(queryWildCard);

ResultSetMetaData metadata = assetResults.getMetaData();
int columns = metadata.getColumnCount();
int row = 0;
while (assetResults.next())
{
System.out.print("\nRow " + (++row) + ") ");
for(int i = 1 ; i<=columns ; i++)
System.out.print((i>1?", ":" ")+assetResults.getString(i));
}

assetResults.close();
}
catch (SQLException ex)
{
System.err.println("\nSQLException----------------\n");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
}
}

//Close the connection
void closeConnection()
{
if(connection != null)
try
{
connection.close();
connection = null;
}
catch (SQLException ex)
{
System.out.println("\nSQLException------------------\n");
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message : " + ex.getMessage());
}
}

Connection connection;
Statement statement;
String sourceURL = "jdbc:odbc:JavaConnectionTest";
String queryIDandName = "SELECT AssetName, IDNum FROM tblAssetInfo";
String queryWildCard = "SELECT * FROM tblAssetInfo";

}

the above code is working on my system.

Enjoy


0
 
LVL 4

Author Comment

by:berg1375
ID: 2277025
Did you change anything? Is my machine just dumb as a rock? Please let me know.

berg
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2277070
Yaa,

I changed two things.

1.for loop i <=3 to i < 3 in getResultByColumnPosition;

2.commenting

//assetResults.close();

the line as  i told earlier

Any problems?????????

Best of luck

0
 
LVL 4

Author Comment

by:berg1375
ID: 2277091
Lots of problems......

I pasted the code into a new notepad, and changed all to EssentialJDBC2. I complied it (fine), than ran it (java EssentialJDBC2).....I now have eight errors. Way too many to type in here. Is there something wrong with my system?

berg
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2277168

Hi berg1375,

Dont' save as EssentialJdbc2.java

Save as EssentialJDBC.java only.

Rename your current wrong code as like
EssentialJDBC1.java or EssentialJDBC2.java.

It is the correct code.

So only save as EssentialJDBC.java only.

BEst of luck
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2277185
Hi berg1375,

1. Two options for you.

If you follow my previous code,
run as
a. Save my code as EssentialJDBC.java
b. type
  java EssentialJDBC  
  in dos prompt.

2. If you change, my code as
EssentialJDBC.java

a. Save this in EssentialJDBC2.java

Now i changed the My file name as
EssentialJDBC2.java


import java.sql.*;

public class EssentialJDBC2
{

public static void main(String[] args)
{
EssentialJDBC2 SQLExample = new EssentialJDBC2();

SQLExample.getResultsByColumnName();
SQLExample.getResultsByColumnPosition();
SQLExample.getAllColumns();
SQLExample.closeConnection();
}

public EssentialJDBC2()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(sourceURL);
statement = connection.createStatement();
}
catch(SQLException sqle)
{
System.err.println("Error creating connection");
sqle.printStackTrace();
}
catch(ClassNotFoundException cnfe)
{
System.err.println(cnfe.toString());
}
}

void getResultsByColumnName()
{
try
{
ResultSet assetResults = statement.executeQuery(queryWildCard);
int row = 0;

while(assetResults.next())
System.out.println("Row " + (++row) + ") "+ assetResults.getString("IDNum")+ " " +
assetResults.getString("AssetName"));
//assetResults.close();
}
catch (SQLException sqle)
{
System.err.println ("\nSQLException----------\n");
System.err.println ("SQLState : " + sqle.getSQLState());
System.err.println ("Message : " + sqle.getMessage());
}
}

void getResultsByColumnPosition()
{
try
{
ResultSet assetResults = statement.executeQuery(queryIDandName);

int row = 0;
while (assetResults.next())
{
System.out.println("\nRow " + (++row) + ") ");
for(int i = 1 ; i<3 ; i++)
System.out.print((i>1?", ":" ")+assetResults.getString(i));
}
assetResults.close();
}
catch (SQLException sqle)
{
System.err.println ("\nSQLException----------\n");
System.err.println ("SQLState : " + sqle.getSQLState());
System.err.println ("Message : " + sqle.getMessage());
}
}


void getAllColumns()
{
try
{
ResultSet assetResults = statement.executeQuery(queryWildCard);

ResultSetMetaData metadata = assetResults.getMetaData();
int columns = metadata.getColumnCount();
int row = 0;
while (assetResults.next())
{
System.out.print("\nRow " + (++row) + ") ");
for(int i = 1 ; i<=columns ; i++)
System.out.print((i>1?", ":" ")+assetResults.getString(i));
}

assetResults.close();
}
catch (SQLException ex)
{
System.err.println("\nSQLException----------------\n");
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("Message: " + ex.getMessage());
}
}

//Close the connection
void closeConnection()
{
if(connection != null)
try
{
connection.close();
connection = null;
}
catch (SQLException ex)
{
System.out.println("\nSQLException------------------\n");
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message : " + ex.getMessage());
}
}

Connection connection;
Statement statement;
String sourceURL = "jdbc:odbc:JavaConnectionTest";
String queryIDandName = "SELECT AssetName, IDNum FROM tblAssetInfo";
String queryWildCard = "SELECT * FROM tblAssetInfo";

}


b.Run this as

java EssentialJDBC2
 in dos prompt
You will get result

Best of luck


0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2277213

HI berg1375,

 IF you have erros, post it.

 I will see tomorrow.

 I am leaving now.

 For me ,it is night ( 9 pm).

 Good night.
0
 
LVL 4

Author Comment

by:berg1375
ID: 2277240
Good evening!

I have tried both ways and am getting the same error as the beginning.

I am beginning to thin there is a missing component in my machine, or something.

berg
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2279227
Hi berg1375,


Some problem in your system.

BEst of luck
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month3 days, 17 hours left to enroll

599 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