2017-09-28 177 views
-1

現在我在這裏看到了一些這些問題,但我的問題有點不同。我創建了一個創建賬戶頁面和登錄頁面。我在MYSQL中創建了一個數據庫,其中存儲了各種用戶名和相應的密碼。我使用創建帳戶頁面創建了一個帳戶,然後通過登錄頁面進行了檢查,發現它正在工作。但後來我試着編寫一個代碼,在創建相應帳戶後,在MYSQL中自動創建一個表。但我得到了上述錯誤。我會告訴你我的登錄和創建帳戶頁面。訪問在JAVA中拒絕用戶'root'@'localhost'(使用密碼:YES)

LOGIN: -

import java.awt.*; 

import java.awt.event.*; 

import java.sql.Connection; 

import java.sql.DriverManager; 

import java.sql.ResultSet; 

import java.sql.Statement; 

import javax.swing.*; 

public class Login extends Frame implements ActionListener, WindowListener 

{ 

    Label l1,l2,l3; 

    JTextField t1; 

    JPasswordField p1; 

    JButton b0,b1; 

    String url = "jdbc:mysql://localhost:3306/database1"; 

    String user = "root"; 

    String password = "Newyear2016!"; 

    char[] s2; 

    String s1,s3; 

    Login() 

    { 

     addWindowListener(this); 

     setTitle("Login"); 

     setSize(500,500); 

     setLayout(null); 

     setVisible(true); 

     l3 = new Label("Login"); 

     l3.setBounds(120,50,300,50); 

     add(l3); 

     Font myFont1 = new Font("Helvetica",Font.ITALIC,30); 

     l3.setFont(myFont1); 

     l1 = new Label("User Name: "); 

     l1.setBounds(50,150,100,50); 

     add(l1); 

     l2 = new Label("Password: "); 

     l2.setBounds(50,250,100,50); 

     add(l2); 

     t1 = new JTextField(); 

     p1 = new JPasswordField(); 

     t1.setBounds(170,150,250,50); 

     p1.setBounds(170,250,250,50); 

     add(t1); 

     add(p1); 

     t1.addActionListener(this); 

     p1.addActionListener(this); 

     b0 = new JButton("Login"); 

     b0.setBounds(170,350,100,50); 

     add(b0); 

     b0.addActionListener(this); 

     b1 = new JButton("Go Back"); 

     b1.setBounds(300,350,100,50); 

     add(b1); 

     b1.addActionListener(this); 

    } 

    public void actionPerformed(ActionEvent e) 

