Monday, October 8, 2012

Continue learning JAVA

And it has been quite awhile since the last post, and that was a post of Andrew Jackson State Park. So as the months passed by and things at work have got more challenging, it was time to dust off the JAVA books and continue with learning the language.

Quite awhile ago (years in fact) I picked up this Deitel & Deitel, JAVA How to Program Third Edition. At first I was planning on tossing this book for reasons of not being really that happy with it. After manually typing in some of the demo programs, my mind was changed. It dealt with the technology I am most interested in. This would be JTables, JSpinners and interfacing with MySql.

When I took a JAVA class at Ventura College years ago, I was able to pick up JAVA in a nutshell, Third Edition that covers JAVA 1.2 and 1.3. So that tells you how long ago that JAVA class was. As a reference manual for simple things like strings, arrays, lists, calendar, dates, this is not too bad for reference as long as the code is not that sophisticated.

Finally, after scrubbing Amazon dot com for a decent JAVA book (more modern) I settled on Cay Horstmann, Core JAVA, Volume II, Advance Features, Eight Edition. Well, in my hasty nature, his Volume I book did not even register so I clicked the buy button, next thing you know Volume II arrived. Overall, pretty happy with that book.

So, what kind of progress has been made. As far as the JAVA / MySql interface, I'm pretty happy and feel fairly competent in that area. The Deitel & Deitel book had some good examples that were typed in and ran seemingly right out of the box. Here is the cut/paste from the code that connects as user frank with the same password.


public class AddressBook extends JFrame {
    private ControlPanel controls;
    private ScrollingPanel scrollArea;
    private JTextArea output;
    private String url;
    private Connection connect;
    private JScrollPane textpane;

    ... cut ...


        String driver = "com.mysql.jdbc.Driver", 
        url = "jdbc:mysql://localhost:3306/address",
        user = "frank", 
        password = "frank";
                
        try{
        Class.forName(driver);
        connect = DriverManager.getConnection(url, user, password);
        output.append("Connection Successful");
        System.out.println("Connection Successful");
        }
        catch(ClassNotFoundException cn){
            output.append("Connection not successful\n"+cn.toString());
            System.out.println("Connection not Successful"+cn.toString());

        }
        catch(SQLException sq){
            output.append("Connection not successful\n"+sq.toString());
            System.out.println("Connection not Successful"+sq.toString());

        }
        catch(Exception ex){
            output.append("Connection not successful\n"+ex.toString());
            System.out.println("Connection not Successful"+ex.toString());

        }

This particular file that was typed in had several methods. The nerds would find this pretty straight forward. For me it was informative as the "Connection c" is the connection that is set in the code snippet above is passed in as an argument to each of the below methods. That is not a new concept for me, I'm used to coding functions, passing in all kinds of arguments, datatypes, opaque handles, etc. It is nice to know there is no difference with a JAVA Connection.


    public ControlPanel(Connection c, ScrollingPanel s, JTextArea t){
        setLayout(new GridLayout(1,5));
        
        findName = new JButton("Find");
        findName.addActionListener(new FindRecord(c,s,t));
        add(findName);
        
        addName = new JButton("Add");
        addName.addActionListener(new AddRecord(c,s,t));
        add(addName);
        
        updateName = new JButton("Update");
        updateName.addActionListener(new UpdateRecord(c,s,t));
        add(updateName);
        
        clear = new JButton("clear");
        clear.addActionListener(new ClearFields(s));
        add(clear);
        
        help = new JButton("help");
        help.addActionListener(new Help(t));
        add(help);
      
        System.out.println("ControlPanel:");

    }
}

So, in closing of this blog entry, the number one (only) take-away is the knowledge to connect to a database and the connection handle can be passed to other functions. My knowledge gap is to really understand Classes in JAVA. I have the concept down fairly well, since there are no structures, it is just kind of strange. With C++ no problem, got that understood (for not to sophisticated applications).

1 comment:

  1. Keep up the superb piece of work, I read few blog posts on this site and I think that your blog is really interesting and contains circles of good information.web design india

    ReplyDelete