MySQL雇员数据库java.sql.SQLException:网址不能为null

时间:2018-06-19 14:41:02

标签: java mysql

我正在用Java创建一个雇员数据库,但不断收到“ URL不能为null”错误。这是我为主班的代码:

import java.util.Scanner;
import java.sql.SQLException;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
        Scanner in = new Scanner(System.in);
        boolean quit = false;
        String input = "";
    //  SimpleDataSource.init(args[0]);
    //  Database.createDB();

        while (quit == false) {
            System.out.println("Choose an Option: A)dd, S)earch, Q)uit");
            input = in.next();
            if (input.equalsIgnoreCase("a")) {
                System.out.println("Choose an Option: E)mployee");
                input=in.next();
                if (input.equalsIgnoreCase("e")) {
                    System.out.println("Enter new Employee ID: ");
                        int EmpID = in.nextInt();
                    System.out.println("Enter First Name: ");
                    String fName = in.next();
                    System.out.println("Enter Last Name: ");
                        String lName = in.next();
                    System.out.println("Enter Skill: ");
                        String skill1 = in.next();
                    System.out.println("Enter Department ID: ");
                        String deptID = in.next();
                    System.out.println("Enter Skill ID: ");
                        String skillID = in.next();
                    System.out.println("Enter Employee Email: ");
                        String empEmail = in.next();
                    System.out.println("Enter Employee Phone Number: ");
                        String empPhone = in.next();
                    System.out.println("Enter Department Title: ");
                        String depTitle = in.next();
                    System.out.println("Enter Supervisor's ID: ");
                        String supID = in.next();
                    System.out.println("Enter Supervisor's Last Name: ");
                        String supLName = in.next();
                    System.out.println("Enter Supervisor's First Name: ");
                        String supFName = in.next();
                    Database.addEmployee(EmpID,fName, lName, skill1, deptID, skillID, empEmail, empPhone, depTitle, supID, supLName, supFName);

            } 
            }else if (input.equalsIgnoreCase("s")) {
                System.out.println("Choose an Option: E)mployee, S)kill, E)mail");
                input=in.next();
                if (input.equalsIgnoreCase("e")) {
                    System.out.println("Enter Employee ID: ");
                    int id = in.nextInt();
                    Database.findEmployee(id);
                } else if (input.equalsIgnoreCase("s")) {
                    System.out.println("Enter Skill: ");
                    String empSkill1= null;
                    Database.findSkill(empSkill1);
                }
            } else if (input.equalsIgnoreCase("e")) {
                System.out.println("Enter Employee Email: ");
                String empEmail=in.next();
                Database.findEmail(empEmail);


            } else if (input.equalsIgnoreCase("q")) {
                quit = true;
            }
        }
        in.close();

}

}

