Skip to main content
Home
Live to Learn!

Main navigation

  • Home
  • Learn
    • FoxPro
    • MS-DOS
    • C PRG
    • Java
    • ASP
    • Ruby on Rails
    • ASP.NET
    • E-Books
    • Exam Preparation
    • Tools
  • Blog
  • Forums
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Learn by Example - Java Sample Programs

JDBC Program Select, Insert, Update, Delete records

Book navigation

  • Armstrong Number
  • Palindrome Checking
  • Playing Audio Clip using Applet
  • Applet Form
  • Exception Handling
  • Multiple Inheritance and Packages
  • Shopping List
  • Simple Multithread program
  • JDBC Program Select, Insert, Update, Delete records
  • RMI - Example AddServer
  • Simple Bank Account Process
  • TCP Server and Client in Java
  • UDP Server and Client in Java
  • FTP Server and Client in Java
  • Chat Server and Client in Java
  • Echo Server and Client in Java
  • Address Resolution Protocol in Java
  • Ping server and Client in Java
  • Multicast Server and Client in Java
  • Transposition Cipher Method
  • Poly-alphabetic Cipher Method Encryption - Java
  • DES - Using Data Encryption Standard in Java
  • AES - Using Advanced Encryption Standard in Java
  • Bit Stuffing
By Karthik , 24 June, 2012
Before running this program, create access database and add a table named 'emp' with the following fields eno - Number ename - Text salary - Number Cofingure ODBC in your system using Control Panel -> Administrative Tools -> ODBC

import java.io.*;
import java.sql.*;
public class jdbcprg {

    static void myLine(){
        for (int i=1;i<=80;i++)
        {
            System.out.print("*");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        Connection con;
        Statement st;
        BufferedReader bin;
        ResultSet rs;
        ResultSetMetaData rm;
        String eno, ename, salary;
        int ch, nof;
      
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:karthik");
            st = con.createStatement();
            bin = new BufferedReader(new InputStreamReader(System.in));
            while(true){
                System.out.println("Choose Option");
                System.out.println("1. Select");
                System.out.println("2. Insert");
                System.out.println("3. Update");
                System.out.println("4. Delete");
                System.out.println("0. Exit");
                ch = Integer.parseInt(bin.readLine());
           if (ch==1)
               
           {
               rs = st.executeQuery("select * from emp");
               rm = rs.getMetaData();
               nof = rm.getColumnCount();
               myLine();
               for(int i=1; i<=nof; i++)
               {
                   System.out.print(rm.getColumnName(i)+"\t\t");
               }
               System.out.println();
               myLine();
               while(rs.next())
               {
               for(int i=1; i<=nof; i++)
               {
                   System.out.print(rs.getString(i) +"\t\t");
               }
               System.out.println();
               }
                myLine();
               
           }
           else if(ch==2)
                   {
            do
            {
                System.out.println("Enter E.No");
                eno = bin.readLine();
                System.out.println("Enter Name");
                ename = bin.readLine();
                System.out.println("Enter Salary");
                salary = bin.readLine();
                st.execute("insert into emp values("+eno+",'"+ename+"',"+salary+")");
                System.out.println("1 Record inserted");
                System.out.println("Continue ? [y/n]");
                eno = bin.readLine();
            }while(eno.equalsIgnoreCase("y"));
         }
           else if(ch==3)
           {
               System.out.println("Enter E.No. to Edit :");
               eno = bin.readLine();
               System.out.println("Enter Name");
               ename = bin.readLine();
               System.out.println("Enter Salary");
               salary = bin.readLine();
               st.execute("update emp set ename='"+ename+"', salary="+salary+" where eno ="+eno);
               System.out.println("1 Record Updated");
               
           }
           else if(ch==4)
           {
               System.out.println("Enter E.No. to Delete :");
               eno = bin.readLine();
               st.execute("delete from emp where eno ="+eno);
               System.out.println("Record Deleted");
           }
           else if(ch==0)
           {
            bin.close();
            con.close();
            System.exit(0);
           }
        }
        }
        catch(Exception e)
        {
            System.out.println("Error :"+e.getMessage());
        }
    }
}
  • Add new comment

Comments

Featured Blog Posts

Convert Currency in Number to Words (Indian Rupees) - MS Excel
Foxpro Tutorial and Programs
Convert Currency in Number to Words in MS Word
Convert Currency in Number to Words (Indian Rupees) - Version 2
Best way to Use Rupee Symbol in Windows – Easy steps
Convert Currency in Number to Words - MS Access
Creating All in One Windows XP DVD with all Important Applications
RSS feed

© 2009-2025 Live to Learn.In

Terms of Use | Privacy Policy