Monday, 26 October 2015

Insert data into mysql database using java and netbeans

import java.sql.*;
import java.lang.*;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;

/*
 * 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.
 */

/**
 *
 * @author KA
 */
public class contact_list extends javax.swing.JFrame {

    /**
     * Creates new form contact_list
     */
    public contact_list() {
        initComponents();
    }

    /**
     * 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">                        
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Name");

        jLabel2.setText("Mobile");

        jLabel3.setText("email");

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jButton1.setText("Add Contact");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Exit");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        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(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jButton1)))
                .addGap(63, 63, 63)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jButton2)
                        .addComponent(jTextField2)
                        .addComponent(jTextField1))
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(115, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(95, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                      

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
         String Name=jTextField1.getText();
        String Mobile=jTextField2.getText();
        String Email=jTextField3.getText();
       
         String emailregex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
         String mobileregex = "^[_0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
         Boolean b = Email.matches(emailregex);
       
        if (Name.isEmpty())
            JOptionPane.showMessageDialog(this,"Name not Entered");
         else if (Mobile.isEmpty())
            JOptionPane.showMessageDialog(this,"Mobile not Entered");
         else if (Email.isEmpty())
            JOptionPane.showMessageDialog(this,"Email not Entered");
         else if(b == false)
         {
               JOptionPane.showMessageDialog(this,"Email Address is Invalid");
         }
       
        else
         {
                try
                   {
                        Class.forName("java.sql.DriverManager");
                        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cbse","root","root");
                        String query="INSERT INTO Contact VALUES('"+Name+"','"+Mobile+"','"+Email+"');";
                         PreparedStatement pst = con.prepareStatement(query);
                         pst.executeUpdate(query);

                    }
                    catch(Exception e)
                    //this block is executed in case of an exception
                    {
                    //Display an error message in the dialog box for an exception
                    JOptionPane.showMessageDialog (this, e.getMessage());
                    }
         }
    }                                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        System.exit(0);
    }                                      

 
    /**
     * @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(contact_list.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(contact_list.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(contact_list.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(contact_list.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 contact_list().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration                  
}

Saturday, 20 December 2014

Meaning for public static void main () in java

Meaning for public static void main () in java
public  is a modifier indicates that method can be used by other java clases
static  is a modifier indicates that main() doesn’t require any object to access the method
void -no value is returned

main() -flow control is given to main().Execution order is depend on this instruction order  in the main() function

Tuesday, 16 December 2014

Class Key Words And Identifiers In Java


public class Display{

public is a keyword which means means what we are named can use by other classes
class is a keyword which indicates a class is being named
Display is an identifier gives the name of the class

{   Opening braces delimiter  indicates description of class members 

Wednesday, 8 October 2014

Write a program to reverse a string

import java.util.*;
 class ReverseAString           //class name
{
   public static void main(String args[])  //main function
   {
      String original, reverse = "";    //string declaration
      Scanner in = new Scanner(System.in);                //to get input from- //-user

      System.out.println("Enter a string to reverse"); //Ask //string to reverse from user
original = in.nextLine();                            //getting input from user //using ‘in’ object

      int length = original.length();            //finding length of the //inputted string

      for ( int i = length - 1 ; i >= 0 ; i-- )  //using for loop //getting each character by using charAt(i) method
         reverse = reverse + original.charAt(i);  //reversed string is //stored in reverse string

      System.out.println("Reverse of entered string is: "+reverse);
//reversed sting is outputted to user
   }
}

Java Interview questions

·        Collections in java
·        Interfaces in java
·        Why static is used in public static void main()
Question
·        Difference between jre and jdk
Answer
·        JRE: Java Runtime Environment.
JRE basically the Java Virtual Machine where Java programs run on and  also includes browser plugins for Applet execution.
·        JDK: Java Development Kit

It is a Software Development Kit for Java, including JRE, and compilers and tools (eg. JavaDoc,, Java Debugger) to create and compile programs.

Sunday, 28 September 2014

Final Variable With Example


There is a final variable a, we are going to change the value of this variable, but It can't be changed because final variable once assigned a value can never be changed.

Example Program

class Super
{
                       final int a=100;
                       void show()
                       {
                        //a=200;error , can't be changed because final variable once assigned a value can never be changed.

                        System.out.println("Welcome to the final Keyword                       "+a);
                       }
}

class Demofinal
{
                       public static void main(String args[])
                       {
                        Super obj=new Super();
                        obj.show();
                       }
}


Output

D:\>javac Demofinal

D:\>java Demofinal

Welcome to the final Keyword    100