Solved

What is wrong with this class

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 473
Java exception bubble up 2 26
SequenceInputStream example 3 38
maven not picking latest jar instead picking old jar from .m2 12 75
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

752 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