CrazyEngineers
  • help needed in java coding

    Updated: Oct 26, 2024
    Views: 1.0K
    can anyone help me in java coding
    i am trying to building a frame which will ask 4 username and psw and if they mathches by looking in database ,it will automatically show new frame which shows account info of login user by rerieving info from another table
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • hbk

    MemberJun 17, 2008

    u talking abt JDBC??
    Are you sure? This action cannot be undone.
    Cancel
  • elric

    MemberJun 17, 2008

    You could be more specific, like in which part you need help? Database connectivity or GUI design?
    Are you sure? This action cannot be undone.
    Cancel
  • jaichan

    MemberJul 18, 2008

    Hi friend first of all u understand the database connectivity
    Are you sure? This action cannot be undone.
    Cancel
  • Neha

    MemberJul 23, 2008

    hi!
    This is all related to database connectivity.
    You'll have to maintain a database for the same recording the logins and the passwords etc and connect it to your file.
    Are you sure? This action cannot be undone.
    Cancel
  • niraj.kumar

    MemberJul 24, 2008

    You can follow this step
    1. You need to create a table to store this information
    2. Learn JDBC its easy ... make the connection.. u will find lots of tutorial on this .
    3. Make a simple UI is java swing ....

    thats it ... if u need further then reply ... i mean wat kind of problem u r facing ..... paste the error msg here ...(if u have any)
    Are you sure? This action cannot be undone.
    Cancel
  • komputergeek

    MemberOct 21, 2008

    These are two classes from my project:


    Login.java :  
    
    import javax.swing.*; 
    import java.awt.event.*; 
    import java.sql.*; 
     
    public class Login 
    { 
        static JFrame f; 
        static JPanel p1; 
        static JPanel p2; 
        static JPanel p3; 
        static JLabel lUserName; 
        static JTextField tUserName; 
        static JLabel lPassword; 
        static JPasswordField pPassword; 
        static JButton bLogin; 
        static JButton bCancel;    
       
        public static void showLoginWindow () 
        {         
            f = new JFrame ("Login"); 
            p1 = new JPanel (); 
            p2 = new JPanel (); 
            p3 = new JPanel (); 
            lUserName = new JLabel ("User Name : "); 
            tUserName = new JTextField (20); 
            lPassword = new JLabel ("Password  :  "); 
            pPassword = new JPasswordField (20); 
            bLogin = new JButton ("Login"); 
            bLogin.setToolTipText ("Click here to Login"); 
            bLogin.setMnemonic('L'); 
            bLogin.addActionListener (new ActionListener () 
            { 
                public void actionPerformed (ActionEvent e) 
                {                                                  
                    try                     
                    {                                                                        
                        Connection con = null; 
                        Statement stmt; 
                        ResultSet rs;   
                           boolean flag=false;                                                                            
                        con = DBConnect.getCon ();                          
                        stmt = con.createStatement ();                                                                 
                        rs = stmt.executeQuery ("Select * from USERS  where USERNAME='" + tUserName.getText () + "' and PASSWORD = '" + pPassword.getText() + "'" ); 
                           while(rs.next()) 
                           {                                                 
                               if(rs.getString("USERNAME").equals(tUserName.getText()) &&   rs.getString("PASSWORD").equals(pPassword.getText())) 
                               {                                
                                   flag=true; 
                               }                                
                           }  
                           if(flag==false) 
                           {                                         
                               Result_AnalysisFrame frame= new Result_AnalysisFrame();  
                            frame.setVisible(true);   
                            f.hide(); 
                           } 
                           else 
                           { 
                               JOptionPane.showMessageDialog(null,"Login Failed !!!","Login Fail",JOptionPane.WARNING_MESSAGE); 
                           } 
                    } 
                    catch (Exception ex) 
                    { 
                        JOptionPane.showMessageDialog (null, ex, "Error", JOptionPane.INFORMATION_MESSAGE); 
                    } 
                } 
            } 
            ); 
            bCancel = new JButton ("Cancel"); 
            bCancel.setToolTipText ("Click here to Exit"); 
            bCancel.setMnemonic('C'); 
            bCancel.addActionListener (new ActionListener () 
            { 
                public void actionPerformed (ActionEvent e) 
                { 
                    System.exit (0); 
                } 
            } 
            ); 
            p1.add (lUserName); 
            p1.add (tUserName); 
            p2.add (lPassword); 
            p2.add (pPassword); 
            p3.add (bLogin); 
            p3.add (bCancel); 
     
            Box bx1 = Box.createVerticalBox (); 
            bx1.add (p1); 
            bx1.add (p2); 
            bx1.add (p3); 
     
            f.getContentPane ().add (bx1); 
            f.setVisible (true); 
            f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
            f.setLocation (380, 280);       
            f.pack (); 
            f.setResizable (false);         
        } 
    } 
     
     
    
    DBConnect.java :
    
     
    import java.sql.*; 
    import oracle.jdbc.*; 
    import javax.swing.*; 
     
    public class DBConnect 
    { 
     
        public static Connection getCon () 
        { 
            Connection con = null; 
            String url = "jdbc:odbc:RA2"; 
     
            try 
            { 
                Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); 
                 
                con = DriverManager.getConnection (url, "RA", "RA"); 
            }                
            catch (Exception e) 
            { 
                JOptionPane.showMessageDialog (null, e, "Error", JOptionPane.INFORMATION_MESSAGE); 
            } 
            return con; 
        } 
    }
    Are you sure? This action cannot be undone.
    Cancel
  • xero

    MemberOct 21, 2008

    the info is not sufficient to rectify. Looking at code(the jdbc ones) it seems more like a driver issue. But before proceeding further, i think you should first go thru the basics of jdbc and the types of drivers available with databases.

    I'll just give you intro with the 4 types of drivers ->
    1. JDBC ODBC driver - this is the driver you have used in your program. This driver converts the jdbc calls into odbc acting as a bridge. So make sure have configured the odbc correctly. eg if you are using oracle as db so you have to configure the odbc for oracle. (for more reference, google 😁)

    2. Native-API partly Java technology-enabled driver - in this the java converts the jdbc calls into db api calls.

    3. Net-protocol fully Java technology-enabled driver - in this JDBC API calls is translated into a DBMS-independent net protocol which is then translated to a DBMS protocol by a server

    4. Native-protocol fully Java technology-enabled driver - converts JDBC technology calls into the network protocol used by DBMSs directly. This is a pure java based driver !

    The above was just informative, don't need to think deep into it. It won't harm to know.

    So the take-away is that your code is using the driver of type 1 so first you must check the configuration for Db specific odbc configuration.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register