所以我想創建一個模擬在線視頻商店的應用程序。我在Workbench上創建了一個數據庫,我試圖創建一個if語句來檢查用戶輸入是否與數據庫上的電子郵件和密碼相匹配。但是,我要麼得到關於連接或關於驅動程序的錯誤。希望有人能幫助我,謝謝!Java和SQL數據庫
這裏是Java代碼
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
Class.forName("com.mysql.jdbc.Driver");
Scanner input = new Scanner(System.in);
String answer = "";
String sql = "";
String email = "";
String password = "";
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull [root on Default schema]", "<username>", "<password>");
Statement myStmt = myConn.prepareStatement(sql);
ResultSet rs;
while(!answer.equals("n") || !answer.equals("y"))
{
System.out.print("Do you have an account? (Y/N) : ");
answer = input.nextLine();
if(answer.toLowerCase().equals("n"))
{
System.out.println("Please enter the email and password for your new account.");
System.out.print("Email: ");
email = input.nextLine();
System.out.print("Password: ");
password = input.nextLine();
sql = "insert into accounts "
+ " (UserEmail, UserPassword)" + " values (?, ?)";
myStmt.executeUpdate(sql);
}
else if(answer.toLowerCase().equals("y"))
{
System.out.print("\nEmail: ");
email = input.nextLine();
System.out.print("\nPassword:");
password = input.nextLine();
rs = myStmt.executeQuery(sql);
if(!rs.absolute(1))
{
System.out.println("You do not have an account. Please create one.");
continue;
}
}
else{
System.out.println("Invalid input. Please try again.");
continue;
}
}
這裏是我的SQL腳本
create database users;
use users;
create Table Accounts(
UserEamil Char(20) NOT NULL ,
UserPassword Char(20) NOT NULL
);
這是我的錯誤: 異常線程 「main」 拋出java.lang.ClassNotFoundException:com.mysql。 jdbc.Driver
請發佈你的錯誤 –
我加了它。如果我註釋掉class.forname行,我得到這個錯誤: 線程「main」中的異常java.sql.SQLException:找不到適合jdbc的驅動程序:mysql:// localhost:3306/mysql?zeroDateTimeBehavior = convertToNull [根目錄默認模式] – pca03