Advertisement
Guest User

Untitled

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