-
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
-
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
-
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?
-
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
-
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
-
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();
}
}
-
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 06:16 AM.
eschew obfuscation
-
thanks!!!!!!
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks