codice:
package org.schedule.iu;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Login2 {
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="38,21"
private JMenuBar jJMenuBar = null;
private JMenu fileMenu = null;
private JMenu helpMenu = null;
private JMenuItem exitMenuItem = null;
private JMenuItem aboutMenuItem = null;
private JPasswordField jPasswordField1;
private JButton jButton1;
private JLabel jLabel3;
private JLabel jLabel2;
private JTextField jTextField2;
private JLabel jLabel1;
private JDialog aboutDialog = null;
private JPanel aboutContentPane = null;
private JLabel aboutVersionLabel = null;
public static boolean utenteLoggato = false;
private Connection connessione;
public void init()
{
try {
// Inizia connessione con il database
Class.forName("com.mysql.jdbc.Driver");
connessione=DriverManager.getConnection("jdbc:mysql://localhost:3306/schedule" , "root","root");
}
catch (ClassNotFoundException cnfex) {
//processo ClassNotFoundExceptions qui
System.err.println("Caricamento di driver JDBC/ODBC non riuscito");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex) {
//processo SQLExceptions qui
System.err.println("Non è possibile completare la connessione");
sqlex.printStackTrace();
}}
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame() {
if (jFrame == null) {
jFrame = new JFrame();
FormLayout jFrameLayout = new FormLayout(
"max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu)",
"max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu), max(p;5dlu)");
jFrame.getContentPane().setLayout(jFrameLayout);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setJMenuBar(getJJMenuBar());
jFrame.setSize(1025, 588);
jFrame.setTitle("Schedule");
jFrame.setPreferredSize(new java.awt.Dimension(1025, 588));
jFrame.getContentPane().setBackground(new java.awt.Color(0,128,255));
jFrame.getContentPane().add(getJLabel1(), new CellConstraints("4, 1, 1, 1, default, default"));
jFrame.getContentPane().add(getJTextField2(), new CellConstraints("4, 2, 1, 1, default, default"));
jFrame.getContentPane().add(getJPasswordField1(), new CellConstraints("4, 3, 1, 1, default, default"));
jFrame.getContentPane().add(getJLabel2(), new CellConstraints("1, 2, 1, 1, right, top"));
jFrame.getContentPane().add(getJLabel3(), new CellConstraints("1, 3, 1, 1, right, top"));
jFrame.getContentPane().add(getJButton1(), new CellConstraints("4, 4, 1, 1, default, center"));
}
return jFrame;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getFileMenu());
jJMenuBar.add(getHelpMenu());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getFileMenu() {
if (fileMenu == null) {
fileMenu = new JMenu();
fileMenu.setText("File");
fileMenu.add(getExitMenuItem());
}
return fileMenu;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getHelpMenu() {
if (helpMenu == null) {
helpMenu = new JMenu();
helpMenu.setText("Help in Linea");
helpMenu.add(getAboutMenuItem());
}
return helpMenu;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getExitMenuItem() {
if (exitMenuItem == null) {
exitMenuItem = new JMenuItem();
exitMenuItem.setText("Esci");
exitMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
return exitMenuItem;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getAboutMenuItem() {
if (aboutMenuItem == null) {
aboutMenuItem = new JMenuItem();
aboutMenuItem.setText("Come funziona Schedule?");
aboutMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog aboutDialog = getAboutDialog();
aboutDialog.pack();
Point loc = getJFrame().getLocation();
loc.translate(20, 20);
aboutDialog.setLocation(loc);
aboutDialog.setVisible(true);
}
});
}
return aboutMenuItem;
}
/**
* This method initializes aboutDialog
*
* @return javax.swing.JDialog
*/
private JDialog getAboutDialog() {
if (aboutDialog == null) {
aboutDialog = new JDialog(getJFrame(), true);
aboutDialog.setTitle("Schedule");
aboutDialog.setContentPane(getAboutContentPane());
}
return aboutDialog;
}
/**
* This method initializes aboutContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getAboutContentPane() {
if (aboutContentPane == null) {
aboutContentPane = new JPanel();
aboutContentPane.setLayout(new BorderLayout());
aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
}
return aboutContentPane;
}
/**
* This method initializes aboutVersionLabel
*
* @return javax.swing.JLabel
*/
private JLabel getAboutVersionLabel() {
if (aboutVersionLabel == null) {
aboutVersionLabel = new JLabel();
aboutVersionLabel.setText("STUDIARE è DIFFICILE ... soprattutto il Java!!!");
aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
return aboutVersionLabel;
}
/**
* Launches this application
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Login2 application = new Login2();
application.getJFrame().setVisible(true);
}
});
}
private JLabel getJLabel1() {
if(jLabel1 == null) {
jLabel1 = new JLabel();
jLabel1.setText(" " +
"");
jLabel1.setIcon(new ImageIcon(getClass().getClassLoader().getResource("img/schedule.png")));
}
return jLabel1;
}
private JTextField getJTextField2() {
if(jTextField2 == null) {
jTextField2 = new JTextField();
}
return jTextField2;
}
private JPasswordField getJPasswordField1() {
if(jPasswordField1 == null) {
jPasswordField1 = new JPasswordField();
}
return jPasswordField1;
}
private JLabel getJLabel2() {
if(jLabel2 == null) {
jLabel2 = new JLabel();
jLabel2.setText("Username");
jLabel2.setFont(new java.awt.Font("Comic Sans MS",1,18));
jLabel2.setBackground(new java.awt.Color(0,128,192));
jLabel2.setLocation(new java.awt.Point(0, 0));
jLabel2.setForeground(new java.awt.Color(255,255,255));
}
return jLabel2;
}
private JLabel getJLabel3() {
if(jLabel3 == null) {
jLabel3 = new JLabel();
jLabel3.setText("Password");
jLabel3.setFont(new java.awt.Font("Comic Sans MS",1,18));
jLabel3.setForeground(new java.awt.Color(255,255,255));
}
return jLabel3;
}
private JButton getJButton1() {
if(jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("entra");
// definizioni dei campi di testo
jTextField2=new JTextField(10);
// questo campo accetterà solo numeri e il pulsante
jPasswordField1=new JPasswordField(10);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
Login(e); }
public void Login(ActionEvent e)
{
try {
String text2 = jPasswordField1.getText();
String text = text2;
if(!jTextField2.getText().equals("") && !text.equals(""))
{
Statement statement =connessione.createStatement();
// query che usa i dati dei campi Codice e Password della tabella Utenti
String query="SELECT Cognome, Password FROM Utente_Docente WHERE Cognome='"+ jTextField2.getText()+
"' AND Password='"+jPasswordField1.getText()+"'";
// stampa della stringa della query
System.out.println("\nInviando query: "+connessione.nativeSQL(query)+"\n");
ResultSet resultset=statement.executeQuery(query);
display(resultset);
statement.close();
}
else if(jTextField2.getText().equals("") && !text.equals(""))
{
JOptionPane.showMessageDialog(null, "Inserire Nome Utente","Login", JOptionPane.ERROR_MESSAGE);
}
else if(!jTextField2.getText().equals("") && text.equals(""))
{
JOptionPane.showMessageDialog(null, "Inserire Password","Login", JOptionPane.ERROR_MESSAGE);
}
else
{ JOptionPane.showMessageDialog(null, "Inserire Nome Utente e Password","Login", JOptionPane.ERROR_MESSAGE);}
}
// cattura eventuali errori ed eccezioni nella richiesta de la query
catch(SQLException sqlex)
{
sqlex.printStackTrace();
}
;
}
public void display(ResultSet resultset)
{
String urlString="Programma.html";
URL url = null;
try
{
if(resultset.next())
{
// Carica il file Programma.html nella stessa pagina dell'applet
JOptionPane.showMessageDialog(null, "Utente loggato" ,"Login", JOptionPane.INFORMATION_MESSAGE);
}
else
{JOptionPane.showMessageDialog(null, "Password e/o Username errate, Riprovare!!","Login", JOptionPane.ERROR_MESSAGE);
return; }
}
catch (SQLException sqlex)
{
sqlex.printStackTrace();
}
}
}
);
}
return jButton1;
}
}