JDBC Example using Forms

JDBC Program to Insert a record to database using Forms.

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.sql.*;

class Form extends JFrame
{
JButton ADD;
JPanel panel;
JLabel label1,label2,label3,label4,label5;
final JTextField text1,text2,text3,text4,text5;

Form()
{
label1 = new JLabel();
label1.setText("UserID:");
text1 = new JTextField(20);

label2 = new JLabel();
label2.setText("First Name:");
text2 = new JTextField(20);

label3 = new JLabel();
label3.setText("Last Name:");
text3 = new JTextField(20);

label4 = new JLabel();
label4.setText("ADDRESS:");
text4 = new JTextField(20);

label5 = new JLabel();
label5.setText("Salary:");
text5 = new JTextField(20);

ADD=new JButton("ADD");

panel=new JPanel(new GridLayout(6,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
setTitle("FORM");

setDefaultCloseOperation(EXIT_ON_CLOSE);

ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=text4.getText();
String value5=text5.getText();


try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection     con = DriverManager.getConnection("jdbc:odbc:test");
Statement sta = con.createStatement(); 
sta.executeUpdate("insert into Emp_Det(userid,fname,lname, address, salary) values('"+value1+"','"+value2+"' ,'"+value3+"','"+value4+"','"+value5+"')");

            JOptionPane.showMessageDialog(null,"Inserted Successfully!");
        }
catch(Exception e){}
}
});
}
}
class FormDemo
{
public static void main(String arg[])
{
try
{
Form frame=new Form();
frame.setSize(300,150);
frame.setVisible(true);
}
catch(Exception e)
{
}
}
}

Java Program to show Frames

import javax.swing.*;

public class frame
{
public static void main(String[] args)
{
JFrame f = new JFrame("Frame in Java Swing");
f.setSize(400, 400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

Java Program to demonstrate HashMap example.

import java.util.*;
public class HashMapExa 
{
public static void main(String args[]) 
{
HashMap<Integer, String> hmap = new HashMap<Integer, String>();

      hmap.put(1, "AppleIOS");
      hmap.put(2, "Windows");
      hmap.put(3, "Linux");
      hmap.put(4, "Solaris");
      hmap.put(5, "DOS");

      /* Now, Display content using Iterator*/
      Set set = hmap.entrySet();
      Iterator iterator = set.iterator();
System.out.println("List of Contents in the HashMap"); 

 while(iterator.hasNext()) 
   {
    Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
         System.out.println(mentry.getValue());
      }

      /* Request values based on key*/
      String var= hmap.get(1);
      System.out.println("Value at index 1 is: "+var);

      /* Remove values based on key*/
      hmap.remove(3);
      System.out.println("Map key and values after removal:");
      Set set2 = hmap.entrySet();
      Iterator iterator2 = set2.iterator();
      while(iterator2.hasNext()) {
          Map.Entry mentry2 = (Map.Entry)iterator2.next();
          System.out.print("Key is: "+mentry2.getKey() + " & Value is: ");
          System.out.println(mentry2.getValue());
       }
   }
}

Java Program to demonstrate Vector with sorting records example

import java.util.*;
public class VectorExa {

public static void main(String args[])
 {
      Vector<String> vec = new Vector<String>(3);

      vec.addElement("AppleIOS");
      vec.addElement("Windows");
      vec.addElement("Linux");
      vec.addElement("solaris");

      /* check size and capacity Increment of vector */
      System.out.println("Size of Vector is: "+vec.size());
      System.out.println("Capacity increment is: "+vec.capacity());


      vec.addElement("Android");
      vec.addElement("Symbian");
      vec.addElement("Bada");

      /* After inserting extra elements and capacityIncrement after insertions*/
      System.out.println("Size after addition: "+vec.size());
      System.out.println("Capacity after increment is: "+vec.capacity());

      /*Display Vector elements*/
      Enumeration en = vec.elements();
      System.out.println("\nElements in Vector are:");
      while(en.hasMoreElements())
         System.out.println(en.nextElement() + " ");


Collections.sort(vec);
  System.out.println("Elements in Vector after sorting :");
  for(int i=0; i < vec.size(); i++)
System.out.println(vec.get(i));
   }
}

Java program without using main()

class WithoutMain
{
static{
System.out.println("Hello world");
System.exit(0);
}
}
Share
Share
Scroll to Top