0

I have this problem where I tried to connect to mySQL driver by using this code below.I set the code that if I failed to connect to mySQL Driver, a popup will appear and say mySQL Driver cannot be reached and it does appear. I have imported mysql-connector-java-8.0.29.jar in `Referenced Libraries.

package belajarjavaGUI;


    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.Connection;
    import java.sql.Statement;


    public class trial extends JFrame implements ActionListener {
                
                Connection con;
                Statement stmt;
                JLabel          lbluser, lblpass;
                JTextField      txtuser;
                JPasswordField  txtpass;
                JButton         btnLogin;
                
                public trial() 
                {
                    settingFrame();
                    settingInterface();
                }
                
                void settingFrame()
                {
                    setLayout(null);
                    setSize(280,200);
                    setTitle("Unit Latihan Industri");
                    setVisible(true);
                    setLocationRelativeTo(null);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                     
                }

                void settingInterface()
                {
                    
                    btnLogin = new JButton("Login");
                    btnLogin.setBounds(70, 100, 100, 50);
                    add(btnLogin);
                    btnLogin.addActionListener(this);
                }
                
                public static void main(String[] args) 
                {
                    new trial();

                }

                public void actionPerformed(ActionEvent e) 
                {
                    if(e.getSource() == btnLogin) {
                
                        //1.load driver
                        try {
                            Class.forName("com.mysql.jdbc.Driver");
                            
                        } catch (ClassNotFoundException e1) {
                            
                            JOptionPane.showMessageDialog(null,"mySQL Driver cannot be reached");
                        }
                    }
                }
    }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Peragut
  • 1
  • 2
  • 1
    *Class.forName("com.mysql.jbdc.Driver");* Typo: should be `Class.forName("com.mysql.jdbc.Driver");` but you should probably be using [DriverManager](https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection-drivermanager) – g00se May 05 '22 at 07:35
  • @g00se I tried that. It still doesn't work. It feels like something is blocking the connection but don't know what that is or what kind of setting that is blocking it. – Peragut May 05 '22 at 09:23
  • Please edit your code alteration in your question. Also, place `System.out.println(System.getProperty("java.class.path"));` as the first line of `main` and tell us what it prints – g00se May 05 '22 at 09:58
  • It says _"mySQL Driver cannot be reached"_, because that is what your code prints when an exception occurs. It is better to print or log the actual exception stacktrace, so you know the actual cause. – Mark Rotteveel May 05 '22 at 11:25

1 Answers1

0

Try with Class.forName("com.mysql.cj.jdbc.Driver");

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Eric Ked
  • 1
  • 1