-->

Thursday, February 18, 2016

How to retrieve items from database table in JAVA using Netbeans

In this article i will explain you, how to retrieve items from database table in Java using Netbeans. In previous article, we have already learned, how to create connection with the derby database using netbeans. Before reading this article, please must to see the previous article for implementations. In this article, i only explain you, how to use executeQuery( ) method and select statement in java.

--How to create connection with database full video tutorial 


Code :

class abc {
    public abc() throws SQLException
    {
        Connection conn=DriverManager.getConnection("jdbc:derby://localhost:1527/student", "tarun", "tarun");
        System.out.println("Connection Created");
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("select * from USERTABLE");
        while(rs.next())
        {
            System.out.println(rs.getString(2));
        }
   
    }

}

Here, we have a table i.e USERTABLE. If you want to learn how to create table in exists database using Netbeans. Read mentioned steps:
Step-1 :  Right click on Connection String, select "connect" from popup menu.
Step-2 :  Expand username which contains Tables, views and stored procedure.
Step-3 :   Right click on table, select create table.


Write the name of table also add columns with their datatypes. 

Wednesday, February 17, 2016

How to create Database connection in JAVA using Netbeans

If you want to connect database with your java application then first to need a database. First to create database using Netbeans services tab. This tab available under Windows tab. You can also select this tab by using shortcut CTRL+ 5.


services tab in netbeans


  1. Expand the databases tab, select "Create Database." option. 
create database in netbeans

2. Fill fields which are shown under diagram.

Database fields in Netbeans
 3. After filling the form, you have to create database successfully.
 4. Right Click on your connection string name, select properties for copy the database URL.

java netbeans database properties

5. Copy Database URL, which is used further.

netbeans database properties
6. Create a new java project under projects tab. 
7. Add a Library in the "Libraries" section of your project.
add libraries in projects
8. Select Java DB Driver under Add Library section.

JAVA DB Drivers
9. Open your .java file which is exists in java packages folder of your project. 
10. Create a another class object, which is exists in same package.
11. Now, Open the object class, create constructor in it.
12. paste the code under the constructor.

Connection conn=DriverManager.getConnection("jdbc:derby://localhost:1527/jk", "jk", "jk");
        System.out.println("connection created"); 

Here we have three parameters in getConnection method. 
First refer to database url ; according to 5th step.
Second : Username 
Third :  password.

Saturday, December 12, 2015

AWT Frame close using close Button

In AWT Java we have a Frame class which is inherit from window class. If you are a beginner in AWT Java then you know that Frame doesn't close when we press close button of it. So, In last we pressed stop debugging button in Netbeans IDE. If you want to close Frame using Close button, which is see in top right corner of Frame then you should implement code for it. 


Complete Java Code


package awtframe;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class AWTFrame {

    public AWTFrame()
    {
        Frame f1=new Frame("Welcome to Closing");
        f1.setSize(500,500);
        f1.setVisible(true);
       
        f1.addWindowListener(new WindowAdapter(){
           
            @Override
            public void windowClosing(WindowEvent e)
            {
              System.exit(0);
            }
           
        });
       
       
    }
    public static void main(String[] args) {
     
       AWTFrame at=new AWTFrame();
       
    }
   
}

Here, we have windowClosing ( ) method to close Frame using close button.

Sunday, December 28, 2014

Create an Object using New Operator: Java

New operator is used to create a new object of an existing class and associate the object with a variable that names it. Basically new operator instantiates a class by allocating memory for a new object and returning reference to that memory.
Every class needs an object to use its member and methods by other classes. Programmer have to create that object using new operator to use functionality provided by that class. Following is the syntax to create new object of an existing class:

Class_variable =new Class_Name();

This syntax basically describes some points about new operator as below:
  • Allocates memory to class_variable for new object of class.
  • It invokes object constructor.
  • Requires only single postfix argument which calls to constructor.
  • Returns reference to memory which is usually assigned to variable of appropriate type.
Following statement will create an object of city class and allocate memory to this newly created object:

City metro;
metro = new City();

The equivalent code for the above statement that can be written in single line is:

City metro = new City();

So new operator does two things overall i.e. it first allocates memory somewhere to hold an instance of the type, and then calls a constructor to initialize an instance of the type in that newly allocated memory.

After creating new object for a class, that object can use all the members and methods defined in that class. You can assign all the properties for that class and operate available or new functions on those properties.

Monday, November 10, 2014

Compiling and Running Java Programs

There are various steps to compiling and running java programs, these are
Step-1 : First to install JDK, JRE and JVM into the System.
Step-2 : Prepare source code in any editor like notepad, c++ editor etc.
Step-3:

class HelloWorld
{
public static void main(String r[])
{
int a=10, b=10,c;
c=a+b;
System.out.println("Output of the program is"+c);
}
}

Step-4 : Save the source code in the java bin directory. Class name which contain main method should same of the file name.
Example :  HelloWorld (class name)
                  HelloWorld.java (file name)

Step-5 : Open command prompt and change the directory where your java program has been saved.
Suppose your java software installed in C:/>. Now, compiling steps is:

