Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.04 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.sql.*;
  5. import java.util.Scanner;
  6.  
  7. public class Serwer2 {
  8. static Connection c = null;
  9. static Scanner sc = new Scanner(System.in);
  10.  
  11. public static String logowanie(Socket s) throws IOException, SQLException {
  12. System.out.println("checkpoint logowanie 1");
  13. BufferedReader din = new BufferedReader(
  14. new InputStreamReader(
  15. s.getInputStream()));
  16. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  17.  
  18. System.out.println("checkpoint logowanie 2");
  19. String login = "", haslo = "", wynik = "";
  20. char prawaC;
  21.  
  22. System.out.println("checkpoint logowanie 3");
  23. do {
  24. System.out.println("checkpoint logowanie 4");
  25. login = din.readLine();
  26. System.out.println("login: "+login);
  27. System.out.println("checkpoint logowanie 5");
  28. haslo = din.readLine();
  29. System.out.println("haslo: "+haslo);
  30.  
  31. System.out.println("checkpoint logowanie 6");
  32. wynik = szukaj(login, haslo);
  33. System.out.println(wynik);
  34. dout.writeUTF(wynik);
  35. } while (!wynik.equals("ok"));
  36.  
  37. System.out.println("checkpoint logowanie 7");
  38.  
  39. prawaC = login.charAt(0);
  40.  
  41. if (prawaC == '0') {
  42. dout.writeUTF("Admin");
  43. } else if (prawaC == '1' || prawaC == '2') {
  44. dout.writeUTF("Pracownik");
  45. } else {
  46. dout.writeUTF("Uzytkownik");
  47. }
  48.  
  49. return login;
  50. }
  51.  
  52. public static String sprawdzPrawa(String login) throws SQLException {
  53. String prawa = "";
  54. char prawaC;
  55.  
  56.  
  57. prawaC = login.charAt(0);
  58.  
  59. if (prawaC == '0') {
  60. prawa = "Admin";
  61. } else if (prawaC == '1' || prawaC == '2') {
  62. prawa = "Pracownik";
  63. } else {
  64. prawa = "Uzytkownik";
  65. }
  66.  
  67. return prawa;
  68. }
  69.  
  70. public static String szukaj(String login, String haslo) throws IOException, SQLException {
  71. /*String wynik = "not", hasloTest = "";
  72.  
  73. FileReader fr = null;
  74. try {
  75. fr = new FileReader("pliki/hasla/" + login + ".txt");
  76. } catch (FileNotFoundException e) {
  77. fr = new FileReader("pliki/hasla/00000000.txt");
  78. }
  79.  
  80. BufferedReader br = new BufferedReader(fr);
  81. try {
  82. hasloTest = br.readLine();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86.  
  87. try {
  88. br.close();
  89. fr.close();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93.  
  94. if(haslo.equals(hasloTest)){
  95. wynik = "ok";
  96. }else{
  97. wynik = "not";
  98. }
  99. return wynik;*/
  100. String wynik = "notl";
  101. int i = 0;
  102. Statement stmt = null;
  103. ResultSet rs = null;
  104. stmt = c.createStatement();
  105. rs = stmt.executeQuery("SELECT Login FROM Konta");
  106. while (rs.next()) {
  107. String name = rs.getString(1);
  108. if (name.equals(login)) wynik = "ok";
  109. i++;
  110. }
  111. //if (wynik.equals("notl")) return wynik;
  112. rs = stmt.executeQuery("SELECT Haslo FROM Hasla WHERE ID = '" + i + "'");
  113. while (rs.next()) {
  114. String name = rs.getString(1);
  115. if (name.equals(haslo)) wynik = "ok";
  116. }
  117. if (rs != null) {
  118. try {
  119. rs.close();
  120. } catch (SQLException sqlEx) {
  121. } // ignore
  122. rs = null;
  123. }
  124. if (stmt != null) {
  125. try {
  126. stmt.close();
  127. } catch (SQLException sqlEx) {
  128. } // ignore
  129.  
  130. stmt = null;
  131. }
  132.  
  133. return wynik;
  134.  
  135. }
  136.  
  137. public static void utworzKonto(Socket s) throws IOException, SQLException {
  138. DataInputStream din = new DataInputStream(s.getInputStream());
  139.  
  140. String login = "", imie = "", nazwisko = "", pesel = "", nrKonta = "", haslo = "";
  141. char loginC;
  142.  
  143. imie = din.readUTF();
  144. nazwisko = din.readUTF();
  145. pesel = din.readUTF();
  146. haslo = din.readUTF();
  147. login = din.readUTF();
  148. nrKonta = din.readUTF();
  149.  
  150. loginC = login.charAt(0);
  151. if (loginC == '1' || loginC == '2') {
  152. dodajPracownika(nazwisko, imie, pesel, haslo, login);
  153. } else {
  154. dodajUzytkownika(nazwisko, imie, pesel, haslo, login, nrKonta);
  155. }
  156. }
  157.  
  158. public static void dodajPracownika(String nazwisko, String imie, String pesel, String haslo, String login) throws SQLException {
  159. /*FileWriter fwdane = null;
  160. FileWriter fwhasla = null;
  161.  
  162. try {
  163. fwdane = new FileWriter("pliki/dane/" + login + ".txt");
  164. fwhasla = new FileWriter("pliki/hasla/" + login + ".txt");
  165. }
  166. catch (IOException e) {
  167. e.printStackTrace();
  168. }
  169.  
  170. BufferedWriter bwdane = new BufferedWriter(fwdane);
  171. BufferedWriter bwhasla = new BufferedWriter(fwhasla);
  172.  
  173. try {
  174. bwdane.write(imie + "\r\n" + nazwisko + "\r\n" + pesel);
  175. bwhasla.write("password");
  176. }
  177. catch (IOException e) {
  178. e.printStackTrace();
  179. }
  180.  
  181. try {
  182. bwdane.close();
  183. fwdane.close();
  184. bwhasla.close();
  185. fwhasla.close();
  186. }
  187. catch (IOException e) {
  188. e.printStackTrace();
  189. }*/
  190. int i = 1;
  191. Statement stmt = null;
  192. ResultSet rs = null;
  193. stmt = c.createStatement();
  194. rs = stmt.executeQuery("SELECT ID FROM Dane");
  195. while (rs.next()) {
  196. String name = rs.getString(1);
  197. if (!(name.equals(i))) break;
  198. i++;
  199. }
  200. stmt.executeUpdate("INSERT INTO Dane " + "VALUES ('" + i + "','" + nazwisko + "','" + imie + "','" + pesel + "')");
  201. stmt.executeUpdate("INSERT INTO Hasla " + "VALUES ('" + i + "','" + haslo + "')");
  202. //Random generator = new Random();
  203. /*do {
  204. losowa = generator.nextInt(100000000);
  205. } while (losowa < 10000000);*/
  206. /*do {
  207. losowa = generator.nextInt(300000);
  208. } while (losowa < 100000);
  209. los = losowa+"";*/
  210. stmt.executeUpdate("INSERT INTO Konta " + "VALUES ('nd','" + i + "','" + login + "','" + "'nd','nd')");
  211. if (rs != null) {
  212. try {
  213. rs.close();
  214. } catch (SQLException sqlEx) {
  215. } // ignore
  216. rs = null;
  217. }
  218.  
  219. if (stmt != null) {
  220. try {
  221. stmt.close();
  222. } catch (SQLException sqlEx) {
  223. } // ignore
  224.  
  225. stmt = null;
  226. }
  227. }
  228.  
  229. public static void dodajUzytkownika(String nazwisko, String imie, String pesel, String haslo, String login, String nrKonta) throws SQLException {
  230. /*FileWriter fwdane = null;
  231. FileWriter fwhasla = null;
  232. FileWriter fwkonto = null;
  233.  
  234. try {
  235. fwdane = new FileWriter("pliki/dane/" + login + ".txt");
  236. fwhasla = new FileWriter("pliki/hasla/" + login + ".txt");
  237. fwkonto = new FileWriter("pliki/konta/" + nrKonta + ".txt");
  238. }
  239. catch (IOException e) {
  240. e.printStackTrace();
  241. }
  242.  
  243. BufferedWriter bwdane = new BufferedWriter(fwdane);
  244. BufferedWriter bwhasla = new BufferedWriter(fwhasla);
  245. BufferedWriter bwkonto = new BufferedWriter(fwkonto);
  246.  
  247. try {
  248. bwdane.write(imie + "\r\n" + nazwisko + "\r\n" + pesel + "\r\n" + nrKonta);
  249. bwhasla.write("password");
  250. bwkonto.write(login + "\r\n" + "0" + "\r\n" + "0");
  251. }
  252. catch (IOException e) {
  253. e.printStackTrace();
  254. }
  255.  
  256. try {
  257. bwdane.close();
  258. fwdane.close();
  259. bwhasla.close();
  260. fwhasla.close();
  261. bwkonto.close();
  262. fwkonto.close();
  263. }
  264. catch (IOException e) {
  265. e.printStackTrace();
  266. }*/
  267. int i = 1;
  268. Statement stmt = null;
  269. ResultSet rs = null;
  270. stmt = c.createStatement();
  271. rs = stmt.executeQuery("SELECT ID FROM Dane");
  272. while (rs.next()) {
  273. String name = rs.getString(1);
  274. if (!(name.equals(i))) break;
  275. i++;
  276. }
  277. stmt.executeUpdate("INSERT INTO Dane " + "VALUES ('" + i + "','" + nazwisko + "','" + imie + "','" + pesel + "')");
  278. stmt.executeUpdate("INSERT INTO Hasla " + "VALUES ('" + i + "','" + haslo + "')");
  279. /*Random generator = new Random();
  280. do {
  281. losowa = generator.nextInt(100000000);
  282. } while (losowa < 10000000);
  283. do {
  284. losowa2 = generator.nextInt(300000);
  285. } while (losowa2 < 100000);
  286. los = losowa+"";
  287. los2 = losowa2+"";*/
  288. stmt.executeUpdate("INSERT INTO Konta " + "VALUES ('" + nrKonta + "','" + i + "','" + login + "','" + "'0','0')");
  289. if (rs != null) {
  290. try {
  291. rs.close();
  292. } catch (SQLException sqlEx) {
  293. } // ignore
  294. rs = null;
  295. }
  296.  
  297. if (stmt != null) {
  298. try {
  299. stmt.close();
  300. } catch (SQLException sqlEx) {
  301. } // ignore
  302.  
  303. stmt = null;
  304. }
  305.  
  306. }
  307.  
  308. public static void usunKonto(Socket s) throws IOException, SQLException {
  309. DataInputStream din = new DataInputStream(s.getInputStream());
  310. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  311.  
  312. String login = "", typ = "";
  313.  
  314. typ = din.readUTF();
  315. login = din.readUTF();
  316. /*
  317. if(typ.equals("prac")){
  318. try{
  319. File fdhasla = new File("pliki/hasla/" + login + ".txt");
  320. File fddane = new File("pliki/dane/" + login + ".txt");
  321. if(fdhasla.delete() && fddane.delete()){
  322. wynik = "ok";
  323. }
  324. else{
  325. wynik = "not";
  326. }
  327.  
  328. }catch(Exception e){
  329. wynik = "err";
  330. }
  331. }
  332. else if(typ.equals("uzyt")){
  333. nrKonta = sprawdzKonto(login);
  334.  
  335. try{
  336. File fdhasla = new File("pliki/hasla/" + login + ".txt");
  337. File fddane = new File("pliki/dane/" + login + ".txt");
  338. File fdkonto = new File("pliki/konta/" + nrKonta + ".txt");
  339.  
  340. if(fdhasla.delete() && fddane.delete() && fdkonto.delete()){
  341. wynik = "ok";
  342. }
  343. else{
  344. wynik = "not";
  345. }
  346.  
  347. } catch(Exception e){
  348. wynik = "err";
  349. }
  350. }
  351. else if(typ.equals("not")){
  352. try{
  353. wynik = "ok";
  354. }
  355. catch(Exception e){
  356. wynik = "err";
  357. }
  358. }
  359. dout.writeUTF(wynik);*/
  360. String i = "", wynik = "ok";
  361. Statement stmt = null;
  362. ResultSet rs = null;
  363. stmt = c.createStatement();
  364. rs = stmt.executeQuery("SELECT ID FROM Konta WHERE Login = '" + login + "'");
  365. while (rs.next()) {
  366. String name = rs.getString(1);
  367. i = name;
  368. }
  369. stmt.executeUpdate("DELETE FROM Konta WHERE Login = '" + login + "'");
  370. stmt.executeUpdate("DELETE FROM Dane WHERE ID = '" + i + "'");
  371. stmt.executeUpdate("DELETE FROM Hasla WHERE ID = '" + i + "'");
  372. if (rs != null) {
  373. try {
  374. rs.close();
  375. } catch (SQLException sqlEx) {
  376. } // ignore
  377. rs = null;
  378. }
  379.  
  380. if (stmt != null) {
  381. try {
  382. stmt.close();
  383. } catch (SQLException sqlEx) {
  384. } // ignore
  385.  
  386. stmt = null;
  387. }
  388. dout.writeUTF(wynik);
  389. }
  390.  
  391. public static String sprawdzKonto(String login) throws IOException, SQLException {
  392. String nrKonta = "";
  393.  
  394. /*FileReader fr = null;
  395. try {
  396. fr = new FileReader("pliki/dane/" + login + ".txt");
  397. } catch (FileNotFoundException e) {
  398. nrKonta = "0";
  399. }
  400.  
  401. BufferedReader br = new BufferedReader(fr);
  402. try {
  403. for(int i=0; i<4; i++){
  404. nrKonta = br.readLine();
  405. }
  406. } catch (IOException e) {
  407. e.printStackTrace();
  408. }
  409.  
  410. try {
  411. br.close();
  412. fr.close();
  413. } catch (IOException e) {
  414. e.printStackTrace();
  415. }*/
  416. Statement stmt = null;
  417. ResultSet rs = null;
  418. stmt = c.createStatement();
  419. rs = stmt.executeQuery("SELECT NrKonta FROM Konta WHERE Login = '" + login + "'");
  420. while (rs.next()) {
  421. String name = rs.getString(1);
  422. nrKonta = name;
  423. }
  424. if (rs != null) {
  425. try {
  426. rs.close();
  427. } catch (SQLException sqlEx) {
  428. } // ignore
  429. rs = null;
  430. }
  431.  
  432. if (stmt != null) {
  433. try {
  434. stmt.close();
  435. } catch (SQLException sqlEx) {
  436. } // ignore
  437.  
  438. stmt = null;
  439. }
  440. return nrKonta;
  441. }
  442.  
  443. public static void zmienHaslo(Socket s) throws IOException, SQLException {
  444. DataInputStream din = new DataInputStream(s.getInputStream());
  445. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  446.  
  447. String login = "", hasloS = "", hasloN = "", wyjscie = "";
  448.  
  449. login = din.readUTF();
  450. hasloS = din.readUTF();
  451.  
  452. wyjscie = sprawdzHaslo(login, hasloS);
  453. dout.writeUTF(wyjscie);
  454.  
  455. if (wyjscie.equals("ok")) {
  456. hasloN = din.readUTF();
  457.  
  458. wyjscie = dokonajZmianyHasla(login, hasloN);
  459. dout.writeUTF(wyjscie);
  460. }
  461. }
  462.  
  463. public static String sprawdzHaslo(String login, String hasloS) throws SQLException {
  464. /*String potwierdzenie = "", hasloPlik = "";
  465.  
  466. FileReader fr = null;
  467. try {
  468. fr = new FileReader("pliki/hasla/" + login + ".txt");
  469. } catch (FileNotFoundException e) {
  470. potwierdzenie = "err";
  471. }
  472.  
  473. BufferedReader br = new BufferedReader(fr);
  474. try {
  475. hasloPlik = br.readLine();
  476. } catch (IOException e) {
  477. potwierdzenie = "err";
  478. }
  479.  
  480. try {
  481. br.close();
  482. fr.close();
  483. } catch (IOException e) {
  484. potwierdzenie = "err";
  485. }
  486.  
  487. if(hasloPlik.equals(hasloS)){
  488. potwierdzenie = "ok";
  489. }
  490. else{
  491. potwierdzenie = "not";
  492. }
  493.  
  494. return potwierdzenie;*/
  495. String i = "", potwierdzenie = "", hasloT = "";
  496. Statement stmt = null;
  497. ResultSet rs = null;
  498. stmt = c.createStatement();
  499. rs = stmt.executeQuery("SELECT ID FROM Konta WHERE Login = '" + login + "'");
  500. while (rs.next()) {
  501. String name = rs.getString(1);
  502. i = name;
  503. }
  504. rs = stmt.executeQuery("SELECT Haslo FROM Hasla WHERE ID = '" + i + "'");
  505. while (rs.next()) {
  506. String name = rs.getString(1);
  507. hasloT = name;
  508. }
  509. if (hasloT.equals(hasloS)) potwierdzenie = "ok";
  510. else potwierdzenie = "not";
  511. if (rs != null) {
  512. try {
  513. rs.close();
  514. } catch (SQLException sqlEx) {
  515. } // ignore
  516. rs = null;
  517. }
  518.  
  519. if (stmt != null) {
  520. try {
  521. stmt.close();
  522. } catch (SQLException sqlEx) {
  523. } // ignore
  524.  
  525. stmt = null;
  526. }
  527. return potwierdzenie;
  528. }
  529.  
  530. public static String dokonajZmianyHasla(String login, String hasloN) throws SQLException {
  531. /*String potwierdzenie = "ok";
  532.  
  533. FileWriter fwhaslo = null;
  534. try {
  535. fwhaslo = new FileWriter("pliki/hasla/" + login + ".txt");
  536. }
  537. catch (IOException e) {
  538. potwierdzenie = "err";
  539. }
  540.  
  541. BufferedWriter bwhaslo = new BufferedWriter(fwhaslo);
  542. try {
  543. bwhaslo.write(hasloN);
  544. }
  545. catch (IOException e) {
  546. potwierdzenie = "err";
  547. }
  548.  
  549. try {
  550. bwhaslo.close();
  551. fwhaslo.close();
  552. }
  553. catch (IOException e) {
  554. potwierdzenie = "err";
  555. }
  556.  
  557. return potwierdzenie;*/
  558. String i = "", potwierdzenie = "";
  559. Statement stmt = null;
  560. ResultSet rs = null;
  561. stmt = c.createStatement();
  562. rs = stmt.executeQuery("SELECT ID FROM Konta WHERE Login = '" + login + "'");
  563. while (rs.next()) {
  564. String name = rs.getString(1);
  565. i = name;
  566. }
  567. stmt.executeUpdate("UPDATE Hasla SET Haslo = '" + hasloN + "' WHERE ID = '" + i + "'");
  568. if (rs != null) {
  569. try {
  570. rs.close();
  571. } catch (SQLException sqlEx) {
  572. } // ignore
  573. rs = null;
  574. }
  575.  
  576. if (stmt != null) {
  577. try {
  578. stmt.close();
  579. } catch (SQLException sqlEx) {
  580. } // ignore
  581.  
  582. stmt = null;
  583. }
  584. return potwierdzenie;
  585.  
  586. }
  587.  
  588. public static void wplataGotowki(Socket s) throws IOException, SQLException {
  589. DataInputStream din = new DataInputStream(s.getInputStream());
  590. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  591.  
  592. String login = "", potwierdzenie = "", nrKonta = "", wartosc = "", konto = "", akcja = "";
  593. char loginC;
  594.  
  595. login = din.readUTF();
  596. loginC = login.charAt(0);
  597.  
  598. /*if(loginC != '0' && loginC != '1' && loginC != '2'){
  599. FileReader fr = null;
  600. try {
  601. fr = new FileReader("pliki/dane/" + login + ".txt");
  602. } catch (FileNotFoundException e) {
  603. fr = new FileReader("pliki/dane/00000000.txt");
  604. }
  605.  
  606. BufferedReader br = new BufferedReader(fr);
  607. try {
  608. for(int i=0; i<4; i++){
  609. nrKonta = br.readLine();
  610. }
  611.  
  612. } catch (IOException e) {
  613. e.printStackTrace();
  614. }
  615.  
  616. try {
  617. br.close();
  618. fr.close();
  619. } catch (IOException e) {
  620. e.printStackTrace();
  621. }
  622.  
  623. if(nrKonta.equals("00000000000000")){
  624. potwierdzenie = "not";
  625. }else{
  626. potwierdzenie = "ok";
  627. }
  628. }
  629. else{
  630. potwierdzenie = "not";
  631. }
  632. dout.writeUTF(potwierdzenie);
  633.  
  634. if(potwierdzenie.equals("ok")){
  635. potwierdzenie = din.readUTF();
  636. }
  637.  
  638. if(potwierdzenie.equals("TAK")){
  639. akcja = din.readUTF();
  640. wartosc = din.readUTF();
  641. konto = din.readUTF();
  642.  
  643. if(konto.equals("1")){
  644. dodajWplate(nrKonta, wartosc, 1, akcja);
  645. }
  646. else if(konto.equals("2")){
  647. dodajWplate(nrKonta, wartosc, 2, akcja);
  648. }
  649. }*/
  650. if (loginC != '0' && loginC != '1' && loginC != '2') {
  651. if (nrKonta.equals("00000000000000")) {
  652. potwierdzenie = "not";
  653. } else {
  654. potwierdzenie = "ok";
  655. }
  656. } else potwierdzenie = "not";
  657. dout.writeUTF(potwierdzenie);
  658. if (potwierdzenie.equals("ok")) {
  659. potwierdzenie = din.readUTF();
  660. }
  661. if (potwierdzenie.equals("TAK")) {
  662. akcja = din.readUTF();
  663. wartosc = din.readUTF();
  664. konto = din.readUTF();
  665.  
  666. if (konto.equals("1")) {
  667. dodajWplate(nrKonta, wartosc, 1, akcja);
  668. } else if (konto.equals("2")) {
  669. dodajWplate(nrKonta, wartosc, 2, akcja);
  670. }
  671. }
  672. }
  673.  
  674. public static void dodajWplate(String nrKonta, String wartosc, int konto, String akcja) throws SQLException {
  675. int wplata = 0, saldo = 0;
  676.  
  677. wplata = Integer.parseInt(wartosc);
  678.  
  679. //sprawdz wartosc
  680. //FileReader fr = null;
  681. /*try {
  682. fr = new FileReader("pliki/konta/" + nrKonta + ".txt");
  683. } catch (FileNotFoundException e) {
  684. e.printStackTrace();
  685. }
  686.  
  687. BufferedReader br = new BufferedReader(fr);
  688. try {
  689. for(int i=0; i<=2; i++){
  690. kontoS[i] = br.readLine();
  691. }
  692. } catch (IOException e) {
  693. e.printStackTrace();
  694. }
  695.  
  696. try {
  697. br.close();
  698. fr.close();
  699. } catch (IOException e) {
  700. e.printStackTrace();
  701. }
  702. saldo = Integer.parseInt(kontoS[konto]);
  703. if(akcja.equals("wyplata")){
  704. saldo = saldo - wplata;
  705. }
  706. else{
  707. saldo = saldo + wplata;
  708. }
  709. kontoS[konto] = saldo + "";
  710.  
  711. //zmien wartosc
  712. FileWriter fwkonto = null;
  713. try {
  714. fwkonto = new FileWriter("pliki/konta/" + nrKonta + ".txt");
  715. }
  716. catch (IOException e) {
  717. e.printStackTrace();
  718. }
  719.  
  720. BufferedWriter bwkonto = new BufferedWriter(fwkonto);
  721. try {
  722. bwkonto.write(kontoS[0] + "\r\n" + kontoS[1] + "\r\n" + kontoS[2]);
  723. }
  724. catch (IOException e) {
  725. e.printStackTrace();
  726. }
  727.  
  728. try {
  729. bwkonto.close();
  730. fwkonto.close();
  731. }
  732. catch (IOException e) {
  733. e.printStackTrace();
  734. }*/
  735.  
  736. Statement stmt = null;
  737. ResultSet rs = null;
  738. String saldoRoz = "";
  739. int saldoR;
  740. stmt = c.createStatement();
  741. rs = stmt.executeQuery("SELECT SaldoRozliczeniowe FROM Konta WHERE NrKonta = '" + nrKonta + "'");
  742. while (rs.next()) {
  743. String name = rs.getString(1);
  744. saldoRoz = name;
  745. }
  746. saldoR = Integer.parseInt(saldoRoz);
  747. if (akcja.equals("wyplata"))
  748. stmt.executeUpdate("UPDATE Konta SET SaldoRozliczeniowe = '" + (saldoR - wplata) + "WHERE nrKonta = '" + nrKonta + "'");
  749. else
  750. stmt.executeUpdate("UPDATE Konta SET SaldoRozliczeniowe = '" + (saldoR + wplata) + "WHERE nrKonta = '" + nrKonta + "'");
  751. if (rs != null) {
  752. try {
  753. rs.close();
  754. } catch (SQLException sqlEx) {
  755. } // ignore
  756. rs = null;
  757. }
  758.  
  759. if (stmt != null) {
  760. try {
  761. stmt.close();
  762. } catch (SQLException sqlEx) {
  763. } // ignore
  764.  
  765. stmt = null;
  766. }
  767. }
  768.  
  769. public static void sprawdzSaldo(Socket s) throws IOException, SQLException {
  770. DataInputStream din = new DataInputStream(s.getInputStream());
  771. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  772.  
  773. String login = "", saldoR = "", saldoO = "";
  774.  
  775. login = din.readUTF();
  776.  
  777. /*FileReader fr = null;
  778. try {
  779. fr = new FileReader("pliki/dane/" + login + ".txt");
  780. } catch (FileNotFoundException e) {
  781. e.printStackTrace();
  782. }
  783.  
  784. BufferedReader br = new BufferedReader(fr);
  785. try {
  786. for(int i=0; i<4; i++){
  787. nrKonta = br.readLine();
  788. }
  789. } catch (IOException e) {
  790. e.printStackTrace();
  791. }
  792.  
  793. try {
  794. br.close();
  795. fr.close();
  796. } catch (IOException e) {
  797. e.printStackTrace();
  798. }
  799.  
  800.  
  801. FileReader fr2 = null;
  802. try {
  803. fr2 = new FileReader("pliki/konta/" + nrKonta + ".txt");
  804. } catch (FileNotFoundException e) {
  805. e.printStackTrace();
  806. }
  807.  
  808. BufferedReader br2 = new BufferedReader(fr2);
  809. try {
  810. for(int i=0; i<=2; i++){
  811. kontoS[i] = br2.readLine();
  812. }
  813. } catch (IOException e) {
  814. e.printStackTrace();
  815. }
  816.  
  817. try {
  818. br2.close();
  819. fr2.close();
  820. } catch (IOException e) {
  821. e.printStackTrace();
  822. }
  823. */
  824. Statement stmt = null;
  825. ResultSet rs = null;
  826. stmt = c.createStatement();
  827. rs = stmt.executeQuery("SELECT SaldoRozliczeniowe FROM Konta WHERE Login = '" + login + "'");
  828. while (rs.next()) {
  829. String name = rs.getString(1);
  830. saldoR = name;
  831. }
  832. rs = stmt.executeQuery("SELECT SaldoOszczednosciowe FROM Konta WHERE Login = '" + login + "'");
  833. while (rs.next()) {
  834. String name = rs.getString(1);
  835. saldoO = name;
  836. }
  837. if (rs != null) {
  838. try {
  839. rs.close();
  840. } catch (SQLException sqlEx) {
  841. } // ignore
  842. rs = null;
  843. }
  844.  
  845. if (stmt != null) {
  846. try {
  847. stmt.close();
  848. } catch (SQLException sqlEx) {
  849. } // ignore
  850.  
  851. stmt = null;
  852. }
  853. dout.writeUTF(saldoR);
  854. dout.writeUTF(saldoO);
  855. }
  856.  
  857. public static void edytujDane(Socket s) throws IOException, SQLException {
  858. DataInputStream din = new DataInputStream(s.getInputStream());
  859. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  860.  
  861. String login = "", potwierdzenie = "", imie = "", nazwisko = "", pesel = "";
  862. String[] dane = new String[4];
  863. char loginC;
  864.  
  865. login = din.readUTF();
  866. //loginC = login.charAt(0);
  867. potwierdzenie = "not";
  868. Statement stmt = null;
  869. ResultSet rs = null;
  870. stmt = c.createStatement();
  871. rs = stmt.executeQuery("SELECT Login FROM Konta");
  872. while (rs.next()) {
  873. String name = rs.getString(1);
  874. if (login.equals(name)) potwierdzenie = "ok";
  875. }
  876. dout.writeUTF(potwierdzenie);
  877. /*if(loginC != '0'){
  878. FileReader fr = null;
  879. try {
  880. fr = new FileReader("pliki/dane/" + login + ".txt");
  881. } catch (FileNotFoundException e) {
  882. fr = new FileReader("pliki/dane/00000000.txt");
  883. }
  884.  
  885. BufferedReader br = new BufferedReader(fr);
  886. try {
  887. for(int i=0; i<4; i++){
  888. dane[i] = br.readLine();
  889. }
  890.  
  891. } catch (IOException e) {
  892. e.printStackTrace();
  893. }
  894.  
  895. try {
  896. br.close();
  897. fr.close();
  898. } catch (IOException e) {
  899. e.printStackTrace();
  900. }
  901.  
  902. if(dane[0].equals("blad")){
  903. potwierdzenie = "not";
  904. }else{
  905. potwierdzenie = "ok";
  906. }
  907. }
  908. else{
  909. potwierdzenie = "not";
  910. }
  911.  
  912. dout.writeUTF(potwierdzenie);
  913. if(potwierdzenie.equals("ok") && (loginC == '1' || loginC == '2')){
  914. imie = din.readUTF();
  915. nazwisko = din.readUTF();
  916. pesel = din.readUTF();
  917.  
  918. FileWriter fwkonto = null;
  919. try {
  920. fwkonto = new FileWriter("pliki/dane/" + login + ".txt");
  921. }
  922. catch (IOException e) {
  923. e.printStackTrace();
  924. }
  925.  
  926. BufferedWriter bwkonto = new BufferedWriter(fwkonto);
  927. try {
  928. bwkonto.write(imie + "\r\n" + nazwisko + "\r\n" + pesel);
  929. }
  930. catch (IOException e) {
  931. e.printStackTrace();
  932. }
  933.  
  934. try {
  935. bwkonto.close();
  936. fwkonto.close();
  937. }
  938. catch (IOException e) {
  939. e.printStackTrace();
  940. }
  941. }
  942. else if(potwierdzenie.equals("ok") && (loginC != '1' || loginC != '2')){
  943. imie = din.readUTF();
  944. nazwisko = din.readUTF();
  945. pesel = din.readUTF();
  946.  
  947. FileWriter fwkonto = null;
  948. try {
  949. fwkonto = new FileWriter("pliki/dane/" + login + ".txt");
  950. }
  951. catch (IOException e) {
  952. e.printStackTrace();
  953. }
  954.  
  955. BufferedWriter bwkonto = new BufferedWriter(fwkonto);
  956. try {
  957. bwkonto.write(imie + "\r\n" + nazwisko + "\r\n" + pesel + "\r\n" + dane[3]);
  958. }
  959. catch (IOException e) {
  960. e.printStackTrace();
  961. }
  962.  
  963. try {
  964. bwkonto.close();
  965. fwkonto.close();
  966. }
  967. catch (IOException e) {
  968. e.printStackTrace();
  969. }
  970. }*/
  971. if (potwierdzenie.equals("ok")) {
  972. String id = "";
  973. stmt = c.createStatement();
  974. rs = stmt.executeQuery("SELECT ID FROM Konta WHERE Login = '" + login + "'");
  975. while (rs.next()) {
  976. String name = rs.getString(1);
  977. id = name;
  978. }
  979. nazwisko = din.readUTF();
  980. imie = din.readUTF();
  981. pesel = din.readUTF();
  982. stmt = c.createStatement();
  983. stmt.executeUpdate("UPDATE Dane SET nazwisko = '" + nazwisko + "', imie = '" + imie + "', pesel = '" + pesel + "' WHERE ID = '" + id + "'");
  984. if (rs != null) {
  985. try {
  986. rs.close();
  987. } catch (SQLException sqlEx) {
  988. } // ignore
  989. rs = null;
  990. }
  991.  
  992. if (stmt != null) {
  993. try {
  994. stmt.close();
  995. } catch (SQLException sqlEx) {
  996. } // ignore
  997.  
  998. stmt = null;
  999. }
  1000. }
  1001. }
  1002.  
  1003. public static void funkcje(Socket s, String login, String prawa) throws IOException, SQLException {
  1004. DataInputStream din = new DataInputStream(s.getInputStream());
  1005. DataOutputStream dout = new DataOutputStream(s.getOutputStream());
  1006.  
  1007. String akcja = "";
  1008.  
  1009. do {
  1010. akcja = prawa + din.readUTF();
  1011.  
  1012. switch (akcja) {
  1013. case "A1": //dodaj pracownika
  1014. utworzKonto(s);
  1015. break;
  1016.  
  1017. case "A2": //dodaj uzytkownika
  1018. case "P1": //dodaj uzytkownika
  1019. utworzKonto(s);
  1020. break;
  1021.  
  1022. case "P2": //edytuj dane
  1023. edytujDane(s);
  1024. break;
  1025.  
  1026. case "A3": //usun pracownika
  1027. usunKonto(s);
  1028. break;
  1029.  
  1030. case "A4": //usun uzytkownika
  1031. case "P3": //usun uzytkownika
  1032. usunKonto(s);
  1033. break;
  1034. case "P4": //wplata gotowki
  1035. wplataGotowki(s);
  1036. break;
  1037. case "P5": //wyplata gotowki
  1038. wplataGotowki(s);
  1039. break;
  1040.  
  1041. case "P6": //sprawdzenie salda
  1042. case "U3": //sprawdzenie salda
  1043. sprawdzSaldo(s);
  1044. break;
  1045.  
  1046. case "P8": //zmiana hasla
  1047. case "U2": //zmiana hasla
  1048. zmienHaslo(s);
  1049. break;
  1050.  
  1051. case "A0": //wyloguj
  1052. case "P0": //wyloguj
  1053. case "U0": //wyloguj
  1054. login = "logout";
  1055. break;
  1056. }
  1057. } while (!login.equals("logout"));
  1058. }
  1059.  
  1060. public static void main(String args[]) throws IOException, SQLException {
  1061. ServerSocket ss = null;
  1062. try {
  1063. ss = new ServerSocket(8050);
  1064. } catch (IOException e) {
  1065. System.out.println("Could not listen on port: 6666");
  1066. System.exit(-1);
  1067. }
  1068. Socket s = null;
  1069. try {
  1070. s = ss.accept();
  1071. } catch (IOException e) {
  1072. System.out.println("Accept failed: 6666");
  1073. System.exit(-1);
  1074. }
  1075. try {
  1076. Class.forName("oracle.jdbc.driver.OracleDriver");
  1077. } catch (ClassNotFoundException e) {
  1078. e.printStackTrace();
  1079. }
  1080. try {
  1081. c = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe", "hr", "hr");
  1082. } catch (SQLException e) {
  1083. e.printStackTrace();
  1084. }
  1085.  
  1086. //DataInputStream din = new DataInputStream(s.getInputStream());
  1087. BufferedReader din = new BufferedReader(
  1088. new InputStreamReader(
  1089. s.getInputStream()));
  1090.  
  1091. String start, login, prawa;
  1092.  
  1093. do {
  1094. System.out.println("Logowanie");
  1095. login = logowanie(s);
  1096. System.out.println("Sprawdzanie praw");
  1097. prawa = sprawdzPrawa(login);
  1098. System.out.println("Mozliwe dzialania");
  1099. funkcje(s, login, prawa);
  1100.  
  1101. start = din.readLine();
  1102. } while (!start.equals("NIE"));
  1103.  
  1104.  
  1105. s.close();
  1106. ss.close();
  1107. sc.close();
  1108.  
  1109. }
  1110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement