Guest User

Untitled

a guest
Jul 1st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.86 KB | None | 0 0
  1. package com.nucleus.nsbt.brd1.connectionsetup;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. //import java.sql.*;
  7.  
  8.  
  9. public class OracleConnectionSetup
  10. {
  11. //private static Connection connection = null;
  12. Connection connection = null;
  13.  
  14. //Setup Connection to JDBC -
  15. public Connection getConnection()
  16. {
  17. String driverclass = "oracle.jdbc.driver.OracleDriver";
  18. String url = "jdbc:oracle:thin:@10.1.50.198:1521:orcl";
  19. String username = "sh";
  20. String password = "sh";
  21.  
  22. try
  23. {
  24. Class.forName("driverclass");
  25. connection=DriverManager.getConnection("url","username","password");
  26. System.out.println("Error - \n Driver Class is Successfully Loaded...");
  27. }
  28. catch (ClassNotFoundException e)
  29. {
  30. System.out.println("Error - \n Driver Class Not Loaded...\nClassNotFound Exception Encountered while setting up JDBC connection..");
  31. e.printStackTrace();
  32. }
  33. catch (SQLException e)
  34. {
  35. System.out.println("Error - \n Driver Class Not Loaded...\n SQLException Encountered while setting up JDBC connection");
  36. e.printStackTrace();
  37. }
  38. return connection;
  39. }
  40.  
  41. //Close Connection from JDBC -
  42. public void CloseConnection()
  43. {
  44. try
  45. {
  46. connection.close();
  47. System.out.println("\nJBDC connection has been Closed..");
  48. }
  49.  
  50. catch (SQLException e)
  51. {
  52. e.printStackTrace();
  53. System.out.println("Error in JDBC Connection - \n Connection not Terminated.. ");
  54. }
  55. }
  56. }
  57.  
  58.  
  59.  
  60.  
  61. package daoOperation;
  62.  
  63. import java.sql.Connection;
  64. import java.util.HashSet;
  65.  
  66. import CustomerDetails.Customer;
  67.  
  68. public interface DaoInterface {
  69. public void conditionCheck(String server, String line, String rejectiontype, Customer customer);
  70.  
  71. public int inputbd(String server, Customer customer, Connection connection);
  72.  
  73. public Connection connection();
  74. public HashSet<String> fetch_customer_code();
  75. }
  76.  
  77.  
  78.  
  79. package daoOperation;
  80.  
  81. import java.sql.Connection;
  82. import java.sql.PreparedStatement;
  83. import java.sql.ResultSet;
  84. import java.sql.SQLException;
  85. import java.sql.Statement;
  86. import java.util.ArrayList;
  87. import java.util.HashSet;
  88.  
  89. import connection.ConnectionFactory;
  90. import connection.ConnectionI;
  91. import connection.OracleConnection;
  92. import customerdetails.Customer;
  93.  
  94. import validation.Rejection;
  95.  
  96. public class DatabaseEntries implements DaoInterface {
  97. Customer customer;
  98. Connection connection = null;
  99. ConnectionI conn = null;
  100. PreparedStatement ps = null;
  101. int recordsAffected = 0;
  102.  
  103. public Connection connection()
  104. {
  105. conn = new OracleConnectionSetup();
  106. connection = conn.getConnection();
  107. try {
  108. connection.setAutoCommit(false);
  109. } catch (SQLException e) {
  110. // TODO Auto-generated catch block
  111. e.printStackTrace();
  112. }
  113. return connection;
  114. }
  115.  
  116. @Override
  117. public void conditionCheck(String server, String line, String rejectiontype, Customer customer) {
  118. if (rejectiontype.equalsIgnoreCase("r")) {
  119. Rejection r = new Rejection();
  120.  
  121. r.recordLevel(server, line, customer);
  122. } else {
  123.  
  124. Rejection r = new Rejection();
  125. r.fileLevel(server, line, customer);
  126. }
  127. }
  128.  
  129. public HashSet<String> fetch_customer_code()
  130. {
  131. HashSet<String> hashset=new HashSet<String>();
  132.  
  133. try
  134. {
  135. Connection connection=connection();
  136. Statement statement=null;
  137. ResultSet resultset=null;
  138. statement=connection.createStatement();
  139. resultset=statement.executeQuery("select customer_code from acustomer_master");
  140.  
  141. while(resultset.next())
  142. {
  143. String code=resultset.getString(1);
  144.  
  145. hashset.add(code);
  146. }
  147.  
  148. }
  149. catch(Exception e)
  150. {}
  151. return hashset;
  152. }
  153.  
  154. public int DataInput(String server, Customer customer, Connection connection) {
  155. try {
  156.  
  157. ps = connection.prepareStatement("insert into acustomer_master values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
  158.  
  159. ps.setInt(1, customer.getCustomer_id());
  160. ps.setString(2, customer.getCustomer_code());
  161. ps.setString(3, customer.getCustomer_name());
  162. ps.setString(4, customer.getCustomer_address1());
  163. ps.setString(5, customer.getCustomer_address2());
  164. ps.setInt(6, customer.getCustomer_pinCode());
  165. ps.setString(7, customer.getEmail_address());
  166. ps.setString(8, customer.getContact_number());
  167. ps.setString(9, customer.getPrimaryConatctPerson());
  168. ps.setString(10, customer.getRecord_status());
  169. ps.setString(11, customer.getActive_inactiveFlag());
  170. ps.setString(12, customer.getCreate_date());
  171. ps.setString(13, customer.getCreated_by());
  172. ps.setString(14, customer.getModified_date());
  173. ps.setString(15, customer.getModified_by());
  174. ps.setString(16, customer.getAuthorized_date());
  175. ps.setString(17, customer.getAuthorized_by());
  176.  
  177. rowsAffected = ps.executeUpdate();
  178. } catch (SQLException e) {
  179. System.err.println("ERROR OCCURED");
  180. }
  181.  
  182. return rowsAffected;
  183. }
  184.  
  185. }
  186.  
  187.  
  188. package entityPojo_customer;
  189.  
  190. public class Customer
  191. {
  192. private int customer_id;
  193. private String customer_code;
  194. private String customer_name;
  195. private String customer_address1;
  196. private String customer_address2;
  197. private int customer_pinCode;
  198. private String email_address;
  199. private String contact_number;
  200. private String primaryConatctPerson;
  201. private String record_status;
  202. private String active_inactiveFlag;
  203. private String create_date;
  204. private String created_by;
  205. private String modified_date;
  206. private String modified_by;
  207. private String authorized_date;
  208. private String authorized_by;
  209.  
  210. public int getCustomer_id() {
  211. return customer_id;
  212. }
  213. public void setCustomer_id(int customer_id) {
  214. this.customer_id = customer_id;
  215. }
  216. public String getCustomer_code() {
  217. return customer_code;
  218. }
  219. public void setCustomer_code(String customer_code) {
  220. this.customer_code = customer_code;
  221. }
  222. public String getCustomer_name() {
  223. return customer_name;
  224. }
  225. public void setCustomer_name(String customer_name) {
  226. this.customer_name = customer_name;
  227. }
  228. public String getCustomer_address1() {
  229. return customer_address1;
  230. }
  231. public void setCustomer_address1(String customer_address1) {
  232. this.customer_address1 = customer_address1;
  233. }
  234. public String getCustomer_address2() {
  235. return customer_address2;
  236. }
  237. public void setCustomer_address2(String customer_address2) {
  238. this.customer_address2 = customer_address2;
  239. }
  240. public int getCustomer_pinCode() {
  241. return customer_pinCode;
  242. }
  243. public void setCustomer_pinCode(int customer_pinCode) {
  244. this.customer_pinCode = customer_pinCode;
  245. }
  246. public String getEmail_address() {
  247. return email_address;
  248. }
  249. public void setEmail_address(String email_address) {
  250. this.email_address = email_address;
  251. }
  252. public String getContact_number() {
  253. return contact_number;
  254. }
  255. public void setContact_number(String contact_number) {
  256. this.contact_number = contact_number;
  257. }
  258. public String getPrimaryConatctPerson() {
  259. return primaryConatctPerson;
  260. }
  261. public void setPrimaryConatctPerson(String primaryConatctPerson) {
  262. this.primaryConatctPerson = primaryConatctPerson;
  263. }
  264. public String getRecord_status() {
  265. return record_status;
  266. }
  267. public void setRecord_status(String record_status) {
  268. this.record_status = record_status;
  269. }
  270. public String getActive_inactiveFlag() {
  271. return active_inactiveFlag;
  272. }
  273. public void setActive_inactiveFlag(String active_inactiveFlag) {
  274. this.active_inactiveFlag = active_inactiveFlag;
  275. }
  276. public String getCreate_date() {
  277. return create_date;
  278. }
  279. public void setCreate_date(String create_date) {
  280. this.create_date = create_date;
  281. }
  282. public String getCreated_by() {
  283. return created_by;
  284. }
  285. public void setCreated_by(String created_by) {
  286. this.created_by = created_by;
  287. }
  288. public String getModified_date() {
  289. return modified_date;
  290. }
  291. public void setModified_date(String modified_date) {
  292. this.modified_date = modified_date;
  293. }
  294. public String getModified_by() {
  295. return modified_by;
  296. }
  297. public void setModified_by(String modified_by) {
  298. this.modified_by = modified_by;
  299. }
  300. public String getAuthorized_date() {
  301. return authorized_date;
  302. }
  303. public void setAuthorized_date(String authorized_date) {
  304. this.authorized_date = authorized_date;
  305. }
  306. public String getAuthorized_by() {
  307. return authorized_by;
  308. }
  309. public void setAuthorized_by(String authorized_by) {
  310. this.authorized_by = authorized_by;
  311. }
  312.  
  313.  
  314.  
  315. }
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326. package main_classes;
  327.  
  328. import java.io.BufferedReader;
  329. import java.io.FileReader;
  330. //import java.util.HashSet;
  331. import java.util.Scanner;
  332.  
  333. import daoOperation.InsertDao;
  334. import daoOperation.InsertToDb;
  335. import customerdetails.Customer;
  336. //import validation.Rejection;
  337. //import validation.ValidateMethods;
  338.  
  339. public class Input {
  340. Customer customer = new Customer();
  341.  
  342. void input() {
  343. int count = 1;
  344. // int affectedRows=0;
  345.  
  346. Scanner sc = new Scanner(System.in)
  347. String server = "Oracle"
  348. System.out.println("Enter the file with file location");
  349. String location = sc.next();
  350. System.out.println("Enter the file name");
  351. String fileName = sc.next();
  352. System.out.println("Enter the file extention");
  353. String fileExtention = sc.next();
  354.  
  355. while (!(fileExtention.equals("txt"))) {
  356. System.out.println("Enter valid file extention");
  357. fileExtention = sc.next();
  358. }
  359.  
  360. System.out.println("Enter rejection level");
  361. String rejection = sc.next();
  362.  
  363. // ValidateMethods vm=new ValidateMethods();
  364. String line;
  365. try {
  366. FileReader fr = new FileReader(location + ":/" + fileName + "." + fileExtention);
  367. BufferedReader br = new BufferedReader(fr);
  368. InsertDao dao = new InsertToDb();
  369.  
  370. while ((line = br.readLine()) != null) {
  371. String st[] = new String[20];
  372. st = line.split("~");
  373. try {
  374. customer.setCustomer_id(count);
  375. count = count + 1;
  376. customer.setCustomer_code(st[0]);
  377. customer.setCustomer_name(st[1]);
  378. customer.setCustomer_address1(st[2]);
  379. customer.setCustomer_address2(st[3]);
  380. customer.setCustomer_pinCode(Integer.parseInt(st[4]));
  381. customer.setEmail_address(st[5]);
  382. customer.setContact_number(st[6]);
  383. customer.setPrimaryConatctPerson(st[7]);
  384. customer.setRecord_status(st[8]);
  385. customer.setActive_inactiveFlag(st[9]);
  386. customer.setCreate_date(st[10]);
  387. customer.setCreated_by(st[11]);
  388. customer.setModified_date(st[12]);
  389. customer.setModified_by(st[13]);
  390. customer.setAuthorized_date(st[14]);
  391. customer.setAuthorized_by(st[15]);
  392. }
  393.  
  394. catch (ArrayIndexOutOfBoundsException e) {
  395. /* e.printStackTrace(); */
  396. }
  397.  
  398. finally {
  399.  
  400. dao.conditionCheck(server, line, rejection, customer);
  401. }
  402.  
  403. }
  404.  
  405. } catch (Exception e) {
  406. System.out.println("File not found");
  407. }
  408.  
  409. sc.close();
  410. }
  411. }
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424. package validation;
  425.  
  426. import java.io.BufferedWriter;
  427. import java.io.FileWriter;
  428. import java.io.IOException;
  429. import java.sql.Connection;
  430. import java.sql.SQLException;
  431. import java.util.HashSet;
  432.  
  433. //import connection.ConnectionI;
  434. //import connection.OracleConnection;
  435. import daoOperation.InsertDao;
  436. import daoOperation.InsertToDb;
  437. import customerdetails.Customer;
  438.  
  439. public class Rejection {
  440.  
  441. ValidationI vm = new ValidateMethods();
  442. HashSet<String> set=new HashSet<String>();
  443.  
  444. public void recordLevel(String server, String line, Customer customer) {
  445. InsertDao dao = new InsertToDb();
  446. // HashSet<String> set=dao.fetch_customer_code();
  447. boolean code = vm.validCustomerCode(customer, set);
  448. set.add(customer.getCustomer_code());
  449.  
  450. boolean name = vm.validCustomerName(customer);
  451.  
  452. boolean pinCode = vm.validPinCode(customer);
  453.  
  454. boolean record = vm.validRecordStatus(customer);
  455.  
  456. boolean flag = vm.validFlag(customer);
  457.  
  458. boolean email = vm.validEmail(customer);
  459.  
  460. Connection conn = dao.connection();
  461. if (code && name && record && pinCode && flag && email) {
  462. int rowsAffected = dao.inputbd(server, customer, conn);
  463.  
  464. if (rowsAffected > 0)
  465. System.out.println("done");
  466.  
  467. else
  468. System.out.println("Some error ocuured");
  469.  
  470. try {
  471. conn.commit();
  472. } catch (SQLException e) {
  473. e.printStackTrace();
  474. }
  475. }
  476. else {
  477. BufferedWriter bw = null;
  478. try {
  479. System.out.println("nhi aa rhe ");
  480. FileWriter fw = new FileWriter("d:/errorLogFil.txt");
  481. bw = new BufferedWriter(fw);
  482. bw.write(line);
  483. bw.newLine();
  484. bw.flush();
  485. } catch (IOException e) {
  486. e.printStackTrace();
  487. }
  488.  
  489. }
  490. }
  491.  
  492. public void fileLevel(String server, String line, Customer customer) {
  493. InsertDao dao = new InsertToDb();
  494. HashSet<String> set=dao.fetch_customer_code();
  495. boolean code = vm.validCustomerCode(customer, set);
  496. set.add(customer.getCustomer_code());
  497. boolean name = vm.validCustomerName(customer);
  498.  
  499. boolean pinCode = vm.validPinCode(customer);
  500.  
  501. boolean record = vm.validRecordStatus(customer);
  502.  
  503. boolean flag = vm.validFlag(customer);
  504.  
  505. boolean email = vm.validEmail(customer);
  506.  
  507. Connection conn = dao.connection();
  508.  
  509. if (code && name && record && pinCode && flag && email) {
  510. int rowsAffected = dao.inputbd(server, customer, connection);
  511. try {
  512. connection.commit();
  513. } catch (SQLException e) {
  514. // TODO Auto-generated catch block
  515. e.printStackTrace();
  516. }
  517.  
  518. if (rowsAffected > 0)
  519. System.out.println("done");
  520. else
  521. System.out.println("Some error ocuured");
  522. }
  523. else {
  524. try {
  525. System.out.println("rollback");
  526. FileWriter fw = new FileWriter("//C:/Users/temp/Desktop/errorfile.txt");
  527. BufferedWriter bw = new BufferedWriter(fw);
  528. bw.append(line);
  529. bw.newLine();
  530. bw.flush();
  531. connection.rollback();
  532.  
  533.  
  534. } catch (Exception e) {
  535. e.printStackTrace();
  536. }
  537.  
  538. }
  539. }
  540. }
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552. package validation;
  553.  
  554. import java.util.HashSet;
  555. import java.util.regex.Pattern;
  556.  
  557. import customerdetails.Customer;
  558.  
  559. public class ValidateMethods implements ValidationI {
  560.  
  561. @Override
  562. public boolean validCustomerCode(Customer customer, HashSet<String> set) {
  563. if (customer.getCustomer_code().length() > 10) {
  564. return false;
  565. } else {
  566. if (customer.getCustomer_code().equals("")) {
  567. return false;
  568. }
  569.  
  570. else if (set.contains(customer.getCustomer_code())) {
  571. return false;
  572. }
  573. }
  574.  
  575. return true;
  576.  
  577. }
  578.  
  579. @Override
  580. public boolean validCustomerName(Customer customer) {
  581. if (customer.getCustomer_name().length() > 30) {
  582. return false;
  583. } else {
  584. char[] arr = new char[40];
  585. // int count=0;
  586.  
  587. arr = customer.getCustomer_name().toCharArray();
  588. for (int i = 0; i < customer.getCustomer_name().length(); i++) {
  589. int k = (int) arr[i];
  590. if ((k > 47 && k < 58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 32) {
  591. // count++;
  592. } else
  593. return false;
  594. }
  595. }
  596. return true;
  597.  
  598. }
  599.  
  600. @Override
  601. public boolean validPinCode(Customer customer) {
  602. int length = String.valueOf(customer.getCustomer_pinCode()).length();
  603. if (length == 6)
  604. return true;
  605. else
  606. return false;
  607. }
  608.  
  609. @Override
  610. public boolean validEmail(Customer customer) {
  611.  
  612. String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\." + "[a-zA-Z0-9_+&*-]+)*@" + "(?:[a-zA-Z0-9-]+\\.)+[a-z"
  613. + "A-Z]{2,7}$";
  614. Pattern pat = Pattern.compile(emailRegex);
  615. if (customer.getEmail_address().equals(""))
  616. return false;
  617. else
  618. return pat.matcher(customer.getEmail_address()).matches();
  619. }
  620.  
  621. @Override
  622. public boolean validRecordStatus(Customer customer) {
  623. if (customer.getRecord_status().equalsIgnoreCase("N") || customer.getRecord_status().equalsIgnoreCase("M")
  624. || customer.getRecord_status().equalsIgnoreCase("D")
  625. || customer.getRecord_status().equalsIgnoreCase("A")
  626. || customer.getRecord_status().equalsIgnoreCase("R"))
  627. return true;
  628. else
  629. return false;
  630. }
  631.  
  632. @Override
  633. public boolean validFlag(Customer customer) {
  634. if (customer.getActive_inactiveFlag().equalsIgnoreCase("a")
  635. || customer.getRecord_status().equalsIgnoreCase("I"))
  636. return true;
  637. else
  638. return false;
  639. }
  640.  
  641. }
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654. package validation;
  655.  
  656. import java.util.HashSet;
  657.  
  658. import customerdetails.Customer;
  659.  
  660. public interface ValidationI {
  661.  
  662. public boolean validCustomerCode(Customer customer, HashSet<String> set);
  663.  
  664. public boolean validCustomerName(Customer customer);
  665.  
  666. public boolean validPinCode(Customer customer);
  667.  
  668. public boolean validEmail(Customer customer);
  669.  
  670. public boolean validRecordStatus(Customer customer);
  671.  
  672. public boolean validFlag(Customer customer);
  673.  
  674. }
Add Comment
Please, Sign In to add comment