I made a simple form for the user to enter in customer data it the takes the data in the text field and inserts it into the database I have running thorough Microsoft SQL Server Management Studio 18. The database is running on my computer. I'm having issues with the driver part of my code, it keeps throwing an exception. Any help on what I'm doing wrong is greatly appreciated. This is the following message I get every time I run the program and hit the submit button: Exception thrown: com.microsoft.sqlserver.jdbc.SQLServerDriver java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver Full Stack trace for issue: at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:333) at DBFE2.actionPerformed(DBFE2.java:119) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967) at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308) at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405) at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262) at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279) at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636) at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342) at java.desktop/java.awt.Component.processEvent(Component.java:6401) at java.desktop/java.awt.Container.processEvent(Container.java:2263) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844) at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918) at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547) at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
package Assignments;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;//import all
import java.sql.*;//need this to make database connection
public class DataBaseFormExample extends JPanel implements ActionListener
{
private JLabel FirstNameLabel;
private JTextField FirstNameTextField;
private JLabel LastNameLabel;
private JTextField LastNameTextField;
private JLabel PhoneNumLabel;
private JTextField PhoneNumTextField;
private JLabel StreetAddressLabel;
private JTextField StreetAddressTextField;
private JLabel StateLabel;
private JTextField StateTextField;
private JLabel CityLabel;
private JTextField CityTextField;
private JLabel ZipLabel;
private JTextField ZipTextField;
private JLabel GenderLabel;
private JTextField GenderTextField;
private JLabel DOBLabel;
private JTextField DOBTextField;
private JLabel MaritalStatusLabel;
private JTextField MartialStatusTextField;
//constructor
public DataBaseFormExample()
{
//intializing components for form below
FirstNameLabel = new JLabel("Enter the first name of Customer below:");
FirstNameTextField = new JTextField("FirstName");
FirstNameTextField.setColumns(10);
LastNameLabel = new JLabel("Enter the first name of Customer below:");
LastNameTextField = new JTextField("LastName");
LastNameTextField.setColumns(10);
PhoneNumLabel = new JLabel("Enter the Phone Number of Customer below(EX:123-456-7890):");
PhoneNumTextField = new JTextField("123-456-7890");
PhoneNumTextField.setColumns(10);
StreetAddressLabel = new JLabel("Enter the street address of Customer below:");
StreetAddressTextField = new JTextField("123 easy street");
StreetAddressTextField.setColumns(10);
StateLabel = new JLabel("Enter the state of Customer below(EX:PA):");
StateTextField = new JTextField("PA");
StateTextField.setColumns(10);
CityLabel = new JLabel("Enter the city of Customer below(EX:Pittsburgh):");
CityTextField = new JTextField("Pittsburgh");
CityTextField.setColumns(10);
ZipLabel = new JLabel("Enter the zip of Customer below(EX:12345):");
ZipTextField = new JTextField("12345");
ZipTextField.setColumns(10);
GenderLabel = new JLabel("Enter the gender of Customer below(EX:M or F):");
GenderTextField = new JTextField("M");
GenderTextField.setColumns(10);
DOBLabel = new JLabel("Enter the DOB of Customer below(EX:12/03/5678):");
DOBTextField = new JTextField("12/34/2010");
DOBTextField.setColumns(10);
MaritalStatusLabel = new JLabel("Enter the martial status of Customer below(EX: Single / Married):");
MartialStatusTextField = new JTextField("Single");
MartialStatusTextField.setColumns(10);
JButton submitBTN = new JButton("Submit info");
submitBTN.addActionListener(this);
//add components to form
add(FirstNameLabel);
add(FirstNameTextField);
add(LastNameLabel);
add(LastNameTextField);
add(PhoneNumLabel);
add(PhoneNumTextField);
add(StreetAddressLabel);
add(StreetAddressTextField);
add(StateLabel);
add(StateTextField);
add(CityLabel);
add(CityTextField);
add(ZipLabel);
add(ZipTextField);
add(GenderLabel);
add(GenderTextField);
add(DOBLabel);
add(DOBTextField);
add(MaritalStatusLabel);
add(MartialStatusTextField);
add(submitBTN);
}
//driver method
public static void CreateAndShowContent()
{
//frame of calculator
JFrame window = new JFrame("Customer Information Input Form");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//closes when we click the red x
DataBaseFormExample content = new DataBaseFormExample ();
content.setOpaque(true);
window.setContentPane(content);
//size it to fit components
window.setSize(500,330);
//show it
window.setVisible(true);
}
//insert when the button submit is pressed
public void actionPerformed(ActionEvent e)
{
try
{
//try inserting data to database
String JDBC_DRIVER = "org.gjt.mm.mysql.Driver";
String DB_URL = "jdbc:mysql://localhost/Giant_Snail_Grocery";
String UserName = "username";
String UserPass = "password";
Connection conn = null;
Statement statement = null;
Class.forName(JDBC_DRIVER);
System.out.println("Class.forName(driver) accepted....");
conn = DriverManager.getConnection(DB_URL,UserName,UserPass);
System.out.println("Connection Made....");
statement = conn.createStatement();
System.out.println("Statement object created from conn....");
//String sql = "INSERT INTO CUSTOMER " +"VALUES('Todd','Packer','724-345-9876','134 west end road','PA','Pittsburgh','12356','M','12/21/1995','married');";
String sql2 = "INSERT INTO CUSTOMER " + "VALUES("+ FirstNameTextField.getText() + " , " + LastNameTextField.getText() + ", " + PhoneNumTextField.getText() + ", " + StreetAddressTextField.getText() + ", " + StateTextField.getText() + ", " + CityTextField.getText() + ", " + ZipTextField.getText() + "," + GenderTextField.getText() + ", " + DOBTextField.getText() + ", " + MartialStatusTextField.getText() + ");";
statement.executeUpdate(sql2);
System.out.println("Insert successful to database....");
if(statement != null)
{
conn.close();
System.out.println("Connection closed to database....");
}
}
catch(Exception p)
{
//catch any exceptions
System.out.println("Exception thrown: " + p.getMessage());
}
}
//Main
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
CreateAndShowContent();
}
});
}
}