Java驱动程序?

问题描述 投票:0回答:3

我对Java类和驱动程序真的很困惑。驱动程序代码的语法是什么,里面应该有什么,不应该有什么?对于驱动程序,我们可能需要在其中包含一个主程序,但是 Java 类呢,我们不需要吗?

我需要在 Java 类和驱动程序中都有这个,还是只在驱动程序中:

public static void main(String[] args) {

}
java driver
3个回答
7
投票

从这里引用:

什么是司机班? (爪哇语)

“驱动程序类”通常只是包含主类的类。在一个 实际项目中,你可能经常有无数的“驱动类”用于测试 等等,或者你可以在你的任何对象中构建一个 main 和 通过您的 IDE 选择可运行的类,或者通过简单地指定 “Java 类名。”

例子:

这不是驱动程序类,因为它不包含任何主要方法。在这种情况下,它有方法“你好”:

public class HelloWorld {
  public void hello() {
    System.out.println("Hello, world!");
  }
}

相对于这个 - 这是一个驱动程序类,因为它包含一个主要方法,runsHelloWorld的类:

public class HelloWorldDriver {
  public static void main(String[] args) {
    HelloWorld sayhello = new HelloWorld();
    sayhello.hello();
  }
}

因此得名“驱动程序类”——作为 HelloWorldDriver 类“drives”或者更确切地说,controls HelloWorld 类的实例化和使用。


-1
投票

你应该阅读 https://docs.oracle.com/javase/6/docs/api/java/sql/Driver.html 这定义了一个 JDBC 驱动程序。如果你想创建你的 JDBC 驱动程序,只需创建一个实现此接口的类(这比它看起来更复杂,因为必须定义一个特定的

Connexion
类......),但你明白了。

在任何情况下,当您需要使用或实现驱动程序时,您应该有一个接口参考(音频、JDBC、图形、串行接口、XML 解析器、脚本语言……),它定义了您的驱动程序提供哪些方法。

您可以定义自己的驱动方案。然后你定义一个接口,一个或多个实现。通常,驱动程序是动态加载的。那么

Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/DB?user=root&password=myPassword");

将加载 MariaDB 驱动程序。

如果驱动程序类名来自配置文件(这使您的程序可移植),

my_class = driver_name.getClass()
将动态地为您提供类。然后
(MyDriverInterface)my_class.newInstance()
将创建一个您只需要使用的驱动程序实例。


