codice:
package ImportOrUploadCSV_file;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Window;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JSeparator;
import java.awt.SystemColor;
public class registrazione extends JFrame {
private JPanel contentPane;
private JTextField txtNome;
private JTextField txtCognome;
private JTextField txtUser;
private JTextField txtPsw;
private JTextField txtPsw2;
private JTextField txtMail;
private JTextField txtEta;
Connection connection = null;
boolean consenso=false; //se false non deve registrare User
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
registrazione frame = new registrazione();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public void raccoltaDati() {
boolean Flag1=false;
String nome=txtNome.getText();
String cognome=txtCognome.getText();
String user=txtUser.getText();
String psw=txtPsw.getText();
String psw2=txtPsw2.getText();
String mail=txtMail.getText();
String eta=txtEta.getText();
if (psw.equals(psw2)) {
System.out.println(" E' TRUE ");
Flag1=true;
}
}
public void verificaDoppioni() {
connection = sqliteConnection.dbConnector();
String user=txtUser.getText();
String psw=txtPsw.getText();
String psw2=txtPsw2.getText();
boolean Flag2=false;
try {
String query = "select * from EmployeeInfo ";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
int count = 0;
while (rs.next()) {
String xx = rs.getString("Username");
if (xx.equals(user)) {
Flag2=true;
}
}
System.out.println("Flag2 "+Flag2);
if (Flag2==true) {
JOptionPane.showMessageDialog(null, "User Gia' Utilizzato!! ");
txtNome.setText("");
txtCognome.setText("");
txtUser.setText("");
txtPsw.setText("");
txtPsw2.setText("");
txtMail.setText("");
txtEta.setText("");
consenso=false;
}
if (psw.equals(psw2)) {
System.out.println("OK **** Si Vaaaaaaaaaaaaaaaaaaa");
consenso=true;
}
else {
JOptionPane.showMessageDialog(null, "Password non corrispondenti ");
txtNome.setText("");
txtCognome.setText("");
txtUser.setText("");
txtPsw.setText("");
txtPsw2.setText("");
txtMail.setText("");
txtEta.setText("");
consenso=false;
}
rs.close();
pst.close();
} catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
System.out.println("Ok registrazione OK");
}
public void scriviDatDb() {
boolean Flag;
String nome=txtNome.getText();
String cognome=txtCognome.getText();
String user=txtUser.getText();
String psw=txtPsw.getText();
String psw2=txtPsw2.getText();
String mail=txtMail.getText();
String eta=txtEta.getText();
String admin="false";
if (psw.equals(psw2)) {
System.out.println(" E' TRUE ");
Flag=true;
}
else
Flag=false;
System.out.println("il flag e' "+Flag);
verificaDoppioni();
if (consenso==true) {
try {
// String query = "insert into Ordini (data,utente,articolo1,prezzo1,articolo2,prezzo2,articolo3,prezzo3,articolo4,prezzo4,articolo5,prezzo5) values(?,?,?,?,?,?,?,?,?,?,?,?)";
// String query2= "insert into OrdineNew (Data,Utente,ImportoTotale,Evaso) values(?,?,?,?)";
// String query3= "insert into OrdineDettaglio (Id_ordine,Pezzi,Descrizione,Prezzo) values(?,?,?,?)";
String query = "insert into EmployeeInfo (Name,Surname,Username,password,Age,Administrator,Mail,Inscritto,Ultimoaccesso) values(?,?,?,?,?,?,?,?,?)";
connection = sqliteConnection.dbConnector();
PreparedStatement pst1 = connection.prepareStatement(query);
String dataLocale= new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(Calendar.getInstance().getTime()) ; //"yyyy/MM/dd HH:mm:ss")
int prezzoTot=100;
String evaso="false";
pst1.setString(1, nome);
pst1.setString(2, cognome);
pst1.setString(3, user);
pst1.setString(4, psw);
pst1.setString(5, eta);
pst1.setString(6, admin);
pst1.setString(7, mail);
pst1.setString(8, dataLocale);
pst1.setString(9, dataLocale);
pst1.execute();
pst1.close();
JOptionPane.showMessageDialog(null, "Data Saved");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("PROBLEMI...SCRITTO NIENTE");
}
System.out.println("FINE REGOLARE");
//close();
}
public void close() {
WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
}
public registrazione() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 660, 510);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("PANNELLO REGISTRAZIONE");
lblNewLabel.setForeground(Color.GRAY);
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 23));
lblNewLabel.setBounds(22, 24, 435, 61);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Nome");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_1.setBounds(22, 119, 94, 35);
contentPane.add(lblNewLabel_1);
JLabel lblCognome = new JLabel("Cognome");
lblCognome.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCognome.setBounds(22, 165, 94, 35);
contentPane.add(lblCognome);
JLabel lblUser = new JLabel("User");
lblUser.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblUser.setBounds(22, 213, 94, 35);
contentPane.add(lblUser);
JLabel lblPsw = new JLabel("Psw");
lblPsw.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblPsw.setBounds(22, 259, 94, 35);
contentPane.add(lblPsw);
JLabel lblConfermaPsw = new JLabel("Conferma Psw");
lblConfermaPsw.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblConfermaPsw.setBounds(22, 305, 118, 35);
contentPane.add(lblConfermaPsw);
JLabel lblMail = new JLabel("Mail");
lblMail.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblMail.setBounds(22, 351, 94, 35);
contentPane.add(lblMail);
JLabel lblEta = new JLabel("Eta'");
lblEta.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblEta.setBounds(22, 397, 94, 35);
contentPane.add(lblEta);
txtNome = new JTextField();
txtNome.setBounds(150, 119, 248, 35);
contentPane.add(txtNome);
txtNome.setColumns(10);
txtCognome = new JTextField();
txtCognome.setColumns(10);
txtCognome.setBounds(150, 165, 248, 35);
contentPane.add(txtCognome);
txtUser = new JTextField();
txtUser.setColumns(10);
txtUser.setBounds(150, 213, 248, 35);
contentPane.add(txtUser);
txtPsw = new JTextField();
txtPsw.setColumns(10);
txtPsw.setBounds(150, 259, 248, 35);
contentPane.add(txtPsw);
txtPsw2 = new JTextField();
txtPsw2.setColumns(10);
txtPsw2.setBounds(150, 305, 248, 35);
contentPane.add(txtPsw2);
txtMail = new JTextField();
txtMail.setColumns(10);
txtMail.setBounds(150, 351, 248, 35);
contentPane.add(txtMail);
txtEta = new JTextField();
txtEta.setColumns(10);
txtEta.setBounds(150, 397, 248, 35);
contentPane.add(txtEta);
JButton btnNewButton = new JButton("Registrati");
btnNewButton.setForeground(SystemColor.textHighlight);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scriviDatDb();
//contentPane.close;
}
});
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 18));
btnNewButton.setBounds(495, 437, 139, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Esci");
btnNewButton_1.setForeground(SystemColor.textHighlight);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
close();
//System.exit(0);
//contentPane.setVisible(false);
//contentPane.setVisible(true);
// JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JFrame.dispose();
// JFrame.dispatchEvent(close);
}
});
btnNewButton_1.setFont(new Font("Tahoma", Font.BOLD, 17));
btnNewButton_1.setBounds(540, 365, 94, 35);
contentPane.add(btnNewButton_1);
JSeparator separator = new JSeparator();
separator.setBounds(22, 82, 352, 8);
contentPane.add(separator);
JLabel lblNewLabel_2 = new JLabel("");
Image img2 = new ImageIcon(this.getClass().getResource("/Register-icon.png")).getImage(); // questa riga e la successiva mette l'immagine *****
lblNewLabel_2.setIcon(new ImageIcon(img2));
lblNewLabel_2.setBounds(460, 24, 161, 178);
contentPane.add(lblNewLabel_2);
}
}