Solved

What is wrong with this class

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 87
servlet filter example 37 58
How do I remove an object from a 3 23
JavaFX TableView not displaying correctly 3 16
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

816 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