0

I am new to making applications and never used database but know the basics of it. Found out a code to make a simple login page on the internet but the output returned is always "Incorrect Credentials".

Here is the code for the Submit button.

 if(jTextField2.getText().length()==0)  // Checking for empty field
  JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields");
else if(jPasswordField1.getPassword().length==0)  // Checking for empty field
  JOptionPane.showMessageDialog(null, "Empty fields detected ! Please fill up all fields");
else{
   String user = jTextField2.getText();   // Collecting the input
   char[] pass = jPasswordField1.getPassword(); // Collecting the input
   String pwd = String.copyValueOf(pass);  // converting from array to string
   if(validate_login(user,pwd))
      JOptionPane.showMessageDialog(null, "Correct Login Credentials");        
   else
      JOptionPane.showMessageDialog(null, "Incorrect Login Credentials");
   }
}

and here is the code for validate login-

private boolean validate_login(String username,String password) {
    try{           
   Class.forName("com.mysql.jdbc.Driver");  // MySQL database connection
   Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mmtc_law?zeroDateTimeBehavior=convertToNull [root on Default schema]?" + "user=root&password=");     
   PreparedStatement pst = conn.prepareStatement("Select * from login where username=? and password=?");
   pst.setString(1, username); 
   pst.setString(2, password);
   ResultSet rs = pst.executeQuery();                        
   if(rs.next())            
       return true;    
   else
       return false;            
 }
catch(Exception e){
       e.printStackTrace();
   return false;
   }

this is my phpmyadmin page- enter image description here

still when i enter the username and password as "admin" the output is "Incorrect Credentials". Please help. Thanks in advance!

MuthaFury
  • 805
  • 1
  • 7
  • 22

1 Answers1

1

This is clearly incorrect:

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mmtc_law?zeroDateTimeBehavior=convertToNull [root on Default schema]?" + "user=root&password=");     

Try this:

// Enter the root username and password or some credential that you know you can connect with
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mmtc_law", "root", "rootPassword");

Letting an application log into a database as root is asking for trouble. I'd create a new user and GRANT only the credentials it needs for the application.

There are too many things wrong with your code. Once you get this working I'd recommend refactoring this. It's always a bad sign when you see Java Swing code mingled in with database code. There's no layering that way. Write your data access layer as an interface-based, separate object that you can test and deploy on its own.

You should never, ever have hardwired database connection URL and credentials in code. They should be externalized. You shouldn't be creating connections, either. Those should be obtained from a connection pool.

Update: I just ran this class locally on my machine and connected to the MySQL instance. It'll work for you too if you give it the correct parameters.

Here is the Maven pom.xml entry for the connector JAR:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.2</version>
    </dependency>

Here is the class:

package database;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Database utilities
 * Created by Michael
 * Creation date 5/3/2016.
 * @link https://stackoverflow.com/questions/36999860/mysql-driver-problems/37000276#comment61553720_37000276
 */
public class DatabaseUtils {

    public static final String DEFAULT_DRIVER = "com.mysql.cj.jdbc.Driver";
    public static final String DEFAULT_URL = "jdbc:mysql://localhost/your-database-here";
    public static final String DEFAULT_USERNAME = "root";
    public static final String DEFAULT_PASSWORD = "your-root-password-here";

    public static void main(String[] args) {
        Connection connection = null;
        try {
            connection = createConnection(DEFAULT_DRIVER, DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
            DatabaseMetaData meta = connection.getMetaData();
            System.out.println(String.format("Connected to %s version %s", meta.getDatabaseProductName(), meta.getDatabaseProductVersion()));
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            close(connection);
        }
    }

    public static Connection createConnection(String driverClass, String url, String username, String password) throws ClassNotFoundException, SQLException {
        Class.forName(driverClass);
        return DriverManager.getConnection(url, username, password);
    }

    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • It says cannot identify symbol root. and the password keyword has got linked to the password field in my table, – Rishabh Gambhir Jul 01 '16 at 09:20
  • Please enter the root password to see if the connection can be made. – duffymo Jul 01 '16 at 09:21
  • Entering a password gives this error- Cannot establish a connection to jdbc:mysql://localhost:3306/mmtc_law?zeroDateTimeBehavior=convertToNull using com.mysql.jdbc.Driver (Access denied for user 'root'@'localhost' (using password: YES)) – Rishabh Gambhir Jul 01 '16 at 09:29