Can't figure out whats wrong with my code


#1

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() {
    initComponents();
}

/**
 * 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.
 */
@SuppressWarnings("unchecked")
// <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();

    jLabel1.setText("Username");

    jLabel2.setText("Password");

    button_submit.setText("Submit");
    button_submit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            button_submitActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(44, 44, 44)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1))
                    .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)
                        .addComponent(txt_username)))
                .addGroup(layout.createSequentialGroup()
                    .addGap(145, 145, 145)
                    .addComponent(button_submit)))
            .addContainerGap(75, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(51, 51, 51)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(txt_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(57, 57, 57)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .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)
            .addComponent(button_submit)
            .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;
   
   try(
           Class.forName("com.mysql.jdbc.Driver");
           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();
           if(i>0){
           JOptionPane.showMessageDialog(null,"Data is Saved");
           }
           else{
           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

}

`


#2

From the DB URL, it seems like you are trying to connect to a MySQL DB driver. Check the link for more details - https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html


#3

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.


#4

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.


#5

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 {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
    System.out.println("Where is your MySQL JDBC Driver?");
    e.printStackTrace();
    return;
}

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");
    e.printStackTrace();
    return;
}

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

}

}


#6

I used this video as reference if it helps. https://www.youtube.com/watch?v=q8Z3CmruGzI


#7

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 http://dev.mysql.com/downloads/connector/j/

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 org.gjt.mm.mysql.Driver. If found, you code should work.