-1

I'm new to Java and basically my question is - how to link MS access to NetBeans, I've tried everything but nothing seem to work.

public Student1 (){


   connect ();
}

public void connect () {

    try{

        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);

        String Student1 = "jdbc:odbc:Database1";
        con = DriverManager.getConnection(Student1);
        st = con.createStatement();
        String sql = "select * from Student1";
        rs = st.executeQuery(sql);

        while (rs.next())
        {
            String fname = rs.getString("First Name");
            String lname = rs.getString("Last Name");
            String dob = rs.getString("DOB");
            String studentid = rs.getString("StudentID");
            String mobileno = rs.getString("MobileNo");
            String address = rs.getString("Address");
            String email = rs.getString("Email");


            System.out.println(fname + " "+lname+" "+dob+" "+studentid+" "+mobileno+" "+address+" "+email);

        }

    }catch (Exception ex){

    }

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
new Student1();
}

}

I get a yellow line on:

connect ();
catch (Exception ex)
new Student1();
Mysterion
  • 9,050
  • 3
  • 30
  • 52
  • 1
    If you hover the mouse over this line NetBeans should give you a hint an what is wrong. What is this hint? –  Apr 13 '15 at 09:55
  • 2
    If you add some logging in your `catch()` you might be able to diagnose the error. – TZHX Apr 13 '15 at 09:56
  • 1
    use `Exception#printStackTrace()` method in your catch block !! It will show you the reason – Neeraj Jain Apr 13 '15 at 09:59
  • @Tichodroma On the connect() the hint is saying: Overridable method call in constructor, on catch (Exception ex) it is saying: The catch (Java.lang.exception) is too board, it catches the following exception types: Java.lang.clasNotFound Eeption and Java.sql.SQLException and new Student1() is saying; new instance, ignored, surround with introduce – Rasta .Dan Apr 13 '15 at 10:01
  • As a helpful tip you can see this answer http://stackoverflow.com/a/28939677/4336130 which describes how to connect to ms sql server so maybe it will give you some additional ideas of how to adapt the tip for your case – user390525 Apr 13 '15 at 10:04

2 Answers2

0

Yellow Line says that the catch java.lang.exception is too broad.

The Exception class is the super class of IO and Runtime Exceptions. There for you can either use a subclass of that or continue using this. Its up to you.

Doc Here

enter image description here

connect() the hint is saying: Overridable method call in constructor

This may cause may troubles. When the method is invoked the state of the object may be inconsistent. Please refer Here

You got yellow line for new Student1(); because you created a instance but you haven't stored it anywhere.

something like this will solve the problem.

Student1 n = new Student1();
Community
  • 1
  • 1
Anjula Ranasinghe
  • 584
  • 1
  • 9
  • 24
0

The error is being caused because the constructor for the Student1 class is making a call to an overridable method connect() in the same class.

The reason this is bad practice is because if someone subclasses Student1 and overrides connect(), then they would also be directly changing the behavior of the parent class constructor for Student1. This is a break of encapsulation.

Example:

public class NewStudent extends Student1 {
    // constructor, etc...

    @Override
    public void connect() {
        // do something new here
        System.out.println("calling NewStudent.connect()");
    }
}

NewStudent student = new NewStudent();

This will output calling NewStudent.connect()

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360