Solved

What is wrong with this class

Posted on 2004-08-06
5
1,705 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
[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
  • 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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

632 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