I am a newbie to java and developing. I am using NetBeans 8.0 and Query Browser 1.1.2 to create the database and tables. once I hit the login button after entering the user details A message is being displayed as below
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
I created the database using Query Browser as I am new to MYSQL workbench than to Query Browser.
Below is the Home_Page code
public class Home_Page extends javax.swing.JFrame {
public String User;
Model.JDBC j=new JDBC();
public Home_Page(String U_ID) {
initComponents();
User=U_ID;
Toolkit t = Toolkit.getDefaultToolkit();
Dimension d = t.getScreenSize();
int w = (int) d.getWidth();
int h = (int) d.getHeight()-40;
setBounds(0, 0, w, h);
jLabel2.setText(new Controler.getDate().date());
time();
jLabel3.setText(U_ID);
ResultSet rs;
try {
rs = j.getData("select ulevel from user U_ID='"+U_ID+"'");
if(rs.next()==true){
if(rs.getString(1).equals("Admin")){
home.setEnabled(true);
EMPLOYEE.setEnabled(true);
employee.setEnabled(true);
user.setEnabled(true);
machine.setEnabled(true);
branch.setEnabled(true);
}else if(rs.getString(1).equals("user")){
home.setEnabled(true);
EMPLOYEE.setEnabled(false);
employee.setEnabled(false);
user.setEnabled(false);
machine.setEnabled(false);
branch.setEnabled(false);
}else{
System.out.println("else");
}
}
} catch (Exception ex) {
Logger.getLogger(Home_Page.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void time() {
new Thread(new Runnable() {
public void run() {
while (true) {
String times = new Date().toString();
String h = times.split(" ")[3];
jLabel1.setText(h);
try {
Thread.sleep(60);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
@SuppressWarnings("unchecked")
private void branchActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
Branch b=new Branch();
jDesktopPane1.add(b);
b.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
public void clear() {
this.dispose();
}
private void logoutActionPerformed(java.awt.event.ActionEvent evt) {
java.awt.Toolkit.getDefaultToolkit().beep(); int t = JOptionPane.showConfirmDialog(null, "Are You Sure...?");
if(t==JOptionPane.YES_OPTION){
this.dispose();
clear();
new Viewer.login().setVisible(true);
}
else if(t==JOptionPane.NO_OPTION)
{
}
else
{
}
}
private void userActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
User_ u=new User_();
jDesktopPane1.add(u);
u.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void employeeActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
Employee_ emp=new Employee_();
jDesktopPane1.add(emp);
emp.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void machineActionPerformed(java.awt.event.ActionEvent evt) {
jDesktopPane1.removeAll();
machine_ m = new machine_();
jDesktopPane1.add(m);
m.setVisible(true);
jDesktopPane1.updateUI();
// TODO add your handling code here:
}
private void homeActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void EMPLOYEEActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
Calendar expiry = Calendar.getInstance();
expiry.set(2013, 9 , 27,0,0); // Expire at 31 Jan 2010
Calendar now = Calendar.getInstance();
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Home_Page(" ").setVisible(true);
}
});
}
}
Below is my Login Form code
public class login extends javax.swing.JFrame {
public login() {
initComponents();
}
@SuppressWarnings("unchecked")
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
if (eid.getText().trim().length() == 0 | pwd.getPassword() == null) {
JOptionPane.showMessageDialog(null, "Username or Password cannot be blank", "Error", 0);
} else {
try {
ResultSet rs = new DBG1().getData("select * from user where eid = '" +eid.getText() + "'");
Vector v = new Vector();
if (rs.next()) {
v.add(rs.getString(1));
}
if (v.isEmpty()) {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", 0);
eid.setText("");
pwd.setText("");
eid.grabFocus();
} else {
ResultSet rs1 = new DBG1().getData("select * from user where eid ='" + eid.getText() + "'");
while (rs1.next()) {
String type = (rs1.getString(3));
if (eid.getText().trim().equalsIgnoreCase(rs1.getString("eid")) && pwd.getText().equals(rs1.getString("pwd")) && type.equals("Admin")) {
this.dispose();
new Home_Page(eid.getText()).setVisible(true);
// new Home_Page(eid.getText()).setVisible(true);
} else if (eid.getText().trim().equalsIgnoreCase(rs1.getString("eid")) && pwd.getText().equals(rs1.getString("pwd")) && type.equals("user")) {
this.dispose();
new Home_Page(eid.getText()).setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, "Invalid Username or Password", "Error", 0);
eid.setText("");
pwd.setText("");
eid.grabFocus();
}
}
this.dispose();
}
} catch (Exception e) {
System.out.println(e);
}
}
// TODO add your handling code here:
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
new login().dispose();
System.out.println(pwd.getPassword());
System.out.println("Cleared");
this.dispose();
// TODO add your handling code here:
}
private void eidActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Viewer.login().setVisible(true);
}
});
}
}
When I debug the project it should first run a loading window(which is not included in here) then the Login window.Once I enter the details and press the Login button, above error is shown in "POST_NEW debug panel" I have studied and followed all the instructions mentioned in all of the similar type questions that are posted earlier in this site.Yet I am in this condition.Could anyone please point out where should I amend my code or how should I get rid of this error and step into the Home_Page of my project
Try using username root and password empty. If that doesn't work you could try this: Access denied for user 'username'@'localhost' (using password: YES) in C:\webdev\wamp\www\membershipSite\classes\Mysql.php on line 9