Can't figure out whats wrong with my code


Hi i can't seem to figure out whats wrong with my code. Wondering if anyone else could help. I just have 1 error on the line that says " Class.forName("com.mysql.jdbc.Driver");" Its got me stumped.

package finalproject1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;

public class UserInfo extends javax.swing.JPanel {

public UserInfo() {

 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    txt_username = new javax.swing.JTextField();
    txt_password = new javax.swing.JPasswordField();
    button_submit = new javax.swing.JButton();



    button_submit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
                    .addGap(44, 44, 44)
                    .addGap(69, 69, 69)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txt_password, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                    .addGap(145, 145, 145)
            .addContainerGap(75, Short.MAX_VALUE))
            .addGap(51, 51, 51)
                .addComponent(txt_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(57, 57, 57)
                .addComponent(txt_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
            .addGap(43, 43, 43))
}// </editor-fold>                        

private void button_submitActionPerformed(java.awt.event.ActionEvent evt) {                                              
   String username= txt_username.getText ();
   String password= String.valueOf(txt_password.getPassword ());
   Connection conn=null;
   PreparedStatement pstmt=null;
           conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/FinalProject","root", "arsenal1996");
           pstmt=conn.prepareStatement("insert into userdetail values(?,?)");
           pstmt.setString(1, username);
           pstmt.setString(2, password);
           int i=pstmt.executeUpdate();
           JOptionPane.showMessageDialog(null,"Data is Saved");
           JOptionPane.showMessageDialog(null,"Data is not saved");
   catch (Exception e){
   JOptionPane.showMessageDialog(null, e);

// Variables declaration - do not modify                     
private javax.swing.JButton button_submit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPasswordField txt_password;
private javax.swing.JTextField txt_username;
// End of variables declaration




From the DB URL, it seems like you are trying to connect to a MySQL DB driver. Check the link for more details -


Well i technically taught that i connected with the line below the one i'm struggling with. I'm just confused as to what Class.forName( ); does and why its giving me an error.


I hope you are running the above java program on your machine and has the JDBC driver for MySQL available on the CLASSPATH. Class.forName is used to register the JDBC driver for the DB in memory so that all database connections use it connect to the database. Most of the database connection class in the java.sql package are abstract implementations and the actual database connection objects are loaded during runtime.


This is my other program that i made yesterday and it does not give me an error on the same line. Did everything the same.

package pkgfinal;
import java.sql.*;
public class Final {

public static void main(String[] args) {
    try {
} catch (ClassNotFoundException e) {
    System.out.println("Where is your MySQL JDBC Driver?");

System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;

try {
    connection = DriverManager
    .getConnection("jdbc:mysql://localhost:3306/FinalProject","root", "arsenal1996");

} catch (SQLException e) {
    System.out.println("Connection Failed! Check output console");

if (connection != null) {
    System.out.println("You made it, take control your database now!");
} else {
    System.out.println("Failed to make connection!");




I used this video as reference if it helps.


There are 4 types of JDBC drivers and from the video it seems like it is Type2. Can you verify if you have the connector jar file? You can download from

If you are using a java IDE, add the jar file to the libraries folder in the java project. Once it is available in the libraries, expand the jar file and check if you have the package com.mysql.jdbc and then check if there is a Driver class. If not search for If found, you code should work.