-1
投票
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
    private static ArrayList<Dog> dogList = new ArrayList<Dog>();
    // Instance variables (if needed)
    private static ArrayList<Monkey> monkeyList = new ArrayList<Monkey>();
    public static void main(String[] args) {
        String userInput;

        initializeDogList();
        initializeMonkeyList();
        displayMenu();
        Scanner scanner = new Scanner(System.in);
        userInput = scanner.next().toUpperCase();
        while (userInput != "Q") {
            if ((userInput != "1") && (userInput != "2") && (userInput != "3") 
                    && (userInput != "4") && (userInput != "5") && (userInput != "6")) {
                System.out.println("Invalid Entry");
                displayMenu();
                userInput = scanner.next().toUpperCase();
            }
            else if (userInput == "1") {
                intakeNewDog(scanner);
                break;
            }
            else if (userInput == "2") {
                intakeNewMonkey(scanner);
                break;
            }
            else if (userInput == "3") {
                reserveAnimal(scanner);
                break;
            }
            else if (userInput == "4") {
                int number = Integer.parseInt(userInput);
                printAnimals(number);
                break;
            }
            else if (userInput == "5") {
                int number = Integer.parseInt(userInput);
                printAnimals(number);
                break;
            }
            else if (userInput == "6") {
                int number = Integer.parseInt(userInput);
                printAnimals(number);
                break;
            }
        }
    }
    // This method prints the menu options
    public static void displayMenu() {
        System.out.println("\n\n");
        System.out.println("\t\t\t\tRescue Animal System Menu");
        System.out.println("[1] Intake a new dog");
        System.out.println("[2] Intake a new monkey");
        System.out.println("[3] Reserve an animal");
        System.out.println("[4] Print a list of all dogs");
        System.out.println("[5] Print a list of all monkeys");
        System.out.println("[6] Print a list of all animals that are not reserved");
        System.out.println("[q] Quit application");
        System.out.println();
        System.out.println("Enter a menu selection");
    }
    // Adds dogs to a list for testing
    public static void initializeDogList() {
        Dog dog1 = new Dog("Moca", "German Shepherd", "male", "1", "25.6", "05-12-2019", "United States", "intake", false, "United States");
        Dog dog2 = new Dog("Smokey", "Great Dane", "male", "3", "35.2", "02-03-2020", "United States", "Phase I", false, "United States");
        Dog dog3 = new Dog("Pickles", "Chihuahua", "female", "4", "25.6", "12-12-2019", "Canada", "in service", true, "Canada");

        dogList.add(dog1);
        dogList.add(dog2);
        dogList.add(dog3);
    }

    // Adds monkeys to a list for testing
    //Optional for testing
    public static void initializeMonkeyList() {
        Monkey monkey1 = new Monkey("Chuckles", "Mandorin", "Female", "2", "200", "8'3", "2'", "Normal", "4/10/2023", "South America", "trained", "reserved", "service");
        Monkey monkey2 = new Monkey("Nitro", "Chimpanzee", "Male", "3", "400", "7", "10'", "Muscular", "4/10/2023", "United States", "Rescue", "reserved", "service Bouncer");
        Monkey monkey3 = new Monkey("Flower", "Mandorin", "Female", "2", "50", "2", "6'","Normal", "4/10/2023", "Turkey", "trained", "Rescue", "service");
        
        monkeyList.add(monkey1);
        monkeyList.add(monkey2);
        monkeyList.add(monkey3);

    }
    public static void intakeNewDog(Scanner scanner) {
        System.out.println("What is the dog's name?");
        String name = scanner.nextLine();
        for(Dog dog: dogList) {
            if(dog.getName().equalsIgnoreCase(name)) {
                System.out.println("\n\nThis dog is already in our system\n\n");
                return; //returns to menu
            }
        }
        System.out.println("What is the dog's breed?");
        String breed = scanner.nextLine();
        System.out.println("What is the dog's gender?");
        String gender = scanner.nextLine();
        System.out.println("What is the dog's age?");
        String age = scanner.nextLine();
        System.out.println("What is the dog's weight?");
        String weight = scanner.nextLine();
        System.out.println("What is the dog's acquisition date?");
        String acquisitionDate = scanner.nextLine();
        System.out.println("What is the dog's acquisition location?");
        String acquisitionCountry = scanner.nextLine();
        System.out.println("What is the dog's training status?");
        String trainingStatus = scanner.nextLine();
        System.out.println("Is the dog reserved?");
        boolean reserved = scanner.nextBoolean();
        System.out.println("What is the dog's service country?");
        String inServiceCountry = scanner.nextLine();
        
        Dog newDog = new Dog(name, breed, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reserved, inServiceCountry);
        dogList.add(newDog);
        System.out.println("Dog added to list");

    }
    public static void intakeNewMonkey(Scanner scanner) {
        System.out.println("What is the monkey's name?");
        String name = scanner.nextLine();
        for(Monkey monkey: monkeyList) {
            if(monkey.getName().equalsIgnoreCase(name)) {
                System.out.println("\n\nThis monkey is already in our system\n\n");
                return; //returns to menu
            }
        }
        System.out.println("What is the monkey's species?");
        String species = scanner.nextLine();
        System.out.println("What is the monkey's gender?");
        String gender = scanner.nextLine();
        System.out.println("What is the monkey's age?");
        String age = scanner.nextLine();
        System.out.println("What is the monkey's weight?");
        String weight = scanner.nextLine();
        System.out.println("What is the monkey's height?");
        String height = scanner.nextLine();
        System.out.println("What is the monkey's tail length?");
        String tail = scanner.nextLine();
        System.out.println("What is the monkey's body length?");
        String body = scanner.nextLine();
        System.out.println("What is the monkey's acquisition date?");
        String date = scanner.nextLine();
        System.out.println("What is the monkey's acquisition location?");
        String country = scanner.nextLine();
        System.out.println("What is the monkey's training status?");
        String training = scanner.nextLine();
        System.out.println("Is the monkey reserved?");
        boolean reserved = scanner.nextBoolean();
        System.out.println("What is the monkey's service country?");
        String service = scanner.nextLine();
        
        Monkey newMonkey = new Monkey(name, species, gender, age, weight, height, tail, body, date, country, training, reserved, service);
        monkeyList.add(newMonkey);
        System.out.println("Monkey added to list");
    }
    public static void reserveAnimal(Scanner scanner) {
        scanner.nextLine();
        System.out.println("Enter animal type: ");
        String animalType = scanner.nextLine();
        if (animalType.equalsIgnoreCase("Monkey")) {
            System.out.println("Enter the monkey's country of acquisiton: ");
            String country = scanner.nextLine();
            for(Monkey obj: monkeyList) {
                if(obj.getAcquisitionLocation().equalsIgnoreCase(country)) {
                    obj.setReserved(true);
                    System.out.println("This monkey is now reserved.");
                    return;
                }
            }
            System.out.println("The monkey entered is not in the list");
        }
        else if (animalType.equalsIgnoreCase("Dog")) {
            System.out.println("Enter the dog's country of acquisition: ");
            String country = scanner.nextLine();
            for(Dog obj: dogList){
                if(obj.getAcquisitionLocation().equalsIgnoreCase(country)){
                    obj.setReserved(true);
                    System.out.println("This dog is now reserved.");
                    return;
                }
            }
            System.out.println("The dog entered is not in the list");
        } else {
            System.out.println("Type not found");
        }
    }
    public static void printAnimals(int option) {
        if (option == '4') {
            System.out.println(dogList);
        }
        else if (option == '5') {
            System.out.println(monkeyList);
        }
        else if (option == '6') {
            // Iterates through dogs
            for (int i = 0; i < dogList.size(); i++) {
                if (dogList.get(i).getTrainingStatus().equals("in service") && 
        dogList.get(i).getReserved()==false) {
                    // Prints dogs that are in service and available
                    System.out.println(dogList.get(i));
                }
            }
            // Iterates through monkeys
            for (int i = 0; i < monkeyList.size(); i++) {
                if (monkeyList.get(i).getTrainingStatus().equalsIgnoreCase("in service") && 
            monkeyList.get(i).getReserved()==false) {
                    // Prints monkeys that are in service and available
                    System.out.println(monkeyList.get(i));
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.