-->

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.
© Copyright 2013 Computer Programming | All Right Reserved