Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.nucleus.nsbt.brd1.connectionsetup;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- //import java.sql.*;
- public class OracleConnectionSetup
- {
- //private static Connection connection = null;
- Connection connection = null;
- //Setup Connection to JDBC -
- public Connection getConnection()
- {
- String driverclass = "oracle.jdbc.driver.OracleDriver";
- String url = "jdbc:oracle:thin:@10.1.50.198:1521:orcl";
- String username = "sh";
- String password = "sh";
- try
- {
- Class.forName("driverclass");
- connection=DriverManager.getConnection("url","username","password");
- System.out.println("Error - \n Driver Class is Successfully Loaded...");
- }
- catch (ClassNotFoundException e)
- {
- System.out.println("Error - \n Driver Class Not Loaded...\nClassNotFound Exception Encountered while setting up JDBC connection..");
- e.printStackTrace();
- }
- catch (SQLException e)
- {
- System.out.println("Error - \n Driver Class Not Loaded...\n SQLException Encountered while setting up JDBC connection");
- e.printStackTrace();
- }
- return connection;
- }
- //Close Connection from JDBC -
- public void CloseConnection()
- {
- try
- {
- connection.close();
- System.out.println("\nJBDC connection has been Closed..");
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- System.out.println("Error in JDBC Connection - \n Connection not Terminated.. ");
- }
- }
- }
- package daoOperation;
- import java.sql.Connection;
- import java.util.HashSet;
- import CustomerDetails.Customer;
- public interface DaoInterface {
- public void conditionCheck(String server, String line, String rejectiontype, Customer customer);
- public int inputbd(String server, Customer customer, Connection connection);
- public Connection connection();
- public HashSet<String> fetch_customer_code();
- }
- package daoOperation;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.HashSet;
- import connection.ConnectionFactory;
- import connection.ConnectionI;
- import connection.OracleConnection;
- import customerdetails.Customer;
- import validation.Rejection;
- public class DatabaseEntries implements DaoInterface {
- Customer customer;
- Connection connection = null;
- ConnectionI conn = null;
- PreparedStatement ps = null;
- int recordsAffected = 0;
- public Connection connection()
- {
- conn = new OracleConnectionSetup();
- connection = conn.getConnection();
- try {
- connection.setAutoCommit(false);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return connection;
- }
- @Override
- public void conditionCheck(String server, String line, String rejectiontype, Customer customer) {
- if (rejectiontype.equalsIgnoreCase("r")) {
- Rejection r = new Rejection();
- r.recordLevel(server, line, customer);
- } else {
- Rejection r = new Rejection();
- r.fileLevel(server, line, customer);
- }
- }
- public HashSet<String> fetch_customer_code()
- {
- HashSet<String> hashset=new HashSet<String>();
- try
- {
- Connection connection=connection();
- Statement statement=null;
- ResultSet resultset=null;
- statement=connection.createStatement();
- resultset=statement.executeQuery("select customer_code from acustomer_master");
- while(resultset.next())
- {
- String code=resultset.getString(1);
- hashset.add(code);
- }
- }
- catch(Exception e)
- {}
- return hashset;
- }
- public int DataInput(String server, Customer customer, Connection connection) {
- try {
- ps = connection.prepareStatement("insert into acustomer_master values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
- ps.setInt(1, customer.getCustomer_id());
- ps.setString(2, customer.getCustomer_code());
- ps.setString(3, customer.getCustomer_name());
- ps.setString(4, customer.getCustomer_address1());
- ps.setString(5, customer.getCustomer_address2());
- ps.setInt(6, customer.getCustomer_pinCode());
- ps.setString(7, customer.getEmail_address());
- ps.setString(8, customer.getContact_number());
- ps.setString(9, customer.getPrimaryConatctPerson());
- ps.setString(10, customer.getRecord_status());
- ps.setString(11, customer.getActive_inactiveFlag());
- ps.setString(12, customer.getCreate_date());
- ps.setString(13, customer.getCreated_by());
- ps.setString(14, customer.getModified_date());
- ps.setString(15, customer.getModified_by());
- ps.setString(16, customer.getAuthorized_date());
- ps.setString(17, customer.getAuthorized_by());
- rowsAffected = ps.executeUpdate();
- } catch (SQLException e) {
- System.err.println("ERROR OCCURED");
- }
- return rowsAffected;
- }
- }
- package entityPojo_customer;
- public class Customer
- {
- private int customer_id;
- private String customer_code;
- private String customer_name;
- private String customer_address1;
- private String customer_address2;
- private int customer_pinCode;
- private String email_address;
- private String contact_number;
- private String primaryConatctPerson;
- private String record_status;
- private String active_inactiveFlag;
- private String create_date;
- private String created_by;
- private String modified_date;
- private String modified_by;
- private String authorized_date;
- private String authorized_by;
- public int getCustomer_id() {
- return customer_id;
- }
- public void setCustomer_id(int customer_id) {
- this.customer_id = customer_id;
- }
- public String getCustomer_code() {
- return customer_code;
- }
- public void setCustomer_code(String customer_code) {
- this.customer_code = customer_code;
- }
- public String getCustomer_name() {
- return customer_name;
- }
- public void setCustomer_name(String customer_name) {
- this.customer_name = customer_name;
- }
- public String getCustomer_address1() {
- return customer_address1;
- }
- public void setCustomer_address1(String customer_address1) {
- this.customer_address1 = customer_address1;
- }
- public String getCustomer_address2() {
- return customer_address2;
- }
- public void setCustomer_address2(String customer_address2) {
- this.customer_address2 = customer_address2;
- }
- public int getCustomer_pinCode() {
- return customer_pinCode;
- }
- public void setCustomer_pinCode(int customer_pinCode) {
- this.customer_pinCode = customer_pinCode;
- }
- public String getEmail_address() {
- return email_address;
- }
- public void setEmail_address(String email_address) {
- this.email_address = email_address;
- }
- public String getContact_number() {
- return contact_number;
- }
- public void setContact_number(String contact_number) {
- this.contact_number = contact_number;
- }
- public String getPrimaryConatctPerson() {
- return primaryConatctPerson;
- }
- public void setPrimaryConatctPerson(String primaryConatctPerson) {
- this.primaryConatctPerson = primaryConatctPerson;
- }
- public String getRecord_status() {
- return record_status;
- }
- public void setRecord_status(String record_status) {
- this.record_status = record_status;
- }
- public String getActive_inactiveFlag() {
- return active_inactiveFlag;
- }
- public void setActive_inactiveFlag(String active_inactiveFlag) {
- this.active_inactiveFlag = active_inactiveFlag;
- }
- public String getCreate_date() {
- return create_date;
- }
- public void setCreate_date(String create_date) {
- this.create_date = create_date;
- }
- public String getCreated_by() {
- return created_by;
- }
- public void setCreated_by(String created_by) {
- this.created_by = created_by;
- }
- public String getModified_date() {
- return modified_date;
- }
- public void setModified_date(String modified_date) {
- this.modified_date = modified_date;
- }
- public String getModified_by() {
- return modified_by;
- }
- public void setModified_by(String modified_by) {
- this.modified_by = modified_by;
- }
- public String getAuthorized_date() {
- return authorized_date;
- }
- public void setAuthorized_date(String authorized_date) {
- this.authorized_date = authorized_date;
- }
- public String getAuthorized_by() {
- return authorized_by;
- }
- public void setAuthorized_by(String authorized_by) {
- this.authorized_by = authorized_by;
- }
- }
- package main_classes;
- import java.io.BufferedReader;
- import java.io.FileReader;
- //import java.util.HashSet;
- import java.util.Scanner;
- import daoOperation.InsertDao;
- import daoOperation.InsertToDb;
- import customerdetails.Customer;
- //import validation.Rejection;
- //import validation.ValidateMethods;
- public class Input {
- Customer customer = new Customer();
- void input() {
- int count = 1;
- // int affectedRows=0;
- Scanner sc = new Scanner(System.in)
- String server = "Oracle"
- System.out.println("Enter the file with file location");
- String location = sc.next();
- System.out.println("Enter the file name");
- String fileName = sc.next();
- System.out.println("Enter the file extention");
- String fileExtention = sc.next();
- while (!(fileExtention.equals("txt"))) {
- System.out.println("Enter valid file extention");
- fileExtention = sc.next();
- }
- System.out.println("Enter rejection level");
- String rejection = sc.next();
- // ValidateMethods vm=new ValidateMethods();
- String line;
- try {
- FileReader fr = new FileReader(location + ":/" + fileName + "." + fileExtention);
- BufferedReader br = new BufferedReader(fr);
- InsertDao dao = new InsertToDb();
- while ((line = br.readLine()) != null) {
- String st[] = new String[20];
- st = line.split("~");
- try {
- customer.setCustomer_id(count);
- count = count + 1;
- customer.setCustomer_code(st[0]);
- customer.setCustomer_name(st[1]);
- customer.setCustomer_address1(st[2]);
- customer.setCustomer_address2(st[3]);
- customer.setCustomer_pinCode(Integer.parseInt(st[4]));
- customer.setEmail_address(st[5]);
- customer.setContact_number(st[6]);
- customer.setPrimaryConatctPerson(st[7]);
- customer.setRecord_status(st[8]);
- customer.setActive_inactiveFlag(st[9]);
- customer.setCreate_date(st[10]);
- customer.setCreated_by(st[11]);
- customer.setModified_date(st[12]);
- customer.setModified_by(st[13]);
- customer.setAuthorized_date(st[14]);
- customer.setAuthorized_by(st[15]);
- }
- catch (ArrayIndexOutOfBoundsException e) {
- /* e.printStackTrace(); */
- }
- finally {
- dao.conditionCheck(server, line, rejection, customer);
- }
- }
- } catch (Exception e) {
- System.out.println("File not found");
- }
- sc.close();
- }
- }
- package validation;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.HashSet;
- //import connection.ConnectionI;
- //import connection.OracleConnection;
- import daoOperation.InsertDao;
- import daoOperation.InsertToDb;
- import customerdetails.Customer;
- public class Rejection {
- ValidationI vm = new ValidateMethods();
- HashSet<String> set=new HashSet<String>();
- public void recordLevel(String server, String line, Customer customer) {
- InsertDao dao = new InsertToDb();
- // HashSet<String> set=dao.fetch_customer_code();
- boolean code = vm.validCustomerCode(customer, set);
- set.add(customer.getCustomer_code());
- boolean name = vm.validCustomerName(customer);
- boolean pinCode = vm.validPinCode(customer);
- boolean record = vm.validRecordStatus(customer);
- boolean flag = vm.validFlag(customer);
- boolean email = vm.validEmail(customer);
- Connection conn = dao.connection();
- if (code && name && record && pinCode && flag && email) {
- int rowsAffected = dao.inputbd(server, customer, conn);
- if (rowsAffected > 0)
- System.out.println("done");
- else
- System.out.println("Some error ocuured");
- try {
- conn.commit();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- else {
- BufferedWriter bw = null;
- try {
- System.out.println("nhi aa rhe ");
- FileWriter fw = new FileWriter("d:/errorLogFil.txt");
- bw = new BufferedWriter(fw);
- bw.write(line);
- bw.newLine();
- bw.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public void fileLevel(String server, String line, Customer customer) {
- InsertDao dao = new InsertToDb();
- HashSet<String> set=dao.fetch_customer_code();
- boolean code = vm.validCustomerCode(customer, set);
- set.add(customer.getCustomer_code());
- boolean name = vm.validCustomerName(customer);
- boolean pinCode = vm.validPinCode(customer);
- boolean record = vm.validRecordStatus(customer);
- boolean flag = vm.validFlag(customer);
- boolean email = vm.validEmail(customer);
- Connection conn = dao.connection();
- if (code && name && record && pinCode && flag && email) {
- int rowsAffected = dao.inputbd(server, customer, connection);
- try {
- connection.commit();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if (rowsAffected > 0)
- System.out.println("done");
- else
- System.out.println("Some error ocuured");
- }
- else {
- try {
- System.out.println("rollback");
- FileWriter fw = new FileWriter("//C:/Users/temp/Desktop/errorfile.txt");
- BufferedWriter bw = new BufferedWriter(fw);
- bw.append(line);
- bw.newLine();
- bw.flush();
- connection.rollback();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- package validation;
- import java.util.HashSet;
- import java.util.regex.Pattern;
- import customerdetails.Customer;
- public class ValidateMethods implements ValidationI {
- @Override
- public boolean validCustomerCode(Customer customer, HashSet<String> set) {
- if (customer.getCustomer_code().length() > 10) {
- return false;
- } else {
- if (customer.getCustomer_code().equals("")) {
- return false;
- }
- else if (set.contains(customer.getCustomer_code())) {
- return false;
- }
- }
- return true;
- }
- @Override
- public boolean validCustomerName(Customer customer) {
- if (customer.getCustomer_name().length() > 30) {
- return false;
- } else {
- char[] arr = new char[40];
- // int count=0;
- arr = customer.getCustomer_name().toCharArray();
- for (int i = 0; i < customer.getCustomer_name().length(); i++) {
- int k = (int) arr[i];
- if ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 32) {
- // count++;
- } else
- return false;
- }
- }
- return true;
- }
- @Override
- public boolean validPinCode(Customer customer) {
- int length = String.valueOf(customer.getCustomer_pinCode()).length();
- if (length == 6)
- return true;
- else
- return false;
- }
- @Override
- public boolean validEmail(Customer customer) {
- String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\." + "[a-zA-Z0-9_+&*-]+)*@" + "(?:[a-zA-Z0-9-]+\\.)+[a-z"
- + "A-Z]{2,7}$";
- Pattern pat = Pattern.compile(emailRegex);
- if (customer.getEmail_address().equals(""))
- return false;
- else
- return pat.matcher(customer.getEmail_address()).matches();
- }
- @Override
- public boolean validRecordStatus(Customer customer) {
- if (customer.getRecord_status().equalsIgnoreCase("N") || customer.getRecord_status().equalsIgnoreCase("M")
- || customer.getRecord_status().equalsIgnoreCase("D")
- || customer.getRecord_status().equalsIgnoreCase("A")
- || customer.getRecord_status().equalsIgnoreCase("R"))
- return true;
- else
- return false;
- }
- @Override
- public boolean validFlag(Customer customer) {
- if (customer.getActive_inactiveFlag().equalsIgnoreCase("a")
- || customer.getRecord_status().equalsIgnoreCase("I"))
- return true;
- else
- return false;
- }
- }
- package validation;
- import java.util.HashSet;
- import customerdetails.Customer;
- public interface ValidationI {
- public boolean validCustomerCode(Customer customer, HashSet<String> set);
- public boolean validCustomerName(Customer customer);
- public boolean validPinCode(Customer customer);
- public boolean validEmail(Customer customer);
- public boolean validRecordStatus(Customer customer);
- public boolean validFlag(Customer customer);
- }
Add Comment
Please, Sign In to add comment