1

I'm trying to connect a java program I'm making about a university database that I created in MS Access.

My code goes like this:

    package Uni;
    import java.util.*;
    import java.sql.*;

    public class College {
        static final String database_URL = "H:/CSCI 213/Labs/Lab 11 - 12/src/Uni/Uni.accdb";
        static Scanner input = new Scanner(System.in);
        static Scanner in = new Scanner(System.in);

        @SuppressWarnings("unused")
        public static void main(String[] args) {
            Connection connection = null;
            Statement statement = null;
            ResultSet resultset = null;
            String name;
            int age;
            int id;
            String choice = "y";

            while(choice.equalsIgnoreCase("y")) {
        System.out.println("What Would you like to do");
        System.out.println("1- Insert");
        System.out.println("2- Update");
        System.out.println("3- Search");
        System.out.println("4- Delete");
        System.out.println("5- Exit");

        int x = input.nextInt();
        try {
                    connection = DriverManager.getConnection(database_URL);
                    //connection.commit();
                    //statement = connection.createStatement();
                    switch(x) {
                      case 1:   
                        System.out.println("Enter the name:");
                        name = input.nextLine();
                        System.out.println("Enter the Age:");
                        age = input.nextInt();
                        //statement.executeQuery("INSERT INTO Database VALUES ('"+name+"','"+age+"')");
                        Statement sta1 = connection.createStatement(); 
                        sta1.executeQuery("INSERT INTO Database VALUES ('"+name+"','"+age+"')");
                        sta1.close();
                        System.out.println("Your ID is: ");
                        //statement.executeQuery("select ID from Database where Name='"+name+"';");
                        Statement sta2 = connection.createStatement();
                        sta2.executeQuery("select ID from Database where Name='"+name+"';");
                        connection.commit();
                        sta2.close();
                        break;

                        case 2:
                        System.out.println("Enter the id of the person you'd like to update:");
                        id = input.nextInt();
                        System.out.println("Enter the name:");
                        name = input.nextLine();
                        System.out.println("Enter the Age:");
                        age = input.nextInt();
                        //statement.executeUpdate("UPDATE Database SET Age="+age+", Name='"+name+"' WHERE ID="+id+";");
                        Statement sta3 = connection.createStatement();
                        sta3.executeQuery("UPDATE Database SET Age="+age+", Name='"+name+"' WHERE ID="+id+";");
                        connection.commit();
                        sta3.close();
                        break;

                    case 3:
                        System.out.println("Enter the Name of the person you'd like to search for:");
                        name = input.nextLine();
                        //statement.executeQuery("select Name, Age from Database where Name='"+name+"';");
                        Statement sta4 = connection.createStatement();
                        sta4.executeQuery("select Name, Age from Database where Name='"+name+"';");
                        connection.commit();
                        sta4.close();
                        break;

                    case 4:
                        System.out.println("Enter the id of the person you'd like to delete:");
                        id = input.nextInt();
                        //statement.execute("DELETE FROM Database where ID='"+id+"';");
                        Statement sta5 = connection.createStatement();
                        sta5.executeQuery("DELETE FROM Database where ID='"+id+"';");
                        connection.commit();
                        sta5.close();
                        break;

                    case 5:
                        System.out.println("Terminating");
                        System.exit(0);

                    default:
                        System.out.println("Wrong Entry");
                    }

                }

           catch (SQLException e)  {
               System.out.println("SQLException: "+e.getMessage());
           }


           System.out.println("Would you like to try again ? (enter y for yes)");
           choice = in.nextLine();
           }
     }
   }

now the exact output is as follows

What Would you like to do 1- Insert 2- Update 3- Search 4- Delete 5- Exit 1 SQLException: No suitable driver found for H:/CSCI 213/Labs/Lab 11 - 12/src/Uni/Uni.accdb Would you like to try again ? (enter y for yes)

any ideas on how to solve this is much appreciated

Cory Kendall
  • 7,195
  • 8
  • 37
  • 64
Shahoud
  • 15
  • 3

1 Answers1

0

H:/CSCI 213/Labs/Lab 11 - 12/src/Uni/Uni.accdb is a path to a file, and not a valid JDBC URL.

You need to get a JDBC driver JAR file for your particular database, put it in the classpath when you run your program, and use a correct JDBC URL. What the JDBC URL exactly should look like depends on which database and driver you're using (look in the documentation).

For using a Microsoft Access database via JDBC, see for example: How to connect Java to MS Access or search for "Microsoft Access JDBC driver".

See Oracle's JDBC Getting Started tutorial for more details.

Community
  • 1
  • 1
Jesper
  • 202,709
  • 46
  • 318
  • 350