-->

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