Followers

JDBC : Insertion using Statement Interface

package com.pack1;

import java.io.DataInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertQuery {
    public static void main(String[] args) throws IOException{
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            String url = "jdbc:derby://localhost:1527/hari";
            con = DriverManager.getConnection(url, "app","app");
            stmt = con.createStatement();
            System.out.println("Please enter the employee number to be inserted...");
            DataInputStream din = new DataInputStream(System.in);
            String strid = din.readLine();
            int id = Integer.parseInt(strid);
            System.out.println("Please enter the name");
            String name = din.readLine();
            System.out.println("Enter salary");
            float salary = Float.parseFloat(din.readLine());
            System.out.println("Enter the address");
            String address = din.readLine();            
            String query = "insert into MYEMP values("+id+",'"+name+"',"+salary+",'"+address+"')";
            int count = stmt.executeUpdate(query);
            
        } catch (ClassNotFoundException ex) {
            ex.getMessage();
        }catch (SQLException ex1) {
            ex1.printStackTrace();
        }catch (Exception ex1) {
            ex1.printStackTrace();
        }
        
       System.out.println("Record inserted successfully...");
    
    }
    
    
}



No comments:

Post a Comment