2017-08-04 19 views
-1

所以我想創建一個模擬在線視頻商店的應用程序。我在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

+1

請發佈你的錯誤 –

+0

我加了它。如果我註釋掉class.forname行,我得到這個錯誤: 線程「main」中的異常java.sql.SQLException:找不到適合jdbc的驅動程序:mysql:// localhost:3306/mysql?zeroDateTimeBehavior = convertToNull [根目錄默認模式] – pca03

回答

0

此代碼是不會因爲值尚未着手

sql = "insert into accounts " 
    + " (UserEmail, UserPassword)" + " values (?, ?)"; 

myStmt.executeUpdate(sql); 

你應該做的是使用sql創建一個PreparedStatement,然後調用setString爲每個參數

sql = "insert into accounts " 
    + " (UserEmail, UserPassword)" + " values (?, ?)"; 
Statement myStmt = myConn.prepareStatement(sql); 
myStmt.setString (1, email); 
myStmt.setString (2, password); 

myStmt.executeUpdate(); 

目前在你的代碼的頂部,你有

 Connection myConn = DriverManager.getConnection("....");       
     Statement myStmt = myConn.prepareStatement(sql); 

sql此時的值是一個空字符串 - 它不會工作

注2

請諮詢此answer h ow來設置你的連接字符串正確

+0

謝謝,這樣做更有意義。除了我的驅動問題之外,你還有其他建議嗎? – pca03

+0

yes,'while(!answer.equals(「n」)||!answer.equals(「y」))''應該使用'&&'**不**''' –

+0

讓我的驅動程序工作,但現在我得到這個錯誤:異常在線程「主」com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'UserEmail' 任何想法? – pca03

0

您是否下載了mysql jdbc驅動程序? 你應該能夠通過添加類路徑來解決它:

C:\test>java -cp c:\test\mysql-connector-java-5.1.8-bin.jar;c:\test JDBCExample 

摘自: https://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/

+0

是的,我已經完成了這個 – pca03

+0

ClassNotFoundException通常意味着Java找不到指定的類,並且需要在類路徑中提供類或庫,我的建議是玩弄你的類路徑設置,確保你正確引用它。 –

+0

我有我的項目文件夾中的整個mysql連接器文件夾,但它不在我的src文件夾中。我應該把它放在那裏還是應該更改「Class.forName(」com.mysql.jdbc.Driver「)」行? – pca03