Advertisement
Guest User

Untitled

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