Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.sql.*;
- import java.util.Scanner;
- public class Serwer2 {
- static Connection c = null;
- static Scanner sc = new Scanner(System.in);
- public static String logowanie(Socket s) throws IOException, SQLException {
- System.out.println("checkpoint logowanie 1");
- BufferedReader din = new BufferedReader(
- new InputStreamReader(
- s.getInputStream()));
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- System.out.println("checkpoint logowanie 2");
- String login = "", haslo = "", wynik = "";
- char prawaC;
- System.out.println("checkpoint logowanie 3");
- do {
- System.out.println("checkpoint logowanie 4");
- login = din.readLine();
- System.out.println("login: "+login);
- System.out.println("checkpoint logowanie 5");
- haslo = din.readLine();
- System.out.println("haslo: "+haslo);
- System.out.println("checkpoint logowanie 6");
- wynik = szukaj(login, haslo);
- System.out.println(wynik);
- dout.writeUTF(wynik);
- } while (!wynik.equals("ok"));
- System.out.println("checkpoint logowanie 7");
- prawaC = login.charAt(0);
- if (prawaC == '0') {
- dout.writeUTF("Admin");
- } else if (prawaC == '1' || prawaC == '2') {
- dout.writeUTF("Pracownik");
- } else {
- dout.writeUTF("Uzytkownik");
- }
- return login;
- }
- public static String sprawdzPrawa(String login) throws SQLException {
- String prawa = "";
- char prawaC;
- prawaC = login.charAt(0);
- if (prawaC == '0') {
- prawa = "Admin";
- } else if (prawaC == '1' || prawaC == '2') {
- prawa = "Pracownik";
- } else {
- prawa = "Uzytkownik";
- }
- return prawa;
- }
- public static String szukaj(String login, String haslo) throws IOException, SQLException {
- /*String wynik = "not", hasloTest = "";
- FileReader fr = null;
- try {
- fr = new FileReader("pliki/hasla/" + login + ".txt");
- } catch (FileNotFoundException e) {
- fr = new FileReader("pliki/hasla/00000000.txt");
- }
- BufferedReader br = new BufferedReader(fr);
- try {
- hasloTest = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- br.close();
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(haslo.equals(hasloTest)){
- wynik = "ok";
- }else{
- wynik = "not";
- }
- return wynik;*/
- String wynik = "notl";
- int i = 0;
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT Login FROM Konta");
- while (rs.next()) {
- String name = rs.getString(1);
- if (name.equals(login)) wynik = "ok";
- i++;
- }
- //if (wynik.equals("notl")) return wynik;
- rs = stmt.executeQuery("SELECT Haslo FROM Hasla WHERE ID = '" + i + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- if (name.equals(haslo)) wynik = "ok";
- }
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- return wynik;
- }
- public static void utworzKonto(Socket s) throws IOException, SQLException {
- DataInputStream din = new DataInputStream(s.getInputStream());
- String login = "", imie = "", nazwisko = "", pesel = "", nrKonta = "", haslo = "";
- char loginC;
- imie = din.readUTF();
- nazwisko = din.readUTF();
- pesel = din.readUTF();
- haslo = din.readUTF();
- login = din.readUTF();
- nrKonta = din.readUTF();
- loginC = login.charAt(0);
- if (loginC == '1' || loginC == '2') {
- dodajPracownika(nazwisko, imie, pesel, haslo, login);
- } else {
- dodajUzytkownika(nazwisko, imie, pesel, haslo, login, nrKonta);
- }
- }
- public static void dodajPracownika(String nazwisko, String imie, String pesel, String haslo, String login) throws SQLException {
- /*FileWriter fwdane = null;
- FileWriter fwhasla = null;
- try {
- fwdane = new FileWriter("pliki/dane/" + login + ".txt");
- fwhasla = new FileWriter("pliki/hasla/" + login + ".txt");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- BufferedWriter bwdane = new BufferedWriter(fwdane);
- BufferedWriter bwhasla = new BufferedWriter(fwhasla);
- try {
- bwdane.write(imie + "\r\n" + nazwisko + "\r\n" + pesel);
- bwhasla.write("password");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- try {
- bwdane.close();
- fwdane.close();
- bwhasla.close();
- fwhasla.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }*/
- int i = 1;
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT ID FROM Dane");
- while (rs.next()) {
- String name = rs.getString(1);
- if (!(name.equals(i))) break;
- i++;
- }
- System.out.println(""+imie+" "+nazwisko+" "+pesel+" "+haslo+" "+login+" "+i);
- stmt.executeUpdate("INSERT INTO Dane " + "VALUES ('" + i + "','" + nazwisko + "','" + imie + "','" + pesel + "')");
- stmt.executeUpdate("INSERT INTO Hasla " + "VALUES ('" + i + "','" + haslo + "')");
- //Random generator = new Random();
- /*do {
- losowa = generator.nextInt(100000000);
- } while (losowa < 10000000);*/
- /*do {
- losowa = generator.nextInt(300000);
- } while (losowa < 100000);
- los = losowa+"";*/
- stmt.executeUpdate("INSERT INTO Konta " + "VALUES ('nd','" + i + "','" + login + "','" + "'nd','nd')");
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- }
- public static void dodajUzytkownika(String nazwisko, String imie, String pesel, String haslo, String login, String nrKonta) throws SQLException {
- /*FileWriter fwdane = null;
- FileWriter fwhasla = null;
- FileWriter fwkonto = null;
- try {
- fwdane = new FileWriter("pliki/dane/" + login + ".txt");
- fwhasla = new FileWriter("pliki/hasla/" + login + ".txt");
- fwkonto = new FileWriter("pliki/konta/" + nrKonta + ".txt");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- BufferedWriter bwdane = new BufferedWriter(fwdane);
- BufferedWriter bwhasla = new BufferedWriter(fwhasla);
- BufferedWriter bwkonto = new BufferedWriter(fwkonto);
- try {
- bwdane.write(imie + "\r\n" + nazwisko + "\r\n" + pesel + "\r\n" + nrKonta);
- bwhasla.write("password");
- bwkonto.write(login + "\r\n" + "0" + "\r\n" + "0");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- try {
- bwdane.close();
- fwdane.close();
- bwhasla.close();
- fwhasla.close();
- bwkonto.close();
- fwkonto.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }*/
- int i = 1;
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT ID FROM Dane");
- while (rs.next()) {
- String name = rs.getString(1);
- if (!(name.equals(i))) break;
- i++;
- }
- System.out.println("checkpoint logowanie 4");
- stmt.executeUpdate("INSERT INTO Dane " + "VALUES ('" + i + "','" + nazwisko + "','" + imie + "','" + pesel + "')");
- stmt.executeUpdate("INSERT INTO Hasla " + "VALUES ('" + i + "','" + haslo + "')");
- /*Random generator = new Random();
- do {
- losowa = generator.nextInt(100000000);
- } while (losowa < 10000000);
- do {
- losowa2 = generator.nextInt(300000);
- } while (losowa2 < 100000);
- los = losowa+"";
- los2 = losowa2+"";*/
- stmt.executeUpdate("INSERT INTO Konta " + "VALUES ('" + nrKonta + "','" + i + "','" + login + "','" + "'0','0')");
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- }
- public static void usunKonto(Socket s) throws IOException, SQLException {
- DataInputStream din = new DataInputStream(s.getInputStream());
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- String login = "", typ = "";
- typ = din.readUTF();
- login = din.readUTF();
- /*
- if(typ.equals("prac")){
- try{
- File fdhasla = new File("pliki/hasla/" + login + ".txt");
- File fddane = new File("pliki/dane/" + login + ".txt");
- if(fdhasla.delete() && fddane.delete()){
- wynik = "ok";
- }
- else{
- wynik = "not";
- }
- }catch(Exception e){
- wynik = "err";
- }
- }
- else if(typ.equals("uzyt")){
- nrKonta = sprawdzKonto(login);
- try{
- File fdhasla = new File("pliki/hasla/" + login + ".txt");
- File fddane = new File("pliki/dane/" + login + ".txt");
- File fdkonto = new File("pliki/konta/" + nrKonta + ".txt");
- if(fdhasla.delete() && fddane.delete() && fdkonto.delete()){
- wynik = "ok";
- }
- else{
- wynik = "not";
- }
- } catch(Exception e){
- wynik = "err";
- }
- }
- else if(typ.equals("not")){
- try{
- wynik = "ok";
- }
- catch(Exception e){
- wynik = "err";
- }
- }
- dout.writeUTF(wynik);*/
- String i = "", wynik = "ok";
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT ID FROM Konta WHERE Login = '" + login + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- i = name;
- }
- stmt.executeUpdate("DELETE FROM Konta WHERE Login = '" + login + "'");
- stmt.executeUpdate("DELETE FROM Dane WHERE ID = '" + i + "'");
- stmt.executeUpdate("DELETE FROM Hasla WHERE ID = '" + i + "'");
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- dout.writeUTF(wynik);
- }
- public static String sprawdzKonto(String login) throws IOException, SQLException {
- String nrKonta = "";
- /*FileReader fr = null;
- try {
- fr = new FileReader("pliki/dane/" + login + ".txt");
- } catch (FileNotFoundException e) {
- nrKonta = "0";
- }
- BufferedReader br = new BufferedReader(fr);
- try {
- for(int i=0; i<4; i++){
- nrKonta = br.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- br.close();
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }*/
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT NrKonta FROM Konta WHERE Login = '" + login + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- nrKonta = name;
- }
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- return nrKonta;
- }
- public static void zmienHaslo(Socket s) throws IOException, SQLException {
- DataInputStream din = new DataInputStream(s.getInputStream());
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- String login = "", hasloS = "", hasloN = "", wyjscie = "";
- login = din.readUTF();
- hasloS = din.readUTF();
- wyjscie = sprawdzHaslo(login, hasloS);
- dout.writeUTF(wyjscie);
- if (wyjscie.equals("ok")) {
- hasloN = din.readUTF();
- wyjscie = dokonajZmianyHasla(login, hasloN);
- dout.writeUTF(wyjscie);
- }
- }
- public static String sprawdzHaslo(String login, String hasloS) throws SQLException {
- /*String potwierdzenie = "", hasloPlik = "";
- FileReader fr = null;
- try {
- fr = new FileReader("pliki/hasla/" + login + ".txt");
- } catch (FileNotFoundException e) {
- potwierdzenie = "err";
- }
- BufferedReader br = new BufferedReader(fr);
- try {
- hasloPlik = br.readLine();
- } catch (IOException e) {
- potwierdzenie = "err";
- }
- try {
- br.close();
- fr.close();
- } catch (IOException e) {
- potwierdzenie = "err";
- }
- if(hasloPlik.equals(hasloS)){
- potwierdzenie = "ok";
- }
- else{
- potwierdzenie = "not";
- }
- return potwierdzenie;*/
- String i = "", potwierdzenie = "", hasloT = "";
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT ID FROM Konta WHERE Login = '" + login + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- i = name;
- }
- rs = stmt.executeQuery("SELECT Haslo FROM Hasla WHERE ID = '" + i + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- hasloT = name;
- }
- if (hasloT.equals(hasloS)) potwierdzenie = "ok";
- else potwierdzenie = "not";
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- return potwierdzenie;
- }
- public static String dokonajZmianyHasla(String login, String hasloN) throws SQLException {
- /*String potwierdzenie = "ok";
- FileWriter fwhaslo = null;
- try {
- fwhaslo = new FileWriter("pliki/hasla/" + login + ".txt");
- }
- catch (IOException e) {
- potwierdzenie = "err";
- }
- BufferedWriter bwhaslo = new BufferedWriter(fwhaslo);
- try {
- bwhaslo.write(hasloN);
- }
- catch (IOException e) {
- potwierdzenie = "err";
- }
- try {
- bwhaslo.close();
- fwhaslo.close();
- }
- catch (IOException e) {
- potwierdzenie = "err";
- }
- return potwierdzenie;*/
- String i = "", potwierdzenie = "";
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT ID FROM Konta WHERE Login = '" + login + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- i = name;
- }
- stmt.executeUpdate("UPDATE Hasla SET Haslo = '" + hasloN + "' WHERE ID = '" + i + "'");
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- return potwierdzenie;
- }
- public static void wplataGotowki(Socket s) throws IOException, SQLException {
- DataInputStream din = new DataInputStream(s.getInputStream());
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- String login = "", potwierdzenie = "", nrKonta = "", wartosc = "", konto = "", akcja = "";
- char loginC;
- login = din.readUTF();
- loginC = login.charAt(0);
- /*if(loginC != '0' && loginC != '1' && loginC != '2'){
- FileReader fr = null;
- try {
- fr = new FileReader("pliki/dane/" + login + ".txt");
- } catch (FileNotFoundException e) {
- fr = new FileReader("pliki/dane/00000000.txt");
- }
- BufferedReader br = new BufferedReader(fr);
- try {
- for(int i=0; i<4; i++){
- nrKonta = br.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- br.close();
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(nrKonta.equals("00000000000000")){
- potwierdzenie = "not";
- }else{
- potwierdzenie = "ok";
- }
- }
- else{
- potwierdzenie = "not";
- }
- dout.writeUTF(potwierdzenie);
- if(potwierdzenie.equals("ok")){
- potwierdzenie = din.readUTF();
- }
- if(potwierdzenie.equals("TAK")){
- akcja = din.readUTF();
- wartosc = din.readUTF();
- konto = din.readUTF();
- if(konto.equals("1")){
- dodajWplate(nrKonta, wartosc, 1, akcja);
- }
- else if(konto.equals("2")){
- dodajWplate(nrKonta, wartosc, 2, akcja);
- }
- }*/
- if (loginC != '0' && loginC != '1' && loginC != '2') {
- if (nrKonta.equals("00000000000000")) {
- potwierdzenie = "not";
- } else {
- potwierdzenie = "ok";
- }
- } else potwierdzenie = "not";
- dout.writeUTF(potwierdzenie);
- if (potwierdzenie.equals("ok")) {
- potwierdzenie = din.readUTF();
- }
- if (potwierdzenie.equals("TAK")) {
- akcja = din.readUTF();
- wartosc = din.readUTF();
- konto = din.readUTF();
- if (konto.equals("1")) {
- dodajWplate(nrKonta, wartosc, 1, akcja);
- } else if (konto.equals("2")) {
- dodajWplate(nrKonta, wartosc, 2, akcja);
- }
- }
- }
- public static void dodajWplate(String nrKonta, String wartosc, int konto, String akcja) throws SQLException {
- int wplata = 0, saldo = 0;
- wplata = Integer.parseInt(wartosc);
- //sprawdz wartosc
- //FileReader fr = null;
- /*try {
- fr = new FileReader("pliki/konta/" + nrKonta + ".txt");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- BufferedReader br = new BufferedReader(fr);
- try {
- for(int i=0; i<=2; i++){
- kontoS[i] = br.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- br.close();
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- saldo = Integer.parseInt(kontoS[konto]);
- if(akcja.equals("wyplata")){
- saldo = saldo - wplata;
- }
- else{
- saldo = saldo + wplata;
- }
- kontoS[konto] = saldo + "";
- //zmien wartosc
- FileWriter fwkonto = null;
- try {
- fwkonto = new FileWriter("pliki/konta/" + nrKonta + ".txt");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- BufferedWriter bwkonto = new BufferedWriter(fwkonto);
- try {
- bwkonto.write(kontoS[0] + "\r\n" + kontoS[1] + "\r\n" + kontoS[2]);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- try {
- bwkonto.close();
- fwkonto.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }*/
- Statement stmt = null;
- ResultSet rs = null;
- String saldoRoz = "";
- int saldoR;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT SaldoRozliczeniowe FROM Konta WHERE NrKonta = '" + nrKonta + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- saldoRoz = name;
- }
- saldoR = Integer.parseInt(saldoRoz);
- if (akcja.equals("wyplata"))
- stmt.executeUpdate("UPDATE Konta SET SaldoRozliczeniowe = '" + (saldoR - wplata) + "WHERE nrKonta = '" + nrKonta + "'");
- else
- stmt.executeUpdate("UPDATE Konta SET SaldoRozliczeniowe = '" + (saldoR + wplata) + "WHERE nrKonta = '" + nrKonta + "'");
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- }
- public static void sprawdzSaldo(Socket s) throws IOException, SQLException {
- DataInputStream din = new DataInputStream(s.getInputStream());
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- String login = "", saldoR = "", saldoO = "";
- login = din.readUTF();
- /*FileReader fr = null;
- try {
- fr = new FileReader("pliki/dane/" + login + ".txt");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- BufferedReader br = new BufferedReader(fr);
- try {
- for(int i=0; i<4; i++){
- nrKonta = br.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- br.close();
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- FileReader fr2 = null;
- try {
- fr2 = new FileReader("pliki/konta/" + nrKonta + ".txt");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- BufferedReader br2 = new BufferedReader(fr2);
- try {
- for(int i=0; i<=2; i++){
- kontoS[i] = br2.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- br2.close();
- fr2.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- */
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT SaldoRozliczeniowe FROM Konta WHERE Login = '" + login + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- saldoR = name;
- }
- rs = stmt.executeQuery("SELECT SaldoOszczednosciowe FROM Konta WHERE Login = '" + login + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- saldoO = name;
- }
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- dout.writeUTF(saldoR);
- dout.writeUTF(saldoO);
- }
- public static void edytujDane(Socket s) throws IOException, SQLException {
- DataInputStream din = new DataInputStream(s.getInputStream());
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- String login = "", potwierdzenie = "", imie = "", nazwisko = "", pesel = "";
- String[] dane = new String[4];
- char loginC;
- login = din.readUTF();
- //loginC = login.charAt(0);
- potwierdzenie = "not";
- Statement stmt = null;
- ResultSet rs = null;
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT Login FROM Konta");
- while (rs.next()) {
- String name = rs.getString(1);
- if (login.equals(name)) potwierdzenie = "ok";
- }
- dout.writeUTF(potwierdzenie);
- /*if(loginC != '0'){
- FileReader fr = null;
- try {
- fr = new FileReader("pliki/dane/" + login + ".txt");
- } catch (FileNotFoundException e) {
- fr = new FileReader("pliki/dane/00000000.txt");
- }
- BufferedReader br = new BufferedReader(fr);
- try {
- for(int i=0; i<4; i++){
- dane[i] = br.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- br.close();
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(dane[0].equals("blad")){
- potwierdzenie = "not";
- }else{
- potwierdzenie = "ok";
- }
- }
- else{
- potwierdzenie = "not";
- }
- dout.writeUTF(potwierdzenie);
- if(potwierdzenie.equals("ok") && (loginC == '1' || loginC == '2')){
- imie = din.readUTF();
- nazwisko = din.readUTF();
- pesel = din.readUTF();
- FileWriter fwkonto = null;
- try {
- fwkonto = new FileWriter("pliki/dane/" + login + ".txt");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- BufferedWriter bwkonto = new BufferedWriter(fwkonto);
- try {
- bwkonto.write(imie + "\r\n" + nazwisko + "\r\n" + pesel);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- try {
- bwkonto.close();
- fwkonto.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- else if(potwierdzenie.equals("ok") && (loginC != '1' || loginC != '2')){
- imie = din.readUTF();
- nazwisko = din.readUTF();
- pesel = din.readUTF();
- FileWriter fwkonto = null;
- try {
- fwkonto = new FileWriter("pliki/dane/" + login + ".txt");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- BufferedWriter bwkonto = new BufferedWriter(fwkonto);
- try {
- bwkonto.write(imie + "\r\n" + nazwisko + "\r\n" + pesel + "\r\n" + dane[3]);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- try {
- bwkonto.close();
- fwkonto.close();
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }*/
- if (potwierdzenie.equals("ok")) {
- String id = "";
- stmt = c.createStatement();
- rs = stmt.executeQuery("SELECT ID FROM Konta WHERE Login = '" + login + "'");
- while (rs.next()) {
- String name = rs.getString(1);
- id = name;
- }
- nazwisko = din.readUTF();
- imie = din.readUTF();
- pesel = din.readUTF();
- stmt = c.createStatement();
- stmt.executeUpdate("UPDATE Dane SET nazwisko = '" + nazwisko + "', imie = '" + imie + "', pesel = '" + pesel + "' WHERE ID = '" + id + "'");
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException sqlEx) {
- } // ignore
- rs = null;
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException sqlEx) {
- } // ignore
- stmt = null;
- }
- }
- }
- public static void funkcje(Socket s, String login, String prawa) throws IOException, SQLException {
- DataInputStream din = new DataInputStream(s.getInputStream());
- DataOutputStream dout = new DataOutputStream(s.getOutputStream());
- String akcja = "";
- do {
- akcja = prawa + din.readUTF();
- switch (akcja) {
- case "A1": //dodaj pracownika
- utworzKonto(s);
- break;
- case "A2": //dodaj uzytkownika
- case "P1": //dodaj uzytkownika
- utworzKonto(s);
- break;
- case "P2": //edytuj dane
- edytujDane(s);
- break;
- case "A3": //usun pracownika
- usunKonto(s);
- break;
- case "A4": //usun uzytkownika
- case "P3": //usun uzytkownika
- usunKonto(s);
- break;
- case "P4": //wplata gotowki
- wplataGotowki(s);
- break;
- case "P5": //wyplata gotowki
- wplataGotowki(s);
- break;
- case "P6": //sprawdzenie salda
- case "U3": //sprawdzenie salda
- sprawdzSaldo(s);
- break;
- case "P8": //zmiana hasla
- case "U2": //zmiana hasla
- zmienHaslo(s);
- break;
- case "A0": //wyloguj
- case "P0": //wyloguj
- case "U0": //wyloguj
- login = "logout";
- break;
- }
- } while (!login.equals("logout"));
- }
- public static void main(String args[]) throws IOException, SQLException {
- ServerSocket ss = null;
- try {
- ss = new ServerSocket(8050);
- } catch (IOException e) {
- System.out.println("Could not listen on port: 6666");
- System.exit(-1);
- }
- Socket s = null;
- try {
- s = ss.accept();
- } catch (IOException e) {
- System.out.println("Accept failed: 6666");
- System.exit(-1);
- }
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- try {
- c = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe", "hr", "hr2");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- //DataInputStream din = new DataInputStream(s.getInputStream());
- BufferedReader din = new BufferedReader(
- new InputStreamReader(
- s.getInputStream()));
- String start, login, prawa;
- do {
- System.out.println("Logowanie");
- login = logowanie(s);
- System.out.println("Sprawdzanie praw");
- prawa = sprawdzPrawa(login);
- System.out.println("Mozliwe dzialania");
- funkcje(s, login, prawa);
- start = din.readLine();
- } while (!start.equals("NIE"));
- s.close();
- ss.close();
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement