I'm trying to let my button push (INSERT INTO) Username & Password to my database with the use of eclipse program. Netbeans provides the Myconnection.getConnection(); but in eclipse it give me an error. I tried a different way to code it with Connection conn = Drivermanager.getConnection();
But when I use this I need to put Class.forname("com.mysql.jdbc.Driver"). When using this my catch has to be (Exception e). But for my sql I NEED catch (SQLException e). But then again my Class.forname gives me an error. Anyone who knows how to fix this issue?
package Registeren;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JToolBar;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
public class Registeren {
private JFrame frame;
private JTextField txtRegUsername;
private JPasswordField txtRegPassword;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Registeren window = new Registeren();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Registeren() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1376, 869);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.DARK_GRAY);
panel.setBorder(null);
JLabel lblRegistreren = new JLabel("REGISTREREN");
lblRegistreren.setMaximumSize(new Dimension(82, 16));
lblRegistreren.setFont(new Font("Tahoma", Font.BOLD, 80));
JLabel lblUsername = new JLabel("Gebruikersnaam");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 30));
txtRegUsername = new JTextField();
txtRegUsername.setFont(new Font("Tahoma", Font.PLAIN, 30));
txtRegUsername.setColumns(10);
txtRegUsername.setAutoscrolls(false);
JLabel lblPassword = new JLabel("Wachtwoord");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 30));
JButton btnRegister = new JButton("Aanmaken");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String driver = "com.mysql.jbdc.Driver";
Class.forName(driver);
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant", "root", "");
String User = txtRegUsername.getText();
String Pass = String.copyValueOf(txtRegPassword.getPassword());
String sql = " insert into login (Username, Password)" + " values (?, ?)";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString (1, User);
stmt.setString (2, Pass);
JOptionPane.showMessageDialog(null, "Nieuwe gebruikers toegevoegd!");
stmt.execute();
con.close();
} catch(Exception ex) {
Logger.getLogger(Registeren.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
btnRegister.setBorder(null);
btnRegister.setBackground(new Color(46, 139, 87));
btnRegister.setAutoscrolls(true);
JButton btnDeleteUser = new JButton("Verwijderen");
btnDeleteUser.setBorder(null);
btnDeleteUser.setBackground(new Color(255, 0, 0));
btnDeleteUser.setAutoscrolls(true);
txtRegPassword = new JPasswordField();
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 364, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(201)
.addComponent(lblRegistreren, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(224)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(btnRegister, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnDeleteUser, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblPassword, GroupLayout.PREFERRED_SIZE, 236, GroupLayout.PREFERRED_SIZE)
.addComponent(lblUsername, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(txtRegPassword, 304, 304, 304)
.addComponent(txtRegUsername, GroupLayout.PREFERRED_SIZE, 304, GroupLayout.PREFERRED_SIZE))))))
.addGap(211))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 822, Short.MAX_VALUE)
.addGroup(groupLayout.createSequentialGroup()
.addGap(41)
.addComponent(lblRegistreren, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 228, Short.MAX_VALUE)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(6)
.addComponent(lblUsername, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
.addComponent(txtRegUsername, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE))
.addGap(37)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(lblPassword, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
.addGap(90))
.addGroup(groupLayout.createSequentialGroup()
.addComponent(txtRegPassword, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
.addGap(81)))
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(btnRegister, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
.addComponent(btnDeleteUser, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE))
.addGap(136))
);
frame.getContentPane().setLayout(groupLayout);
}
}
The error show up after
try {
String driver = "com.mysql.jbdc.Driver";
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant","root","");
***Class.forName("com.mysql.jdbc.Driver");*** <-- ERROR
SQLException into Exception ERROR
Username and Password INSERT INTO mysql database
So I have made a new project java. Added everything new again. It looks like the Class.forname(Driver); isn't connecting the right way.
package Registeren;
import java.awt.EventQueue;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Register {
private JFrame frame;
private JTextField txtRegUsername;
private JPasswordField txtRegPassword;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register window = new Register();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Register() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1442, 938);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(null);
panel.setBackground(Color.DARK_GRAY);
JLabel lblRegisteren = new JLabel("REGISTREREN");
lblRegisteren.setMaximumSize(new Dimension(82, 16));
lblRegisteren.setFont(new Font("Tahoma", Font.BOLD, 80));
JLabel lblUsername = new JLabel("Gebruikersnaam");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 30));
txtRegUsername = new JTextField();
txtRegUsername.setFont(new Font("Tahoma", Font.PLAIN, 30));
txtRegUsername.setColumns(10);
txtRegUsername.setAutoscrolls(false);
JLabel lblPassword = new JLabel("Wachtwoord");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 30));
txtRegPassword = new JPasswordField();
JButton btnRegister = new JButton("Aanmaken");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
String driver = "com.mysql.jbdc.Driver";
Class.forName(driver);
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant", "root", "");
String User = txtRegUsername.getText();
String Pass = String.copyValueOf(txtRegPassword.getPassword());
String sql = " insert into login (Username, Password)" + " values (?, ?)";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString (1, User);
stmt.setString (2, Pass);
JOptionPane.showMessageDialog(null, "Nieuwe gebruikers toegevoegd!");
stmt.execute();
con.close();
} catch(Exception ex) {
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
btnRegister.setBorder(null);
btnRegister.setBackground(new Color(46, 139, 87));
btnRegister.setAutoscrolls(true);
JButton btnDelete = new JButton("Verwijderen");
btnDelete.setBorder(null);
btnDelete.setBackground(Color.RED);
btnDelete.setAutoscrolls(true);
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 364, GroupLayout.PREFERRED_SIZE)
.addGap(201)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblRegisteren, GroupLayout.PREFERRED_SIZE, 582, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createSequentialGroup()
.addGap(23)
.addComponent(lblUsername, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE)
.addGap(24)
.addComponent(txtRegUsername, GroupLayout.PREFERRED_SIZE, 304, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(23)
.addComponent(lblPassword, GroupLayout.PREFERRED_SIZE, 236, GroupLayout.PREFERRED_SIZE)
.addGap(5)
.addComponent(txtRegPassword, GroupLayout.PREFERRED_SIZE, 304, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(23)
.addComponent(btnRegister, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
.addGap(273)
.addComponent(btnDelete, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(277, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(lblRegisteren, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE)
.addGap(228)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(6)
.addComponent(lblUsername, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
.addComponent(txtRegUsername, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE))
.addGap(37)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(lblPassword, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createSequentialGroup()
.addGap(8)
.addComponent(txtRegPassword, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)))
.addGap(81)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(btnRegister, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
.addComponent(btnDelete, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)))
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 891, Short.MAX_VALUE)
);
frame.getContentPane().setLayout(groupLayout);
}
}
The following shows up to error
Mar 23, 2019 6:46:38 PM Registeren.Register$2 actionPerformed SEVERE: null java.lang.ClassNotFoundException: com.mysql.jbdc.Driver at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.Class.forName0(N***ative Method***) at java.lang.Class.forName(Unknown Source) at Registeren.Register$2.actionPerformed(Register.java:88) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
ERROR: Class.forname(driver);
try {
String driver = "com.mysql.jbdc.Driver";
Class.forName(driver);
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant", "root", "");