How do I create a global login so that when the user logs in with their username and password that information is retained?
Here is my connection class. Currently, I am using static final variables to send the username and password to the connection method.
Ideally, I want the User to run a query without having to log in all the time.Code:/**
* This is the Connection class
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost:3306/db";
static String DATABASE_NAME = "db";
static final String USERNAME = "root";
static String PASSWORD = "";
static Connection con;
public static boolean login; //login flag
//***Constructor***
public DatabaseConnection( )
{
try
{
Class.forName(JDBC_DRIVER).newInstance();
//connect();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//***end of Constructor***
//***connect***
//Menu Option 1
public static Connection getConnection(String username, String password)
{
try {
Class.forName(JDBC_DRIVER);
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
System.out.println("*** Connected ***");
login = true; //login flag
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
System.out.println("*** Incorrect Login Details ***");
login = false; //login flag
}
return con;
}
//Menu Option 9 - Disconnect
public void disconnect()
{
if (con != null)
{
try
{
con.close();
System.out.println("You are now disconnected from the Database \n");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}//***end of disconnect***
}//end of class
Code:public void insertTableCommand () throws SQLException, IOException
{
if (c.login == true) //Successful
{
try
{
Connection con = c.getConnection(c.USERNAME, c.PASSWORD);
// create java preparedstatement using a sql update query
String sql = "INSERT INTO test (Field1, Field2)" +
"VALUES(?,?)";
PreparedStatement ps1 = con.prepareStatement(sql);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter movie name:");
//set the preparedstatement parameters
String Field1 = bf.readLine();
ps1.setString(1, Field1);
System.out.println("Enter year:");
String Field2 = bf.readLine();
ps1.setString(2, Field2);
int count = ps1.executeUpdate(); // call executeUpdate to insert sql insert statement
System.out.println("Thank you - table updated. " + count + " row(s) affected");
con.commit();//commit the changes to the DB
con.close();//close the statement
ps1.close();//close the connection
}
catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
catch (Exception e){
e.printStackTrace();
}
}
else //Unsuccessful
{
System.out.println("You are not logged in. Please select menu item 1 to login");
}
}//end of method
Thanks
