0

I'm trying to hook up mysql driver in Eclipse IDE but keep getting error message, "Driver not found" here is my code. I went though and added the driver under the ClassPath but it didn't work. I have all the imports I think I need but the driver still isn't being found. Database.java file

package model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Database {
    private List<Person> people;

    public Database() {
        people = new LinkedList<Person>();
    }
    
    public void connect() throws Exception {
        
        final String driver = "com.mysql.jbdc.Driver";
        
        try {
            Class.forName(driver);
            System.out.println("Driver loaded!");
        } catch (ClassNotFoundException e) {
            throw new Exception("Driver not found");
        }
        
        String url = "jbdc:mysql://localhost:3306/swingtest";
        Connection con = DriverManager.getConnection(url, "root", "root");
    }
    public void disconnect() {
        
    }

    public void addPerson(Person person) {
        people.add(person);
    }
    
    public void removePerson(int index) {
        people.remove(index);
    }

    public List<Person> getPeople() {
        return Collections.unmodifiableList(people);
    }

    public void saveToFile(File file) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        Person[] persons = people.toArray(new Person[people.size()]);

        oos.writeObject(persons);

        oos.close();
    }

    public void loadFromFile(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        
        try {
            Person[] persons = (Person[])ois.readObject();
            
            people.clear();
            
            people.addAll(Arrays.asList(persons));
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        ois.close();
    }
}

TestDatabase.java file

import model.Database;

public class TestDatabase {

    public static void main(String[] args) {
        System.out.println("Running database test");
        
        Database db = new Database();
        
        try {
            db.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.disconnect();
    }
} 

Below are pics from my setup. Please help. Thanks! Referenced Jar file

Put under ClassPath

swingtest on workbench running on port 3306

lethalSid
  • 51
  • 3
  • When you create an exception when catching another exception, always include the original exception as the cause, i.e. `throw new Exception("Driver not found", e)` – tgdavies Jul 05 '20 at 06:33
  • Does this answer your question? [java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver](https://stackoverflow.com/questions/5616898/java-sql-sqlexception-no-suitable-driver-found-for-jdbcmicrosoftsqlserver) – Mr. Jain Jul 05 '20 at 06:54
  • Is your code compiling? If it is, then _Build Path_ is not your problem. Provide a screen capture of your runtime configuration. Note the _Show Command_ button in the _Run Configuration_ window. You should also post the actual command line in your question. – Abra Jul 05 '20 at 07:21

0 Answers0