这是创建数据库的类:

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Database {

static int EmployeeID = 000000001;

/**
 * Creates Database
 * @throws SQLException
 */
public static void createDB() throws SQLException {
    try (Connection conn = SimpleDataSource.getConnection()) {
        Statement stat = conn.createStatement();
        stat.execute("CREATE TABLE Employees (EmployeeID VARCHAR(9), EmployeeFName VARCHAR(15), EmployeeLName VARCHAR(15), Skill1 VARCHAR(30), DepartmentID CHAR(2), SkillID CHAR(4), Email VARCHAR(40), Phone CHAR(10)");
        stat.execute("INSERT INTO Employees VALUES (000000001, 'Bob', 'Ross', ???, ???, ???)");
        stat.execute("INSERT INTO Employees VALUES (000000002, 'Chuck', 'Brown', ???, ???, ???)");
        stat.execute("INSERT INTO Employees VALUES (000000003, 'Charles', 'Fischer', ???, ???, ???)");
        conn.close();
    }
}

/**
 * Adds a customer to database using given information
 * @param cFname First Name
 * @param cLname Last name
 * @param Skill1 Skill 1
 * @param Skill2 Skill 2
 * @param Skill3 Skill 3
 * @throws SQLException
 */
public static void addEmployee(int EmpID, String EmpFname, String EmpLname, String sk1, String deptID, String skillID, String email, String phoneNum, String depTitle, String supID, String supLName, String supFName) throws SQLException {
    try (Connection conn = SimpleDataSource.getConnection()) {
        Statement stat = conn.createStatement();
        stat.execute("INSERT INTO Customers VALUES (" + EmployeeID + ", '" + EmpFname + "', '" + EmpLname + "', '"
                + sk1 + ","+deptID+","+skillID+","+email+","+phoneNum+","+depTitle+","+supID+","+supLName+","+supFName+",");
        conn.close();
    }
}

/**
 * Increases loyalty value of given customer
 * @param CustID Customer ID
 * @throws SQLException
 */


/**
 * Adds Employee to database using given info
 * @param itemName Item Name
 * @param price Item Price
 * @throws SQLException
 */


/**
 * Finds Employee information using given ID
 * @param EmployeeID Employee ID
 * @throws SQLException
 */
public static void findEmployee(int EmpID) throws SQLException {
    try (Connection conn = SimpleDataSource.getConnection()) {
        Statement stat = conn.createStatement();
        String query = "SELECT * FROM Employee WHERE EmployeeID = " + EmployeeID;
        ResultSet result = stat.executeQuery(query);

        while (result.next()) {
            for (int i = 1; i <= 9; i++) {
                System.out.print(result.getString(i) + " ");
            }
            System.out.println();
        }
        conn.close();
    }
}

/**
 * Finds Skill information using given Skill
 * @param Skill1 Skill 1
 * @param Skill2 Skill 2
 * @param Skill3 Skill 3
 * @throws SQLException
 */
public static void findSkill(String sk1) throws SQLException {
    try (Connection conn = SimpleDataSource.getConnection()) {
        Statement stat = conn.createStatement();
        String query = "SELECT * FROM Employees WHERE Skill1 ='"+sk1+"";
        ResultSet result = stat.executeQuery(query);

        while (result.next()) {
            for (int i = 1; i <= 4; i++) {
                System.out.print(result.getString(i) + " ");
            }
            System.out.println();
        }
        conn.close();
    }
}
public static void findEmail(String email) throws SQLException {
    try (Connection conn = SimpleDataSource.getConnection()) {
        Statement stat = conn.createStatement();
        String query = "SELECT * FROM Employee WHERE Email = " + email;
        ResultSet result = stat.executeQuery(query);

        while (result.next()) {
            for (int i = 1; i <= 9; i++) {
                System.out.print(result.getString(i) + " ");
            }
            System.out.println();
        }
        conn.close();
    }
}
}

这是我的联系班:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;


public class SimpleDataSource {

    private static String url;
    private static String username;
    private static String password;

public static void init(String fileName) throws IOException, ClassNotFoundException

{
    Properties props= new Properties();
    FileInputStream in = new FileInputStream(fileName);
    //fileName= "database.properties.properties";
    props.load(in);

    String driver = props.getProperty("jdbc.driver");
    url = props.getProperty("jdbc.url");
    username = props.getProperty("jdbc.username");
    if(username == "admin") {username = "";}
    password = props.getProperty("jdbc.password");
    if (password == "secret" ) {password = "";}
    if (driver != null) {Class.forName(driver);}

}


public static Connection getConnection() throws SQLException
{
    return DriverManager.getConnection(url, username, password);
}

}

这是我的jdbc database.properties文件:

jdbc.url=jdbc:derby:BigJavaDB;create=true
With other databases, you may need to add entries such as these
jdbc.username=admin
jdbc.password=secret
jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver

这是我收到的错误:

Exception in thread "main" java.sql.SQLException: The url cannot be null
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at SimpleDataSource.getConnection(SimpleDataSource.java:36)
    at Database.findEmployee(Database.java:89)
    at Main.main(Main.java:77)

目前,我已经尝试了所有方法,需要一些帮助。我已尝试多次调试它,但似乎找不到该错误。有任何帮助或建议吗?

1 个答案:

答案 0 :(得分:0)

SimpleDataSource.init(...)从未调用过,因此不会设置url。

关于您发布的代码的一些评论。为什么每种方法都是静态的,为什么不使用任何面向对象编程的原理?我不知道您的Java开发人员经验是什么(不会冒犯)。因此,使用此应用程序模型可能有很好的理由。如果您相对较新,则应检查这些原则。它使您可以编写更多可维护的代码,并减少容易出错的代码。