Solved

What is wrong with this class

Posted on 2004-08-06
5
1,695 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
Comment Utility
public void disconnect_access()
     {

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

     }


0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
because Connection.close() and ResultSet.moveNext() throws SQLException objects

0
 
LVL 1

Author Comment

by:weeb0
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maven java path setting 5 49
mapShare challenge 13 66
backtracking recursion  code 19 39
Cipher Configuration on Apache HTTPD 4 15
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now