CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2010
    Posts
    121

    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

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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 code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured