Вы находитесь на странице: 1из 15

CATEDRA INFORMATICĂ

Disciplina: Tehnologii Moderne de Programare

Lucrare de studiu individual nr. 2


Tema: Elaborarea unei aplicații cu utilizarea pachetului SWING

A elaborat elevul: Bencheci Valentin, I-1546

A evaluat profesorul: Musteață Victoria

Nota

____________
CUPRINS:
Listing-ul programului: .......................................................................................................................................................... 4

Test program : ....................................................................................................................................................................... 14

Bibliografie: ........................................................................................................................................................................... 15
LISTING-UL PROGRAMULUI:

PassEngine.java

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

class PassEngine
{

public static void main(String args[])


{
HomeView home = new HomeView();
AddView add = new AddView();
ShowView show = new ShowView();
PassModel model = new PassModel();
PassController controller = new PassController(home, add, model, show);
}

HomeView.java

import java.awt.GridLayout;

import java.awt.event.ActionListener;

import javax.swing.*;

public class HomeView extends JFrame {

JButton bAdauga, bAfiseaza;

public void adaugaAscultator(ActionListener listener)

bAdauga.addActionListener(listener);

bAfiseaza.addActionListener(listener);

public HomeView()

setLayout(new GridLayout(2,1));
bAdauga = new JButton("Adauga date noi");

bAfiseaza = new JButton("Afiseaza date");

add(bAdauga);

add(bAfiseaza);

setSize(250,160);

setVisible(true);

setTitle("SmartPass");

AddView.java

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import javax.swing.*;

public class AddView extends JFrame {


JTextField tfServiciu, tfParola;
JButton bGenereaza, bSalveaza, bHome;

public void adaugaAscultator(ActionListener listener)


{
bGenereaza.addActionListener(listener);
bSalveaza.addActionListener(listener);
bHome.addActionListener(listener);
}

public AddView()
{
setLayout(new GridLayout(5, 1));

tfServiciu = new JTextField();


tfParola = new JTextField();
bGenereaza = new JButton("Genereaza parola*");
bSalveaza = new JButton("Salveaza parola");
bHome = new JButton("Home");

add(tfServiciu);
add(tfParola);
add(bGenereaza);
add(bSalveaza);
add(bHome);
setSize(290,300);
setVisible(false);
setTitle("Adauga date");
}
}

PassModel.java

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.awt.Toolkit;

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.StringSelection;

import java.awt.datatransfer.Transferable;

import java.security.SecureRandom;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.*;

import java.util.stream.IntStream;

import java.util.stream.Stream;

import com.mysql.cj.xdevapi.Statement;

public class PassModel {

ShowView view;

public static void writeTextToClipboard(String s) {

Clipboard clipboard =
Toolkit.getDefaultToolkit().getSystemClipboard();

Transferable transferable = new StringSelection(s);


clipboard.setContents(transferable, null);

public void select(String p)

try {

String tampon;

Class.forName("com.mysql.jdbc.Driver") ;

Base64.Encoder encoder = Base64.getMimeEncoder();

tampon = encoder.encodeToString(p.getBytes());

Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/smartpass","root", "") ;

java.sql.Statement stmt = conn.createStatement() ;

String query = "SELECT password FROM data WHERE service='" +


tampon + "' LIMIT 1;";

ResultSet rs = stmt.executeQuery(query);

while ( rs.next() ) {

String password = rs.getString("password");

Base64.Decoder decoder = Base64.getMimeDecoder();

password = new String(decoder.decode(password));

view.lParola.setText(password);

conn.close();

} catch (Exception e) {

System.err.println("Got an exception! ");

System.err.println(e.getMessage());

}
public void selectData()

try {

Class.forName("com.mysql.jdbc.Driver") ;

Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost/smartpass","root", "") ;

java.sql.Statement stmt = conn.createStatement() ;

String query = "SELECT service FROM data;" ;

ResultSet rs = stmt.executeQuery(query);

while ( rs.next() ) {

String service = rs.getString("service");

Base64.Decoder decoder = Base64.getMimeDecoder();

service = new String(decoder.decode(service));

view.cbList.addItem(service);

conn.close();

} catch (Exception e) {

System.err.println("Got an exception! ");

System.err.println(e.getMessage());

public void insertData(String service, String password)

try

Base64.Encoder encoder = Base64.getMimeEncoder();

password = encoder.encodeToString(password.getBytes());

service = encoder.encodeToString(service.getBytes());
Class.forName("com.mysql.jdbc.Driver");

Connection conn = null;

conn =
DriverManager.getConnection("jdbc:mysql://localhost/smartpass","root", "");

PreparedStatement preparedStmt = conn.prepareStatement("INSERT INTO


`SmartPass`.`data` (service, password) VALUES ('" + service + "', '" + password
+ "')");

preparedStmt.execute();

System.out.print("Database is connected !");

conn.close();

catch(Exception e)

System.out.print("Do not connect to DB - Error:"+e);

PassController.java

import java.awt.event.*;

import java.util.Random;

public class PassController {

HomeView home;

AddView add;

PassModel model;
ShowView show;

public PassController (HomeView home, AddView add, PassModel model,


ShowView show)

this.home = home;

this.add = add;

this.model = model;

this.show = show;

this.model.view = show;

this.home.adaugaAscultator(new PassAscultator());

this.add.adaugaAscultator(new PassAscultator());

this.show.adaugaAscultator(new PassAscultator());

this.show.ascul(new ascult());

class ascult implements ItemListener

@Override

public void itemStateChanged(ItemEvent e) {

String tampon;

tampon = show.cbList.getSelectedItem().toString();

model.select(tampon);

class PassAscultator implements ActionListener


{

@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource() == home.bAdauga)

add.setVisible(true);

home.setVisible(false);

if (e.getSource() == home.bAfiseaza)

show.setVisible(true);

home.setVisible(false);

model.selectData();

if (e.getSource() == add.bSalveaza)

String service, password;

service = add.tfServiciu.getText().toString();

password = add.tfParola.getText().toString();

model.insertData(service, password);

if (e.getSource() == add.bHome)

{
add.tfParola.setText("");

add.tfServiciu.setText("");

add.setVisible(false);

home.setVisible(true);

if (e.getSource() == show.bCopy)

String tampon = show.lParola.getText().toString();

model.writeTextToClipboard(tampon);

if (e.getSource() == add.bGenereaza)

String pass;

pass = new Random().ints(10, 33,


122).collect(StringBuilder::new,

StringBuilder::appendCodePoint,
StringBuilder::append)

.toString();

add.tfParola.setText(pass);

if (e.getSource() == show.bHome)

show.setVisible(false);

home.setVisible(true);

}
}

ShowView.java

import java.awt.GridLayout;

import java.awt.event.ActionListener;

import java.awt.event.ItemListener;

import javax.swing.*;

public class ShowView extends JFrame {

JComboBox cbList;

JButton bCopy, bHome;

JLabel lParola;

public void ascul(ItemListener listener)

cbList.addItemListener(listener);

public void adaugaAscultator(ActionListener listener)

bCopy.addActionListener(listener);

bHome.addActionListener(listener);

public ShowView()
{

setLayout(new GridLayout(4,1));

cbList = new JComboBox();

bCopy = new JButton("Salveaza in Clipboard");

lParola= new JLabel("");

bHome = new JButton("Home");

add(cbList);

add(lParola);

add(bCopy);

add(bHome);

setSize(280,320);

setVisible(false);

setTitle("Afiseaza date");

TEST PROGRAM :

1. Adaugarea datelor
2. Afisarea datelor

BIBLIOGRAFIE:

1. https://www.javatpoint.com/
2. https://www.javatpoint.com/java-do-while-loop
3. https://www.javatpoint.com/java-for-loop
4. https://www.javatpoint.com/array-in-java
5. https://www.javatpoint.com/java-math