Tuesday 10 April 2018

Learn how to make java application program from derby data base based on Embedded Method

Introduction to Derby:

Derby  Database is the choice of computer programming to create simple application software.Derby database works more easily with Java programs language. Derby database is based on open source code and provides small footprint and standards based database engine that can be tightly embedded into any java based solution.
It is available with many versions Apache  Technology Through this, many types of data best applications are created. It is used to create two types of databases . An application which is based on single computer machine And in the other the clients that are based on the server architecture. Derby is used in different operating system software with different configurations Use command prompt for its settings.This database works with different operating system software such as Windows Unix Linux .

what is Derby Embedded:

derby provides simple single user java application .in this method Derby runs in the same Java virtual machine (JVM) as the application. Derby can be almost invisible to the end user because it is started and stopped by the application and often requires no administration. it is a rich application method of java programing languages.  

 Here's a general method to build your application based on embedded method.
What is shown in the Java program based on that data base.
 How Databases Work for a Single Computer and Where Derby  can create an independent software application that can be used on a Windows based computer.

creating method:


Step1:open NetBeans IDE 8.2
step 2:create a project with name Databaseapplication.
step 3:add a jframe with name userform
step 4:create GUI like this.

Step 5:do code like soure code Databaseapplication.java and userform.java
step 6:add needed db jar files like darbyrun,darbynet etc. full details see in youtub..
download this project with at this likes.


Databaseapplication.java source code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseapplication;

/**
 *
 * @author rajesh kumar shukla
 */
public class Databaseapplication {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        userform myf=new userform();
        myf.setVisible(true);
    }
    
}

userform.java(source code)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package databaseapplication;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DatabaseMetaData;
import java.sql.Statement;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
/**
 *
 * @author rajesh kumar shukla
 */
public class userform extends javax.swing.JFrame {

