Java GUI Programming

Netbeans IDE and MySQL

First of all you have to install Java Development Kit(1.7),MySQL server and if you can better to download mysql gui tools also. Mysql gui tools will help you to create database in few seconds. But i recomend that you better to use mysql command line to create database.

to download jdk latest version goto http://www.oracle.com/technetwork/java/javase/downloads/index.html and download and install it. I assume that every one can install it and if you want to compile Java source codes using command lines you better to edit environment variables also.

then you can download MySQL server latest version from this website  http://dev.mysql.com/downloads/ and install it. In this lessons i will use MySQL server 5.0.

Now you can download Netbeans IDE from here http://netbeans.org/downloads/index.html according to your requirements. Now you are ready to start your java programming using Netbeans IDE.

First program:

Start the Netbeans IDE goto File –>New project–>

Java application–>

Put a name to the application and select a path to save it.

Now to test the IDE you can write simple hello world application and run it. Remember that every Java project must have a main class. I am not going to post every simple things according to Java in here. In above picture you can see i have put a tick mark in main class check box. In Netbeans it will automatically create a main class.

goto main class and write the ”  System.out.println("Hello world"); ” inside the body of the main class. When you typing easily you can press the ” Ctrl + Space ” buttons together and see what happens. It will list you some sort of words as a hint. You can chose appropriate method name from the list and press enter Button.

Actually it is hard to explain every thing related to to Netbeans IDE. You have to find optional things from the web web if you want.

Now i am going to deep in database programming. Now we are going to create simple database application using Java and MySQL. First of all you have to download java-mysql connector to connect java and mysql together. Download it from here

http://dev.mysql.com/downloads/connector/j/

extract the source file and finally you must have a .jar file called java-mysql connector. Then add that jar file to our project

Finally you can see connector in the library folder

Now add a new class to the project and name it as connector.java. Remember that Java file must be added to your existing package. In my project package name is javaapplication6. Right click on the package and click add new Java class to add a new class to our existing Java package.Let’s  we create a simple Java application in step by step

1. Goto MySQL command line and create a database and name it as “simple”

2. Then create a new table and put two rows as firstname and secondname.

3. Then insert a example values as you wish

Below you can see the Sql syntax to do above steps.

1. CREATE DATABASE simple;

2. USE simple;

3. CREATE TABLE names (firstname varchar(30),lastname varchar(30));

4. INSERT INTO names VALUES('madusanka','chandanamal');

now you have inserted values to the database .You have experienced that it was little hard to insert data using command line. What will happen, if you have to 1000 entries to database using command line?.. that is the reason people use GUI’s to communicate with databases. OK.. lets open the  Netbeans and  add new JFrame. I think you already know where to add it. Remember it must be added to the existing java package. Now in your left hand side you can see palette window. In palette window you can see swing containers. You can drag and drop swing components to your JFrame.

I have added a new JFrame and named it as test.java. see the below picture and create a gui something like that.

Not only in java but also in every programming language variable names play vital role. Because of that it it better to set appropriate variable name to the each and every variable. Below picture you can see how to set variable name to the JTextBoxe’s and other components.Right click on the text box and click change variable name and change it.

now you must have interface like this.

We have completed the GUI part and now we are going to connect and insert data into our database using GUI.Don’t worry you can download complete example. But it is not the way to learn java. How ever to we already added a new class called connection.java. I will explain essential codes that connection class.

Importing essential libraries from lava libraries

import java.sql.Connection;

import java.sql.DriverManager;

import javax.swing.JOptionPane; // this component we use to show error messages to the user’s.

Create a connection and call it using DriveManager

Class.forName("com.mysql.jdbc.Driver").newInstance(); //call jdbc driver

String url = "jdbc:mysql://localhost/simple"; //getting url

conn = DriverManager.getConnection(url,"root","1234");

in here root is my MySQL server username and 1234 is the password to MySQL server. What happens if error occur.We have to use exception handling to handle errors.In here i have used JoptionPane to display error messages to the users.

catch (Exception e)        {

JOptionPane.showMessageDialog(null,e.getMessage(),"Example",JOptionPane.ERROR_MESSAGE,null); //exeption hanling

}

if error occurred you can see the error using  JOptionPane. OK.. Now what we want to do what ever the texts in the text boxes must be added to the database.Every button have an event handler option.If  the user clicked  add button those values must be saved in the database.Let’s we see how it happened.Right click the add button and event –> action –>action performed option and it will goto code window and inside the code body u can type your appropriate Java code. In here i will use very simple codes.I will code every thing inside the click event of the button body.

See the example’s appropriate section to the code. In briefly i will explain important codes.

connection getcon= new connection(); // create a new object of the connection class

Connection conn=null; // create connection type variable.Don’t misunderstand connection and Connection.Connection is a class that already in the jdk.

try{

conn=getcon.creatConnection(); // get returned value to conn object

conn.createStatement().executeUpdate("insert into name values( '"+txtFirstName.getText().trim()+ "', '"+txtLastName.getText().trim()+"' ) "); in here i am getting text boxes values using .getText() method.

// trim() method will ignore the spaces of the text when it save in the database.

JOptionPane.showMessageDialog(null, "Data succesfully inserted to the database","Example",JOptionPane.INFORMATION_MESSAGE);

txtFirstName.setText(null); // we use this method to clear the text boxes text’s.

txtLastName.setText(null);

txtFirstName.requestFocus(); // this method used to send cursor to the first text box again for next data insert.

}

catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.getMessage(), "Example",JOptionPane.ERROR_MESSAGE);

}

Now compile and run it using F6 or button and test it. If error occurred read the error message carefully and understand it. Another very important thing.. Do you remember that you have created a main class in very first in the example.What happened when you compile and run it..There is no any compilation errors but your GUI will not start. Because you haven’t call your JFrame in the main method.To do that create a new object of the .java file and call it in the main class.

Ex:- in my project i have named it as test.java and i am going to create a object of that class in the main method.

test Test= new test();

Test.setVisible(true);

Why setVisible(true) method here.Because every swing components hidden in default manner.Then we have to visible true it. To do that we use that special method.

you can download the example from here

Download Complete example

For uwu friends Download HCI example from here
Download here

Part 2- Update data..

9 thoughts on “Netbeans IDE and MySQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s