    { 

     s1 = t1.getText(); 

     s2 = p1.getPassword(); 

     s3 = new String(s2);   

     Object o = e.getSource(); 

     if(o == b1) 

     { 

      dispose(); 

      Home h = new Home(); 

     } 

     mainLoop: 

     if(o == b0) 

     { 

      try 

      { 

       Connection myConn = 

DriverManager.getConnection(url,user,password); 

       Statement myStmt = myConn.createStatement(); 

       ResultSet myRs = myStmt.executeQuery("select * from login"); 

       while(myRs.next()) 

       { 


if(s1.equals(myRs.getString("usname"))&&s3.equals(myRs.getString("pwd"))) 

        { 

         System.out.println("Username Found"); 

         System.out.println("Password Found"); 

         JOptionPane.showMessageDialog(null,"Login Successful: 

"+s1); 

         break mainLoop; 

        } 



       } 

       JOptionPane.showMessageDialog(null,"Wrong Username/Password"); 



      } 

catch(Exception exc) 

{ 

    exc.printStackTrace(); 

} 
     } 

    } 


    public void windowClosing(WindowEvent e) 

{ 

     dispose(); 

    } 

    public void windowActivated(WindowEvent e) {} 

    public void windowClosed(WindowEvent e) {} 

    public void windowDeactivated(WindowEvent e) {} 

    public void windowDeiconified(WindowEvent e) {} 

    public void windowIconified(WindowEvent e) {} 

    public void windowOpened(WindowEvent e) {} 

} 

請告訴我,我做錯了。我檢查了我提供了正確的密碼。所以這不是問題。

創建帳戶: -

import java.awt。*;

import java.awt.event。*;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.swing。*;

公共類登錄延伸框架實現的ActionListener,的WindowListener

{

Label l1,l2,l3; 

JTextField t1; 

JPasswordField p1; 

JButton b0,b1; 

String url = "jdbc:mysql://localhost:3306/database1"; 

String user = "root"; 

String password = "Newyear2016!"; 

char[] s2; 

String s1,s3; 

Login() 

{ 

    addWindowListener(this); 

    setTitle("Login"); 

    setSize(500,500); 

    setLayout(null); 

    setVisible(true); 

    l3 = new Label("Login"); 

    l3.setBounds(120,50,300,50); 

    add(l3); 

    Font myFont1 = new Font("Helvetica",Font.ITALIC,30); 

    l3.setFont(myFont1); 

    l1 = new Label("User Name: "); 

    l1.setBounds(50,150,100,50); 

    add(l1); 

    l2 = new Label("Password: "); 

    l2.setBounds(50,250,100,50); 

    add(l2); 

    t1 = new JTextField(); 

    p1 = new JPasswordField(); 

    t1.setBounds(170,150,250,50); 

    p1.setBounds(170,250,250,50); 

    add(t1); 

    add(p1); 

    t1.addActionListener(this); 

    p1.addActionListener(this); 

    b0 = new JButton("Login"); 

    b0.setBounds(170,350,100,50); 

    add(b0); 

    b0.addActionListener(this); 

    b1 = new JButton("Go Back"); 

    b1.setBounds(300,350,100,50); 

    add(b1); 

    b1.addActionListener(this); 

} 

public void actionPerformed(ActionEvent e) 

{ 

    s1 = t1.getText(); 

    s2 = p1.getPassword(); 

    s3 = new String(s2);   

    Object o = e.getSource(); 

    if(o == b1) 

    { 

     dispose(); 

     Home h = new Home(); 

    } 

    mainLoop: 

    if(o == b0) 

    { 

     try 

     { 

      Connection myConn = 

的DriverManager.getConnection(URL,用戶,密碼);

  Statement myStmt = myConn.createStatement(); 

      ResultSet myRs = myStmt.executeQuery("select * from login"); 

      while(myRs.next()) 

      { 

如果(s1.equals(myRs.getString( 「usname」))& & s3.equals(myRs.getString( 「PWD」)))

   { 

        System.out.println("Username Found"); 

        System.out.println("Password Found"); 

        JOptionPane.showMessageDialog(null,"Login Successful: 

「+ S1);

    break mainLoop; 

       } 


      } 
      JOptionPane.showMessageDialog(null,"Wrong Username/Password"); 



     } 

     catch(Exception exc) 

     { 
      exc.printStackTrace(); 

     } 

    } 

} 



public void windowClosing(WindowEvent e) 

{ 

    dispose(); 

} 

public void windowActivated(WindowEvent e) {} 

public void windowClosed(WindowEvent e) {} 

public void windowDeactivated(WindowEvent e) {} 

public void windowDeiconified(WindowEvent e) {} 

public void windowIconified(WindowEvent e) {} 

public void windowOpened(WindowEvent e) {} 

} #Edit: - ?對不起,我忘了提供創建帳戶頁面這是問題請問是否通過它

+0

我認爲這可以幫助你https://stackoverflow.com/questions/11922323/java-sql-sqlexception-access-denied-for-user-rootlocalhost-using-password –

+0

我認爲這能幫助你:https://stackoverflow.com/questions/11922323/java-sql-sqlexception-access-denied-for-user-rootlocalhost-using-password –

回答

0

您的一些代碼不在代碼塊中,因此很難閱讀,但您是否加載了驅動程序類?

String url="jdbc:mysql://localhost:3306/database1"; 
String userName="root"; 
String password="Newyear2016!"; 
try { 

    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    Connection con=DriverManager.getConnection(url,username,password); 
+0

其實我看到這個驅動程序的東西,我寫了你寫的確切代碼在這裏,但它給了同樣的錯誤。現在我以前不需要這一行,所以我從來沒有寫過。 –

相關問題