JDBC problem

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
LVL 4
berg1375Asked:
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.

Ravindra76Commented:
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

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
berg1375Author Commented:
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
Ravindra76Commented:

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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

berg1375Author Commented:
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
Ravindra76Commented:
Hi berg1375,

comment this line or delete

assetResults.close();

like

//assetResults.close();

Because alredy, resultset became null
0
berg1375Author Commented:
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
Ravindra76Commented:
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
berg1375Author Commented:
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
Ravindra76Commented:
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
Ravindra76Commented:

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
berg1375Author Commented:
Did you change anything? Is my machine just dumb as a rock? Please let me know.

berg
0
Ravindra76Commented:
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
berg1375Author Commented:
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
Ravindra76Commented:

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
Ravindra76Commented:
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
Ravindra76Commented:

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
berg1375Author Commented:
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
Ravindra76Commented:
Hi berg1375,


Some problem in your system.

BEst of luck
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.