So I am attempting to make a simple chat messenger in Eclipse. At the moment I am stumped on how to compare the users login details against those stored in a database table. I have an 'askName()' method that takes a username and password, and this needs to use my 'authenticate' method to check details stored in a Login table in the database.
I have been searching here and all over online, and have found a lot of code which has been useful for getting ideas. However I have been banging my head against this for a while and am still very new to programming, any help would be greatly appreciated!
So at the moment inside the Client class I have these login methods:
public void askName()
{
// get the clients name
boolean b = true;
// loop in case they enter a null name - aint nobody got time for that
while(b == true)
{
out.println("What is your name?");
String name = null;
try {
name = in.readLine();
} catch (IOException e) {
System.out.println("Can't read name");
}
out.println("What is your password?");
String password = null;
try {
password = in.readLine();
} catch (IOException e) {
System.out.println("Can't read password");
}
if(name != null && password != null)
{
if(Authenticate(name, password))
{
this.username = name;
b = false;
out.println("Welcome " + this.username);
} else {
out.println("Invalid username/password combination!");
}
}
else
{
out.println("Please enter a valid username and password. Your name must contain at least one letter");
}
}
}
private boolean Authenticate(String name, String password) { // added by Alex 18/03/14
// Method to check the entered username/password is valid against the database
// No database class at this point
//Database database = new Database();
return database.Authenticate(name, password);
}
I have managed to implement the database in class 'MyJDBC' and just need to figure out how to use my 'authenticate' method to check the details. I am using PostgreSQL. This is the current query I have made for checking login credentials "SELECT password FROM userlogin WHERE username ='"+name+"'"
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyJDBC {
public static void main(String args[]) {
System.out.println("PostgreSQL JDBC Connection Testing");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
Statement stmt = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://dbteach2.cs.bham.ac.uk:5432/user",
"username",
"password");
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( "SELECT password FROM userlogin WHERE username ='"+name+"'");
while (rs.next()) {
String Username = rs.getString("Username");
String Password = rs.getString("Password");
int sid = rs.getInt("sid");
System.out.println("Username = " + Username);
System.out.println("Password = " + Password);
System.out.println("sid = " + sid);
System.out.println();
}
rs.close();
stmt.close();
connection.close();
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
}}
I am not asking anyone to do my code, but some advice on what I could do/what I am doing wrong would be appreciated. If you would like to see the rest of my code to try it out please say so.