Hi im trying to connect to my database but i dont know why i cant load my jdbc driver:

-I've saved the jar file together with the other java files in the same folder called "a"
-I used –classpath flag:
Windows: "java -classpath .\mysql-connector-java-3.1.8-bin.jar;. LabDB4"

but I dont know if this is right.

import java.sql.*;

public class a{
	public static void main(String[] args) {

		String sDriver = "com.mysql.jdbc.Driver";
		Connection con = null;
		Statement stmt = null;
		
		String sURL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz/<DATABASE_NAME>";
		String sUsername = "<ID>"; // my ID
		String sPassword = "<password>"; // my password
		
		try { // Attempt to load the JDBC driver with newInstance
		
			Class.forName(sDriver).newInstance();
			
		}
		catch( Exception e ) {
		
			System.err.println("Failed to load current driver. ");
			return;
			
		} // end catch
		
		try { //Connect to database
		
			con = DriverManager.getConnection (sURL, sUsername, sPassword);
			stmt = con.createStatement();
		}
		catch ( Exception e) {
			System.err.println("Problems connecting to " + sURL + ":" );
			System.err.println( e.getMessage() );
			
			if( con != null) {
			try { con.close(); }
			catch( Exception e2 ) {}
			}
			return;
		} // end catch
		
		// my code for creating tables and performing queries will go here
		
		// Disconnect
		try { stmt.close(); }
		catch( Exception e ) {}
		
		try { con.close(); }
		catch( Exception e ) {}
	}
}

when i compile and run it it comes up with the error "Failed to load current driver" (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver)

Please help when you can any help will be very much appreciated

Thanks

the above code line #
String sURL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz/<DATABASE_NAME>";
is quite complicated to understand

you can take the help from the following code i hope this may help you.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.io.*;

public class Login extends JFrame implements ActionListener
 {
   JTextField txtname;
   JLabel lblname,lblpass1,lblpass2;
   JButton btnok,btncancel;
   JPasswordField txtpass1,txtpass2;
   Container cp=getContentPane();
   PreparedStatement stmt;
   Connection conn;
   
 Login()
   {
     setLayout(null);
     setSize(400,400);
     setTitle("LOGIN");
     
     lblname = new JLabel("Username");
     lblname.setSize(100,50);
     lblname.setLocation(20,50);
    
      cp.add(lblname);
     
     txtname = new JTextField(15);
     txtname.setSize(150,40);
     txtname.setLocation(130,50);
    
     cp.add(txtname);

     lblpass1 = new JLabel("Password");
     lblpass1.setSize(100,50);
     lblpass1.setLocation(20,100);
  
      cp.add(lblpass1);
    
     txtpass1 = new JPasswordField(10);
     txtpass1.setSize(150,40);
     txtpass1.setLocation(130,100);
    
       cp.add(txtpass1);
   
     lblpass2 = new JLabel("Re-typePassword");
     lblpass2.setSize(120,50);
     lblpass2.setLocation(20,150);
    
      cp.add(lblpass2);
    
     txtpass2 = new JPasswordField(10);
     txtpass2.setSize(150,40);
     txtpass2.setLocation(130,150);
  
      cp.add(txtpass2);
   
     btnok = new JButton("Submit");
     btnok.setSize(80,40); 
     btnok.setLocation(20,220);
     btnok.addActionListener(this);
      cp.add(btnok);
     
     btncancel = new JButton("Cancel");
     btncancel.setSize(80,40); 
     btncancel.setLocation(130,220);
     btncancel.addActionListener(this);     
     cp.add(btncancel); 
    
      try
      {
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      
       conn=DriverManager.getConnection("Jdbc:Odbc:Login");
       
       }
     catch(Exception e)
     {

         System.err.println("Failed to load current driver. ");
         return;
      }
 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
 }
public void actionPerformed(ActionEvent ae)
 {
  String s,s1,s2;
  s=txtname.getText();
  s1=txtpass1.getText();
  s2=txtpass2.getText();
  
if(ae.getSource()==btnok)
    {
    try{
      if(s1.equals(s2))
         {
      		JOptionPane.showMessageDialog(null,"Correct Password");
      		stmt=conn.prepareStatement("insert into Info(username,password) values (?,?)");    
      		stmt.setString(1,s);
      		stmt.setString(2,s1);
      		stmt.executeUpdate();
      		JOptionPane.showMessageDialog(null,"Record Added");
      		txtname.setText("");
      		txtpass1.setText(""); 
      		txtpass2.setText("");
          }   
        else
         {
               JOptionPane.showMessageDialog(null,"Please check your password or username");
               txtname.setText("");
               txtpass1.setText("");
               txtpass2.setText("");
       }
    try
     {
      
      }
   catch(Exception e)
   {}

    }catch(Exception e){}
    }
if(ae.getSource()==btncancel)
    {
  
    JOptionPane.showMessageDialog(null,"System Exited");
    System.exit(0);
    }
}
public static void main(String args[])
 {
  new Login();
 }
}

First thing while writing a code is that your code should be neat and clean to understand.ok

after you are done with your code make a jar file of your code and your database should be ion the same folder where you have saved your java file. this one is the simple way to connect through database

@DeadSoul
1. We want to people learn and not just copy and paste solutions, so next time please provide just basic guidance points and not whole solution.
2. Question was about "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" not about ODBC
3. Was there any request for GUI?

@tufzz good start not many people actually use command line and giving connector location like you did, they most often stick driver where ever they see on internet from wrong people.
As for connection string String sURL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz/<DATABASE_NAME>"; can you elaborate what you doing? If you trying to connect to external database you need have your server configurated properly as most MySQL instalation run under "localhost".

Anyway command line compile from location of your java file keeping in mind you have JAR in same location

javac -classpath .;./mysql-connector-java-5.1.12-bin.jar DatabaseDriver.java

Command line execute

java -classpath .;./mysql-connector-java-5.1.12-bin.jar DatabaseDriver

Have fun ;)

@tufzz good start not many people actually use command line and giving connector location like you did, they most often stick driver where ever they see on internet from wrong people.
As for connection string String sURL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz/<DATABASE_NAME>"; can you elaborate what you doing?

Im trying to connect to a database (access from the web-based interface)

Everyone else can but i cant i think im missing something but dont know what it is

If you trying to connect to external database you need have your server configurated properly as most MySQL instalation run under "localhost".

Anyway command line compile from location of your java file keeping in mind you have JAR in same location

javac -classpath .;./mysql-connector-java-5.1.12-bin.jar DatabaseDriver.java

Command line execute

java -classpath .;./mysql-connector-java-5.1.12-bin.jar DatabaseDriver

Have fun ;)

I already did this and it gave me an error:

Exception in thread "main" java.lang.NoClassDefFoundError: a
Caused by: java.lang.ClassNotFoundException: Banking
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        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)
Could not find the main class: a. Program will exit.

end quote.

Thanks for the help anyway

Regards

However now you are getting error because your class "a" (You should really name it something meaningful) cannot find class "Banking"

Oh yea sorry i renamed the files my bad but my error is fixed.

It was about the -classpath i didnt put ".;." after the -classpath option then the directory. I thought it was -classpath .\dir

Anyway thank you so much for the help.

Them please mark thread as solved. Below last thread there is hyperlink "Mark this Thread as Solved"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.