|
-
May 1st, 2012, 03:38 PM
#1
create a global login
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.
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
Ideally, I want the User to run a query without having to log in all the time.
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
-
May 2nd, 2012, 04:47 AM
#2
Re: create a global login
Creating a new connection each time you need to access the database can be expensive you should look at using connection pooling. This will also sort your problem with having to keep entering the username and password.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|