MySQL员工数据库java.sql.SQLException:url不能为空
我正在用java创建一个员工数据库,但不断出现“url不能为空”错误。以下是我的主类代码:MySQL员工数据库java.sql.SQLException:url不能为空,java,mysql,Java,Mysql,我正在用java创建一个员工数据库,但不断出现“url不能为空”错误。以下是我的主类代码: 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
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();
}
}
}
这是我的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)
在这一点上,我已经尝试了所有的方法,需要一些帮助。我试过多次调试它,但似乎找不到错误。任何帮助或建议?SimpleDataSource.init(…)从未被调用,因此不会设置url
关于您发布的代码的一些备注。为什么每个方法都是静态的,为什么不使用任何面向对象编程的原则?我不知道你对Java的开发经验是什么(没有冒犯)。因此,使用此应用程序模型可能有一个很好的理由。如果你是比较新的,你应该检查这些原则。它允许您编写更易于维护、更不容易出错的代码。在
props.load(in)之后,您是否检查过这一点
你的道具
对象实际上有属性吗?谢谢,是的,我对Java比较陌生。每个人都至少有一次是Java新手。享受语言及其可能性。
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)