Ho sviluppato utilizzando eclipse una classe in modalità visuale per eseguire il login.
Utilizzo un data base access in fase di test, poi in effettivo utilizzerò Sql server express.
In fase di test ho scritto una classe che effettua la connessione al database.
Con la vostra assistenza ho corretto alcuni errori che impedivano la funzionalità ed ora è corretta.
Riporto il codice della classe
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// Funziona
public class InternetDbConn {
public static void main(String[] args) {
Connection conn = null;
Statement s = null;
ResultSet rs = null;
try {
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); non funziona - non più supportato il driver jdbc.odbc
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Problem in loading MS Access JDBC driver");
ex.printStackTrace();
System.out.println(ex.getMessage());
}
try
{
//String msAccDB = "C:\\Archivi\\Sif.accdb";
String msAccDB = "C:/Archivi/Sif.accdb";
String database = "jdbc:ucanaccess://" + msAccDB;
conn = DriverManager.getConnection(database);
System.out.println("Eseguita correttamente la connessione");
s = conn.createStatement();
// Fetch table
String selTable = "SELECT * FROM Anadip";
s.execute(selTable);
rs = s.getResultSet();
while((rs!=null) && (rs.next()))
{
System.out.println(rs.getString(1) + " : " + rs.getString(2));
}
// close and cleanup
s.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
System.out.println(ex.getMessage());
}
finally {
// Step 3: Closing database connection
try {
if(null != conn) {
// cleanup resources, once after processing
rs.close();
s.close();
// and then finally close connection
conn.close();
}
}
catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
}
}
a verifica della correttezza funzionale della classe edito i record della tabella
Eseguita correttamente la connessione
9999 : ADMINISTRATOR
1 : CANTO
2 : TROMBIN
3 : MORENO
4 : PROVA
Ho pertanto riportato il codice della classe in cui viene eseguita la connesione al database nella classe visuale in cui ho definito i campi per la login
La connessione NON viene eseguita inspiegabilmente.
Penso di aver riportato correttamente tutti gli import necessari e definito correttamente le variabili.
Riporto il codice della classe Login in cui NON avviene la connessione
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import javax.swing.JPasswordField;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.JTextPane;
import javax.swing.JSpinner;
import javax.swing.JList;
import javax.swing.JSlider;
import javax.swing.JScrollBar;
import javax.swing.JSeparator;
import javax.swing.JFormattedTextField;
import javax.swing.SwingConstants;
import javax.swing.JInternalFrame;
import java.sql.*;
public class login extends JFrame {
private JPanel contentPane;
private JTextField txtUserName;
private JPasswordField pwdUser;
// parametri per connessione
private static Connection conn;
private static Statement s;
private static ResultSet rs;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
EseguiConnessione();
login frame = new login();
frame.setTitle("Login in Java");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public login() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 473, 289);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBounds(33, 63, 196, 26);
contentPane.add(panel_1);
panel_1.setLayout(null);
JLabel lblPwd = new JLabel("Password");
lblPwd.setBounds(0, 12, 60, 14);
panel_1.add(lblPwd);
pwdUser = new JPasswordField();
pwdUser.setBounds(87, 0, 109, 20);
panel_1.add(pwdUser);
JLabel lblNameUtente = new JLabel("Visualizza Nome Utente");
lblNameUtente.setFont(new Font("Tempus Sans ITC", Font.PLAIN, 14));
lblNameUtente.setForeground(new Color(255, 127, 80));
lblNameUtente.setBounds(33, 103, 251, 20);
contentPane.add(lblNameUtente);
JLabel lblRuolo = new JLabel("Visualizza Ruolo Utente");
lblRuolo.setForeground(new Color(255, 127, 80));
lblRuolo.setFont(new Font("Tempus Sans ITC", Font.PLAIN, 14));
lblRuolo.setBounds(33, 134, 231, 20);
contentPane.add(lblRuolo);
JButton btnLogin = new JButton("Login");
btnLogin.setBounds(80, 181, 109, 28);
contentPane.add(btnLogin);
JLabel lblLogo = new JLabel("");
Image imgLogo = new ImageIcon(this.getClass().getResource("/imagesLogin.png")).getImage();
lblLogo.setIcon(new ImageIcon(imgLogo));
lblLogo.setBounds(316, 35, 108, 135);
contentPane.add(lblLogo);
JPanel panel = new JPanel();
panel.setBounds(33, 27, 192, 20);
contentPane.add(panel);
panel.setLayout(null);
JLabel lblUserName = new JLabel("User Name");
lblUserName.setBounds(0, 3, 52, 14);
panel.add(lblUserName);
lblUserName.setHorizontalAlignment(SwingConstants.LEFT);
txtUserName = new JTextField();
txtUserName.setBounds(106, 0, 86, 20);
panel.add(txtUserName);
txtUserName.setColumns(10);
JPanel panel_2 = new JPanel();
panel_2.setBounds(18, 226, 441, 21);
contentPane.add(panel_2);
panel_2.setLayout(null);
JLabel lblIconMessage = new JLabel("ico");
Image imgLogoMessage = new ImageIcon(this.getClass().getResource("/Ko32x32.png")).getImage();
lblIconMessage.setIcon(new ImageIcon(imgLogoMessage));
lblIconMessage.setBounds(0, 0, 32, 32);
panel_2.add(lblIconMessage);
lblIconMessage.setHorizontalAlignment(SwingConstants.LEFT);
JSeparator separator01 = new JSeparator();
separator01.setBounds(18, 0, 1, 18);
panel_2.add(separator01);
separator01.setOrientation(SwingConstants.VERTICAL);
separator01.setBackground(Color.BLUE);
JLabel lblDay = new JLabel("Day");
lblDay.setBounds(293, 1, 69, 20);
panel_2.add(lblDay);
lblDay.setHorizontalAlignment(SwingConstants.LEFT);
JSeparator separator02 = new JSeparator();
separator02.setOrientation(SwingConstants.VERTICAL);
separator02.setBackground(Color.BLUE);
separator02.setBounds(335, 0, 1, 18);
contentPane.add(separator02);
JLabel lblHms = new JLabel("hh:mm:ss");
lblHms.setBounds(372, 0, 69, 20);
panel_2.add(lblHms);
lblHms.setHorizontalAlignment(SwingConstants.LEFT);
JSeparator separator03 = new JSeparator();
separator03.setBounds(368, 0, 1, 18);
panel_2.add(separator03);
separator03.setOrientation(SwingConstants.VERTICAL);
separator03.setBackground(Color.BLUE);
JFormattedTextField formattedTextField = new JFormattedTextField();
formattedTextField.setBounds(19, 1, 264, 20);
panel_2.add(formattedTextField);
formattedTextField.setText("riga messaggi");
formattedTextField.setHorizontalAlignment(SwingConstants.LEFT);
}
public static void EseguiConnessione()
{
conn = null;
s = null;
rs = null;
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
}
catch(ClassNotFoundException ex) {
System.out.println("Problem in loading MS Access JDBC driver");
ex.printStackTrace();
System.out.println(ex.getMessage());
}
try
{
String msAccDB = "C:/Archivi/Sif.accdb";
String database = "jdbc:ucanaccess://" + msAccDB;
conn = DriverManager.getConnection(database);
System.out.println("Eseguita correttamente la connessione");
}
catch(Exception ex)
{
ex.printStackTrace();
System.out.println(ex.getMessage());
}
}
}
il log dell'errore è:
Problem in loading MS Access JDBC driver
java.lang.ClassNotFoundException: net.ucanaccess.jdbc.UcanaccessDriver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Unknown Source)
at login.EseguiConnessione(login.java:190)
at login$1.run(login.java:61)
at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.EventQueue.access$600(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
net.ucanaccess.jdbc.UcanaccessDriver
java.sql.SQLException: No suitable driver found for jdbc:ucanaccess://C:/Archivi/Sif.accdb
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
at login.EseguiConnessione(login.java:205)
at login$1.run(login.java:61)
at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.EventQueue.access$600(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
No suitable driver found for jdbc:ucanaccess://C:/Archivi/Sif.accdb
la form che dovrei utilizzare per l'effettuazione del login è in allegato
Che errore commetto ?
Grazie per la pazienza.
ciao
Moreno
Allegati: