-
validate uID & pwd from sql base
it has passed over a week since i've been trying to solve the problem and i tried everything, asked people from different java forums, but nobody could help me. a allready post my problem few days ago...now i'm asking if anyone could post their code, so that i can compare and repair my code.
the code should compare entered username and password with the one saved in the sql database.
i would be very very very grateful!!!
my code:
////////////////////////////
CheckUser.java
///////////////////////////
import java.awt.*;
import java.net.*;
import java.io.*;
import java.io.FileNotFoundException;
import java.sql.*;
public class CheckUser {
// Get connection
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 {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
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());
};
}
public boolean isValidLogin(String userName, String password) {
boolean isOk = false;
try {
Statement stmt = connection.createStatement();
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();
}
}
/////////////////////
MyApplet.java
////////////////////
import java.awt.*;
import java.net.*;
import java.io.*;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.*;
import java.sql.*;
public class MyApplet extends java.applet.Applet
{
public String strUsername = "";
public String strPassword = "";
public void init() {
if (!login()) {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"accessdenied.html"),"_top");
}
catch (Exception e) {e.printStackTrace(); }
}
else {
try {
getAppletContext().showDocument
(new URL(getCodeBase()+"applog.html"),"_top");
}
catch (Exception e) {e.printStackTrace(); }
{
Button myButton = new Button (" Zapiši!");
add (myButton);
}
}
}
public boolean login() {
boolean userValid = false;
MyLogin login = new MyLogin (new Frame(""));
//appendtext login = new appendtext (new Frame(""));
requestFocus();
try{
if (login.id)
{
CheckUser check = new CheckUser();
strUsername = login.username.getText();
strPassword = login.password.getText();
userValid = check.isValidLogin(strUsername, strPassword);
System.out.println
("The password for " + strUsername + " is " + (userValid?"valid":"invalid"));
}
else
System.out.println
("Zdej pa nebi vec kliku al ka ???");
}
catch(Exception e) {System.out.println("Napaka!!"); }
login.dispose();
return userValid;
}
}
-
When you run the program, what goes wrong? Does it always deny people, does it always accept people? Does it throw an exception? Also, is there a place where this is online with a test database?
~evlich
Similar Threads
-
By alex in forum Database
Replies: 3
Last Post: 11-03-2005, 05:27 AM
-
By angelia in forum Database
Replies: 0
Last Post: 07-01-2003, 03:42 PM
-
By Samantha in forum Database
Replies: 6
Last Post: 01-25-2002, 02:15 PM
-
By Becky in forum Database
Replies: 1
Last Post: 07-11-2000, 03:27 PM
-
By Gourav Sharma in forum Database
Replies: 8
Last Post: 06-19-2000, 01:52 PM
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