getting information from another class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: getting information from another class

  1. #1
    Join Date
    Jul 2005
    Posts
    9

    getting information from another class

    this is my problem: i have two java files(classes), CheckUser.java and MyLogin.java!
    in first one it opens sql database and gets info about username and password from it.
    in MyLogin.java is a code for GUI...textfield for a username and password.
    now, i wan't to send string username and password from MyLogin to CheckUser,where it'll be compared to the one in database, and if it is correct, it will send back if it is valid or no.

    it's a classic username validation...i just don't know how to send string to another class and back....help me please!!!!i'm new in this

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Piece of cake...

    ...really

    Put this method in CheckUser
    Code:
    public boolean isValidLogin(String userName, String password) {
      boolean isOk=false;
      /* do the check and set the value of isOk accordingly  */
      .
      .
      return isOK; 
    }
    In MyLogin you declare an instance of ChekUser
    Code:
    CheckUser cu=new CheckUser();
    /* get user input from GUI, e.g. userName and password */
    /* then check using the CheckUser instance */
    boolean loginOK=cu.isValidLogin(userName, password);
    Specifically:
    As it is, it is not the classes that pass values between them, its the
    classes' instances.
    eschew obfuscation

  3. #3
    Join Date
    Jul 2005
    Posts
    9
    it gives me an error...
    probably a thing with public function isValidLogin:
    i put it in
    public class CheckUser{
    ...
    ...code for connection...
    ...
    public boolean isValidLogin(...){
    }

    }

    is this wrong? should this boolean function be declared somewhere else?

  4. #4
    Join Date
    Jul 2005
    Posts
    9
    forgot to post the error message:
    G:\JAVA\AppLog\CheckUser.java:38: illegal start of expression
    public boolean isValidLogin(String userName, String password) {
    ^
    1 error

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I need to see the all the code to pick this error, the method declaration is ok, it must
    be where you have placed it, e.g. the coding prior to the declaration.
    eschew obfuscation

  6. #6
    Join Date
    Jul 2005
    Posts
    9
    this is CheckUser.java:

    import java.sql.*;


    public class CheckUser {
    public CheckUser() throws Exception {
    // Get connection


    DriverManager.registerDriver(new
    com.microsoft.jdbc.sqlserver.SQLServerDriver());
    String serverName = "ip_number";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String databaseName = "applog";
    String url = "jdbc:sqlserver://" + mydatabase +";databaseName="+databaseName+";";
    Connection connection = DriverManager.getConnection(url,"usr","pwd");
    if (connection != null) {
    System.out.println();
    System.out.println("Successfully connected");
    System.out.println();
    // Meta data
    DatabaseMetaData meta = connection.getMetaData();
    System.out.println("\nDriver Information");
    System.out.println("Driver Name: "
    + meta.getDriverName());
    System.out.println("Driver Version: "
    + meta.getDriverVersion());
    System.out.println("\nDatabase Information ");
    System.out.println("Database Name: "
    + meta.getDatabaseProductName());
    System.out.println("Database Version: "+
    meta.getDatabaseProductVersion());

    }

    public boolean isValidLogin(String userName, String password) {
    boolean isOk=false;
    Statement stmt = connection.createStatement();
    ResultSet p = stmt.executeQuery("SELECT Login, Password FROM Users WHERE Login=userName AND Password=password");

    if (p.next())

    return isOK;

    };


    //stmt.close();
    //connection.close();

    }
    public static void main (String args[]) throws Exception {
    CheckUser test = new CheckUser();

    }
    }

  7. #7
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    This one compiles.

    Pls check my comments, good luck !

    Code:
    import java.sql.*;
    
    public class CheckUser {
      // declare all these as class global variables, - accessible by all methods
      // in the class
      String serverName = "ip_number";
      String portNumber = "1433";
      String mydatabase = serverName + ":" + portNumber;
      String databaseName = "applog";
      String url = "jdbc:sqlserver://" + mydatabase + ";databaseName=" +
          databaseName + ";";
      Connection connection = null;
    
      public CheckUser() throws Exception {
        // Get connection
    
        DriverManager.registerDriver(new
                                     com.microsoft.jdbc.sqlserver.SQLServerDriver());
    
        connection = DriverManager.getConnection(url, "usr", "pwd");
    
        if (connection != null) {
          System.out.println();
          System.out.println("Successfully connected");
          System.out.println();
          // Meta data
          DatabaseMetaData meta = connection.getMetaData();
          System.out.println("\nDriver Information");
          System.out.println("Driver Name: "
                             + meta.getDriverName());
          System.out.println("Driver Version: "
                             + meta.getDriverVersion());
          System.out.println("\nDatabase Information ");
          System.out.println("Database Name: "
                             + meta.getDatabaseProductName());
          System.out.println("Database Version: " +
                             meta.getDatabaseProductVersion());
    
        }
    
        ;
    
        //stmt.close();
        //connection.close();
    
      }
      /**
       * this one was 'inside' the constructor....
       */
      public boolean isValidLogin(String userName, String password) {
        boolean isOK = false;
        try {
          Statement stmt = connection.createStatement();
          // the sqlstring must use the variables !
          ResultSet p = stmt.executeQuery(
              "SELECT Login, Password FROM Users WHERE "+
              "Login='"+userName+"' AND Password='"+password+"'");
    
          if (p.next())
            isOK = true;
        } catch (SQLException se) {
          se.printStackTrace();  // login fails for db error
        }
        return isOK;
      }
    
      public static void main(String args[]) throws Exception {
        CheckUser test = new CheckUser();
    
      }
    }
    Last edited by sjalle; 07-19-2005 at 07:16 AM.
    eschew obfuscation

  8. #8
    Join Date
    Jul 2005
    Posts
    9
    thanks!!!!!!

  9. #9
    Join Date
    Jul 2005
    Posts
    9

    hmmmm...

    now i don't know how to edit the "MyLogin.java" file:
    CODE:
    ....
    ....
    public boolean login() {
    boolean userValid = false;
    MyLogin login = new MyLogin (new Frame(""));

    // appendtext login = new appendtext (new Frame(""));

    requestFocus();
    if (login.id)
    {
    username = login.username.getText();
    password = login.password.getText();
    userValid = CheckUser(username , password);


    System.out.println
    ("The password for " + username + " is " + (userValid?"valid":"invalid"));
    }
    else
    System.out.println
    ("Zdej pa nebi vec kliku al ka ???");

    login.dispose();
    return userValid;

    }

    private boolean CheckUser(String usr, String pwd) {

    return (usr.equals("drago") && pwd.equals("drago"));
    }

    u see...i wrote that just for testing,so it doesn't read from sql database, i just wrote which password is correct. where do i put these lines?
    CheckUser cu=new CheckUser();
    /* get user input from GUI, e.g. userName and password */
    /* then check using the CheckUser instance */
    boolean loginOK=cu.isValidLogin(userName, password);

    do i need the function boolean CheckUser, or do i have to edit it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center