    /**
     * Creates new form userform
     */
    Connection conn;
    ResultSet rs;
    Statement smt;
    DatabaseMetaData mymetadata;
PreparedStatement psd;
    public userform() {
        initComponents();
        doconnect();
    }
public void doconnect()
{
    try
    {
       String Driver="org.apache.derby.jdbc.EmbeddedDriver";
       String mydata="newdab";
       String url="jdbc:derby:"+mydata+";create=true";
               conn=DriverManager.getConnection(url);
               smt=conn.createStatement();
               DatabaseMetaData dbmd = conn.getMetaData();
rs = dbmd.getTables(null, null, "FIRSTTABLE", null);
String qurer="CREATE TABLE FIRSTTABLE(ID INT PRIMARY KEY,"+ "NAME VARCHAR(30), PRICE INT)";
if(!rs.next()){
smt.executeUpdate(qurer);

 smt.executeUpdate("INSERT INTO FIRSTTABLE VALUES(1, 'Audi', 52642)");
            smt.executeUpdate("INSERT INTO FIRSTTABLE VALUES(2, 'Mercedes', 57127)");
            smt.executeUpdate("INSERT INTO FIRSTTABLE VALUES(3, 'Skoda', 9000)");
            smt.executeUpdate("INSERT INTO FIRSTTABLE VALUES(4, 'Volvo', 29000)");
            smt.executeUpdate("INSERT INTO FIRSTTABLE VALUES(5, 'Bentley', 350000)");
            smt.executeUpdate("INSERT INTO FIRSTTABLE VALUES(6, 'Citroen', 21000)");
            smt.executeUpdate("INSERT INTO FIRSTTABLE VALUES(7, 'Hummer', 41400)");
            smt.executeUpdate("INSERT INTO FIRSTTABLE VALUES(8, 'Volkswagen', 21600)");
}
rs.close();
smt.close();

smt = conn.createStatement();


String query1="SELECT * FROM FIRSTTABLE";
rs=smt.executeQuery(query1);
rs.next();
int idi=rs.getInt("ID");
String mid=Integer.toString(idi);
String named=rs.getString("NAME");
int pri=rs.getInt("PRICE");
String prict=Integer.toString(pri);
textField1.setText(mid);
textField2.setText(named);
textField3.setText(prict);
    }
    catch(SQLException err)
    {
      JOptionPane.showMessageDialog(userform.this, err.getMessage());
    }
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        label3 = new java.awt.Label();
        textField1 = new java.awt.TextField();
        textField2 = new java.awt.TextField();
        textField3 = new java.awt.TextField();
        button1 = new java.awt.Button();
        button2 = new java.awt.Button();
        button3 = new java.awt.Button();
        button4 = new java.awt.Button();
        button5 = new java.awt.Button();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("user form");

        label1.setBackground(new java.awt.Color(51, 255, 51));
        label1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        label1.setForeground(new java.awt.Color(255, 51, 51));
        label1.setText("userId");

        label2.setBackground(new java.awt.Color(51, 255, 51));
        label2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        label2.setForeground(new java.awt.Color(255, 51, 51));
        label2.setText("username");

        label3.setBackground(new java.awt.Color(51, 255, 51));
        label3.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        label3.setForeground(new java.awt.Color(255, 51, 51));
        label3.setText("userphno");

        textField1.setBackground(new java.awt.Color(51, 255, 51));
        textField1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        textField1.setForeground(new java.awt.Color(255, 51, 51));

        textField2.setBackground(new java.awt.Color(51, 255, 51));
        textField2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        textField2.setForeground(new java.awt.Color(255, 51, 51));

        textField3.setBackground(new java.awt.Color(51, 255, 51));
        textField3.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        textField3.setForeground(new java.awt.Color(255, 51, 51));

        button1.setBackground(new java.awt.Color(0, 255, 51));
        button1.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        button1.setForeground(new java.awt.Color(255, 51, 0));
        button1.setLabel("new");

        button2.setBackground(new java.awt.Color(0, 255, 51));
        button2.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        button2.setForeground(new java.awt.Color(255, 51, 0));
        button2.setLabel("save");

        button3.setBackground(new java.awt.Color(0, 255, 51));
        button3.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        button3.setForeground(new java.awt.Color(255, 51, 0));
        button3.setLabel("delet");

        button4.setBackground(new java.awt.Color(0, 255, 51));
        button4.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        button4.setForeground(new java.awt.Color(255, 51, 0));
        button4.setLabel("next");
        button4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button4ActionPerformed(evt);
            }
        });

        button5.setBackground(new java.awt.Color(0, 255, 51));
        button5.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
        button5.setForeground(new java.awt.Color(255, 51, 0));
        button5.setLabel("previous");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(14, 14, 14)
                                .addComponent(textField3, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(textField1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                    .addComponent(textField2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(65, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button4ActionPerformed
        // TODO add your handling code here:
         try
    {
       String Driver="org.apache.derby.jdbc.EmbeddedDriver";
       String mydata="newdab";
       String url="jdbc:derby:"+mydata+";create=true";
               conn=DriverManager.getConnection(url);
               smt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
               String query1="SELECT * FROM FIRSTTABLE";
rs=smt.executeQuery(query1);
while(rs.next()){
  int idi=rs.getInt("ID");
String mid=Integer.toString(idi);
String named=rs.getString("NAME");
int pri=rs.getInt("PRICE");
String prict=Integer.toString(pri);
textField1.setText(mid);
textField2.setText(named);
textField3.setText(prict);
  
}
               
    }
    catch(SQLException err)
    {
      JOptionPane.showMessageDialog(userform.this, err.getMessage());
    }
    }//GEN-LAST:event_button4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(userform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(userform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(userform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(userform.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new userform().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button button1;
    private java.awt.Button button2;
    private java.awt.Button button3;
    private java.awt.Button button4;
    private java.awt.Button button5;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    private java.awt.TextField textField3;
    // End of variables declaration//GEN-END:variables
}


0 comments:

Post a Comment