method non static

public class LoGIne{
public String user (String a){

        String rr = txt_user.getText();
        String pass = txt_pass.getText();
        //ConDb(user,pass);
        return rr;

        }
        ---------------------------------------------------
method  static 
 public static Connection ConDb(){
       //String USER  ="mh";
        String d ,l,h;
        d =("ameer");
       String PASS=d;
        LoGIne a = new LoGIne();

        String USER = a.user(h);
        //  String PASS = "ameer";

       try{
      //STEP 2: Register JDBC driver
      Class.forName("oracle.jdbc.driver.OracleDriver");

  Connection  conn = DriverManager.getConnection(DB_URL,USER,PASS);
   return conn;
   }catch(Exception e){
       JOptionPane.showMessageDialog(null, e);
       return null;
   }

--------------------
we will return value of function user to use it in function conDB to give value into this
USER = user(h);
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);

Recommended Answers

All 10 Replies

Hi,
In real life user is an object, connection is an object, login is a method or an event.
Make an User class with attributes username, password:

public class User
{
    private String username;
    private String password;

    User(String uname, String pass)
    {
        this.username = uname;
        this.password = pass;
    }

    public String getUsername()
    {
        return this.username;
    }

    public String getPassword()
    {
        return this.password;
    }

}

//Connection class

public class OConnection
{
    private User user;
    private String driver;
    private String DB_URL;

    Connection OConnexion(User user, String drv, String server)
    {
        this.user = user;
        this.driver = drv;
        this.DB_URL = server;
        try
        {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection conn = DriverManager.getConnection(this.DB_URL,this.user.getUsername(),this.user.getPassword());
                return conn;
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
            return null;
    }
  }

}

thank you very much
i want to trial it now ..

Nice OO thinking - the User class is excellent, but the other class... horrible naming! A method called OConnexion in a class called OConnection that returns a Connection is guaranteed to confuse people. Did you start out with a constructor, then change it when you realised you couldn't return a Connection from it?
OConnexion is really a factory method that has no state information, so it could be static and included almost anywhere - it doesn't seem to benefit from having a class just to enclose it.

@JamesCherill right. Lets do some changes.

public class ConnFactory
{

public static Connection build_conn(User user, String drv, String server)
{
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(this.DB_URL,this.user.getUsername(),this.user.getPassword());
    return conn;
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
        return null;
    }
}

}

hmm, I'd make some small changes to the User class as well.
Declare the data fields to be final, and declare the constructor arguments to be final as well.

If it were a realistic User you'd also not have a getPassword() method but a validatePassword(final Object candidate) method that simply returns whether the candidate password is valid in relation with whatever is stored on the User instance (in its simplest form that'd be a String comparison, but it doesn't have to be).

Replace the class OConnection with ConnFactory class.
Lol... it's somthing wrong.
Lets write this again.

public class ConnFactory
{

public static Connection build_conn(User user, String drv, String server)
{
    try
    {
    Class.forName(drv);
    Connection conn = DriverManager.getConnection(server,this.user.getUsername(),this.user.getPassword());
    return conn;
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
        return null;
    }
}

}

Now must be ok.

That looks a lot better to me.

whats is correct class
ConnFactory or OConnection

ConnFactory is better than OConnection

Hi ,
The important point of instance methods(Non static methods) is that they're meant to be specific to a particular instance of the class... so you'll need to create an instance first. That way the instance will have access to the right connection and prepared statement.

It's really important that you understand that:

1.Instance methods (and fields etc) relate to a particular instance
2.Static methods and fields relate to the type itself, not a particular instance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.