Solved

What is wrong with this class

Posted on 2004-08-06
5
1,701 Views
Last Modified: 2012-06-27
Hi, I want to build a class connecting to ACCESS (I hate that tool) using odbc.  If I write it in a single class (in the main "function") I get no error, but If I put open_database, close database and browse database of putting all variable public instead of private.

I don't understand what is wrong with that class.  If somebody can help me, thank you!
this class is in the same file than other, could it be the problem ??? (I know in java, 1 class is 1 file) but not this time, lol

thank you.


import java.lang.*;
import java.net.URL;
import java.sql.*;

class connect_access
{
       private Connection dbConn = null;
      private Statement stmt;
    private ResultSet rs;

    connect_access(String query)
      {
          try
            {
                      Driver d = (Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
                       String URL = "jdbc:odbc:" + "testdatabase";
                       dbConn = DriverManager.getConnection(URL, "Admin", "");
                       System.out.println("db Connected...\n");          
                       // Java SQL objects...       
                       stmt = dbConn.createStatement();
                       rs = stmt.executeQuery(query);
      
                       
          }
          catch (Exception e)
            {
                 System.out.println("Whoops! Something went wrong. \n");
                 e.printStackTrace();
            }
      }

    public String get_data(String column)
      {

something = rs.movenext() //<- here I get a unhandled exception type SQLException I know something is not declared, but was'nt my code
            return("");
      }

      public void disconnect_access()
      {
            System.out.println("BYE");
          dbConn.close();  //<------------------------------------------------------------ Here I get a unhandled exception type SQLException
          System.out.println("db connection closed.");
      }

}
0
Comment
Question by:weeb0
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11738515
public void disconnect_access()
     {

        if(dbConn!=null)
        {
         dbConn.close();
          dbConn=null;
         }

     }


0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 50 total points
ID: 11738541
Oops

Should be

 public void disconnect_access()
     {
      try
     {
        if(dbConn!=null)
        {
         dbConn.close();
          dbConn=null;
         }
         }
         catch(Exception ex)
        {
            dbConn=null;
         }  

     }


 public String get_data(String column)
     {
try
{
something = rs.movenext() //<- here I get a unhandled exception type SQLException I know something is not declared, but was'nt my code
}
catch(Exception ex)
{

}
     }
0
 
LVL 1

Author Comment

by:weeb0
ID: 11738572
can you explain me a bit why I ABSOLUTELY have to try and catch ???

Thank you a lot for the answer ;0
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11738623
because Connection.close() and ResultSet.moveNext() throws SQLException objects

0
 
LVL 1

Author Comment

by:weeb0
ID: 11738805
this function is not working, do you know how I can get this working ?

public String get_data(String column)
      {
          String a = "";
          try
            {
                a = rs.getString("Name");
               //<- here I get a unhandled exception type SQLException I know something is not declared, but was'nt my code
            }
            catch(Exception e)
            {
                 System.out.println("Whoops! Something went wrong. \n");
                 e.printStackTrace();
            }
            return(a);
      }
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 35
Error in @AspectJ Based AOP with Spring 2 18
String array comparison 4 38
Is there a simpler dropbox system? 10 34
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

830 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