C:/java/jdk1.7.0/bin> Javac filename.java   (press Enter)
C:/java/jdk1.7.0/bin> Java  filename.java    (press Enter)

Step-6: During compilation, if program generate errors, it means you can't run your program.                 

Wednesday, October 8, 2014

Life Cycle of applet

In the previous article, we have already learned about basics of applet.

Life Cycle of applet

1. init() : init method is used to initialize the applet. Before doing anything, must to initialized, so same thing with the applet. Like, in the games.
Before play any games, must to initialized all the  drivers in the system, which is necessary for the games. In the applet, init method is invoked only once, also that method invoked after param tag, which is inside in <applet></applet> tag.

2. start() : This is invoked after the initialization completed. It is used to start the applet, also automatic start. When user navigate to the web page, if
web page contain applet code then it start automatically also start when user moves from other web pages.

3. paint () : This method is invoked just after start() method. Actually only this method is responsible to design the applet, also responsible for repaint the browser, if applet is stooped. It has contain graphics argument as a parameter, through Graphics class we can design any graphics in the applet like rectangle, oval etc. Graphics class inherit from java.awt class.
4. stop() : This method is invoked, just after when the user moves to other browser tab from current applet tab.

5. destroy() : This method is invoked only that time when user close the browser. You can say, opened web page contain applet code , on that time applet is running
properly but in case if user close the browser, applet instance is automatically deleted from the page and destroy method is called automatically.

Example of Applet

The following is a simple applet named HelloWorld.java

import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet
{
public void paint (Graphics g)
{
g.drawString ("Hello World", 30, 50);
}
}

The first line of code define, imports some packages, which is used for design the applet. Here are two class that is applet and awt. In the second line of code, we have a class "HelloWorld" which is inherited from Applet class. This Applet class inside in java.applet.* package. Through the Graphics class, we can design new object in the applet. This class inherited from java.awt.*. In this example, we have to draw a string with height and width argument(width=30, height=50).
With the help of applet tag, we will show the output in any browser, which support applet. Applet tag is:
  <applet code="HelloWorld.class" width=50 height=60></applet>

This tells the viewer or browser to load the applet whose compiled code is in HelloWorld.class (in the same directory as the current HTML document), and to set the initial size of the applet to 50 pixels wide and 60 pixels high.

Monday, October 6, 2014

Basics of applet

Applet is a small java program that run in browser, but JVM is required for run applet in browser. Basically applet is used where user want to put some dynamic content into html page.
There are some features and advantages of Applet

Features:


  1. It is a java class file, which is inherit from java.applet.Applet class.
  2. A applet class file don't take main() method for entry point.
  3. Applet codes are embedded in HTML page
  4. For run the applet code in browser, must to install JVM in the client machine
  5. During the execution of applet code in browser, code first to download in the client machine.

Advantages:


  1. Very less response time because Applet designed for client machine. If applet is used for server machine then server take more time to execute applet code.
  2. More secure
  3. Platform independent, so it can run on any platform that is windows, Linux, UNIX, etc.
  4. Use for dynamic application


Disadvantages of applet


  1. For run to its code in browser, must to installed JVM plugin

Monday, May 26, 2014

How to Define Methods with Behavior: Java

Objects have behavior that is implemented by its methods. Other objects can ask an object to do something by invoking its methods. This section tells you everything you need to know about writing methods for your Java classes.

In Java, you define a class’s methods in the body of the class for which the method implements some behavior. Typically, you declare a class’s methods after its variables in the class body although this is not required.

Implementing Methods

Similar to a class implementation, a method implementation consists of two parts: the method declaration and the method body

methodDeclaration {
        methodBody
}

The Method Declaration

At minimum, a method declaration has a name and a return type indicating the data type of the value returned by the method:

returnType methodName( ) {
. . .
}

This method declaration is very basic. Methods have many other attributes such as arguments, access control, and so on.

Objects as Instances of Class

A class defines only a blueprint and its concrete version comes into effect only through objects that implement the functionality as defined by class. Recall our class example of City class. The objects created from this class will have two variables: name and population; and they will be able to represent cities. The object of City class will also have a method namely display ( ). An object of a class is typically named by a variable of the class type. For example, the program CityTrial in Example 4.7 declares the two variables metl1 and metro2 to be of type City, as follows;

City metro1, metro2;

This gives us variables of the class City, but so far there are no objects of the class. Objects are class value that are named by the variables. To obtain an object you must use the new operator to create a “new” object. For example, the following creates an object of the class City and names it with the variable metro1:

Metro = new city ( );

For now you need not go into details, simply note that the statement like:

Class-variable = new class-Name ( );

Creates a new object of the specified class and associates it with the class type variables. Since the class variable now names an object of the class, we will often refer to the class variable as an object of the class. (This is really the same usage as when we refer to an int variable n as “the integer n”, even though the integer is strictly speaking not n but the value of n.)

Unlike what we did in previous lines, the declaration of a class type variable and the creation of the object are more typically combined into one statement as follows:
City metro1 = new City( );
To instantiate an object, Java uses the keyword new.
© Copyright 2013 Computer Programming | All Right Reserved