1
0
Fork 0

Add login GUI

This commit is contained in:
Ethanell 2020-06-05 14:13:54 +02:00
parent c6ca370a77
commit e8067d46a8
7 changed files with 299 additions and 0 deletions

3
.gitignore vendored
View file

@ -1,3 +1,6 @@
# JeatBrains stuff
.idea
JDBC.iml
# Credentials
credentials.txt

BIN
ojdbc8.jar Normal file

Binary file not shown.

51
src/DB/Connexion.java Normal file
View file

@ -0,0 +1,51 @@
package DB;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connexion {
private static String[] getCredentials() {
File f = new File("credentials.txt");
try {
if (f.createNewFile() || f.length() == 0) {
JOptionPane.showMessageDialog(null, "No credentials !");
System.exit(1);
return null;
} else {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String url = br.readLine();
String username = br.readLine();
String password = br.readLine();
return new String[]{url, username, password};
}
} catch (IOException e) {
return null;
}
}
public static Connection getConnexion() {
String[] credentials = getCredentials();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
if (credentials == null)
throw new NullPointerException();
return DriverManager.getConnection(credentials[0], credentials[1], credentials[2]);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Can't connect to the database !");
System.exit(1);
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "Database driver not found !");
System.exit(1);
}
return null;
}
}

31
src/DB/Request.java Normal file
View file

@ -0,0 +1,31 @@
package DB;
import Objects.Owner;
import javax.swing.*;
import java.sql.*;
public class Request {
public static Owner getOwner(String lastName, String zip) {
Connection c = Connexion.getConnexion();
try {
PreparedStatement ps = c.prepareStatement("SELECT IDPROPRIO, PRENOMPROPRIO, NOMPROPRIO, ADPROPRIO, CPPROPRIO, VILLEPROPRIO FROM PROPRIETAIRE WHERE NOMPROPRIO = ? AND CPPROPRIO = ?");
ps.setString(1, lastName);
ps.setString(2, zip);
if (ps.execute()) {
ResultSet rs = ps.getResultSet();
if (rs.next())
return new Owner(rs.getInt("IDPROPRIO"),
rs.getString("PRENOMPROPRIO"),
rs.getString("NOMPROPRIO"),
rs.getString("ADPROPRIO"),
rs.getString("CPPROPRIO"),
rs.getString("VILLEPROPRIO"));
}
return null;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error when query owner !");
return null;
}
}
}

100
src/GUI/LoginGUI.form Normal file
View file

@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="GUI.LoginGUI">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="OK"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="76e6c" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="65f87"/>
<text value="Last name"/>
</properties>
</component>
<component id="65f87" class="javax.swing.JTextField" binding="lastNameField">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
<component id="9201f" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="45664"/>
<text value="ZIP"/>
</properties>
</component>
<component id="45664" class="javax.swing.JTextField" binding="zipField">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
</children>
</grid>
</form>

70
src/GUI/LoginGUI.java Normal file
View file

@ -0,0 +1,70 @@
package GUI;
import DB.Request;
import Objects.Owner;
import javax.swing.*;
import java.awt.event.*;
public class LoginGUI extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JTextField lastNameField;
private JTextField zipField;
public LoginGUI() {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});
buttonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
});
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
private void onOK() {
Owner owner = Request.getOwner(lastNameField.getText(), zipField.getText());
if (owner != null) {
System.out.println(owner);
dispose();
}
else
JOptionPane.showMessageDialog(null, "Invalid login !");
}
private void onCancel() {
// add your code here if necessary
dispose();
}
public static void main(String[] args) {
LoginGUI dialog = new LoginGUI();
dialog.pack();
dialog.setVisible(true);
System.exit(0);
}
}

44
src/Objects/Owner.java Normal file
View file

@ -0,0 +1,44 @@
package Objects;
public class Owner {
private int id;
private String firstName;
private String lastName;
private String address;
private String zip;
private String city;
public Owner(int id, String firstName, String lastName, String address, String zip, String city) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.zip = zip;
this.city = city;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getAddress() {
return address;
}
public String getZip() {
return zip;
}
public String getCity() {
return city;
}
}