Salve ho un problema con la connesione al database. Ho costruito una maschera per inserire i dati in un database access, ma al momento della connessione ho questo errore " Errore durante l'inserimento nel database: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/Associati.accdb ". Ho scaricato Mysql e l'ho in stallato nel computer, il mio codice è il seguente :
Potete darmi un consiglio
javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import static java.time.Clock.system;
/**
*
* @author dacer
*/
public class Inserimentodati {
public static void main(String[] args) {
// Crea la finestra principale
JFrame frame = new JFrame("BINARIO ZERO - Inserimento Dati");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
// Crea il pannello con GridBagLayout che gestirà la disposizione
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
// COGNOME ETICHETTA
JLabel lbcg = new JLabel("COGNOME");
gbc.gridx = 0; // Colonna 0
gbc.gridy = 0; // Riga 0
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(lbcg, gbc);
// COGNOME CAMPO TESTO
JTextField tfcg = new JTextField(22);
gbc.gridx = 1; // Colonna 0
gbc.gridy = 0; // Riga 1
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(tfcg, gbc);
// NOME ETICHETTA
JLabel lbn = new JLabel("NOME");
gbc.gridx = 0; // Colonna 0
gbc.gridy = 3; // Riga 3
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(lbn, gbc);
// NOME CAMPO TESTO
JTextField tfn = new JTextField(22);
gbc.gridx = 1; // Colonna 0
gbc.gridy = 3; // Riga 4
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(tfn, gbc);
// CODICE FISCALE ETICHETTA
JLabel lbcf= new JLabel("COD. FISC.");
gbc.gridx = 0; // Colonna 0
gbc.gridy = 6; // Riga 6
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(lbcf, gbc);
// CODICE FISCALE CAMPO TESTO
JTextField tfcf = new JTextField(22);
gbc.gridx = 1; // Colonna 0
gbc.gridy = 6; // Riga 7
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(tfcf, gbc);
// TELEFONO ETICHETTA
JLabel lbtf = new JLabel("TELEFONO");
gbc.gridx = 0; // Colonna 0
gbc.gridy = 9; // Riga 9
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(lbtf, gbc);
// TELEFONO CAMPO TESTO
JTextField tftl = new JTextField(22);
gbc.gridx = 1; // Colonna 0
gbc.gridy = 9; // Riga 10
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(tftl, gbc);
// MAIL ETICHETTA
JLabel lbm = new JLabel("MAIL");
gbc.gridx = 0; // Colonna 0
gbc.gridy = 12; // Riga 12
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(lbm, gbc);
// MAIL CAMPO TESTO
JTextField tfm = new JTextField(50);
gbc.gridx = 1; // Colonna 0
gbc.gridy = 12; // Riga 13
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(tfm, gbc);
// PULSANTE INVIA
JButton invia = new JButton("INVIA");
gbc.gridx = 0; // Colonna 0
gbc.gridy = 15; // Riga 12
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(invia, gbc);
// INVIO DATI E REGISTRAZIONE
ActionListener inviaAzione = new ActionListener() {
public void actionPerformed(ActionEvent e) {
//SCARICO I VALORI NEI VARI CAMPI
String cg = tfcg.getText();
String n = tfn.getText();
String cf = tfcf.getText();
String tel = tftl.getText();
String m = tfm.getText();
// Percorso del file del database Access
String url = "jdbc:mysql://localhost:3306/Associati.accdb";
String utente = "root";
String password = "";
// INSERIMENTO DATI NEL DATABASE
String sql = "INSERT INTO Associati (cognome, nome, codice fiscale, tyelefono, mail) VALUES (cg, n, cf, tl, m)";
try (Connection conn = DriverManager.getConnection(url);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// Imposta i valori dei parametri della query
pstmt.setString(1, cg);
pstmt.setString(2, n);
pstmt.setString(3, cf);
pstmt.setString(4, tel);
pstmt.setString(5, m);
// Esegue l'inserimento
int righe = pstmt.executeUpdate();
if (righe > 0) {
System.out.println("Dato inserito con successo!");
}
tfcg.setText(""); // Pulisce il campo
tfn.setText(""); // Pulisce il campo
tfcf.setText(""); // Pulisce il campo
tftl.setText(""); // Pulisce il campo
tfm.setText(""); // Pulisce il campo
} catch (SQLException ex) {
System.out.println("Errore durante l'inserimento nel database:");
ex.printStackTrace();
}
}
};
// Collega l'azione al click del pulsante
invia.addActionListener(inviaAzione);
//FINE INVIO DATI E REGISTRAZIONE
// PULSANTE PULISCI CAMPI
JButton btnPulisci = new JButton("PULISCI CAMPI");
gbc.gridx = 1; // Colonna 0
gbc.gridy = 15; // Riga 12
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10, 20, 10, 10);
panel.add(btnPulisci, gbc);
// Inizio codice pulisci campi JTextField
// Azione del pulsante Pulisci
btnPulisci.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Svuota i campi di testo
tfcg.setText("");
tfn.setText("");
tfcf.setText("");
tftl.setText("");
tfm.setText("");
// Riporta il cursore sul primo campo
tfcg.requestFocus();
};
});
// Fine codice pulisci campi JTextField
// Aggiunge il pannello alla finestra
frame.add(panel);
frame.setVisible(true);
}
}