diff --git a/.gitignore b/.gitignore index a31ec6e..989a405 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ # JeatBrains stuff .idea JDBC.iml + +# Credentials +credentials.txt diff --git a/ojdbc8.jar b/ojdbc8.jar new file mode 100644 index 0000000..bf41243 Binary files /dev/null and b/ojdbc8.jar differ diff --git a/src/DB/Connexion.java b/src/DB/Connexion.java new file mode 100644 index 0000000..d1b8544 --- /dev/null +++ b/src/DB/Connexion.java @@ -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; + } +} diff --git a/src/DB/Request.java b/src/DB/Request.java new file mode 100644 index 0000000..1e9cae6 --- /dev/null +++ b/src/DB/Request.java @@ -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; + } + } +} diff --git a/src/GUI/LoginGUI.form b/src/GUI/LoginGUI.form new file mode 100644 index 0000000..01d441d --- /dev/null +++ b/src/GUI/LoginGUI.form @@ -0,0 +1,100 @@ + +
diff --git a/src/GUI/LoginGUI.java b/src/GUI/LoginGUI.java new file mode 100644 index 0000000..e1eee97 --- /dev/null +++ b/src/GUI/LoginGUI.java @@ -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); + } +} diff --git a/src/Objects/Owner.java b/src/Objects/Owner.java new file mode 100644 index 0000000..24b0857 --- /dev/null +++ b/src/Objects/Owner.java @@ -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; + } +}