-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();
5
  • 1
    If you hover the mouse over this line NetBeans should give you a hint an what is wrong. What is this hint? Commented Apr 13, 2015 at 9:55
  • 2
    If you add some logging in your catch() you might be able to diagnose the error. Commented Apr 13, 2015 at 9:56
  • 1
    use Exception#printStackTrace() method in your catch block !! It will show you the reason Commented Apr 13, 2015 at 9: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 Commented Apr 13, 2015 at 10:01
  • As a helpful tip you can see this answer 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 Commented Apr 13, 2015 at 10:04

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

Comments

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.