validate uID & pwd from sql base


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: validate uID & pwd from sql base

  1. #1
    Join Date
    Jul 2005
    Posts
    9

    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;

    }
    }

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    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

  1. SQL Mail Problem
    By alex in forum Database
    Replies: 3
    Last Post: 11-03-2005, 06:27 AM
  2. OBDC and SQL Server
    By angelia in forum Database
    Replies: 0
    Last Post: 07-01-2003, 04:42 PM
  3. SQL server vs Access
    By Samantha in forum Database
    Replies: 6
    Last Post: 01-25-2002, 03:15 PM
  4. SQL server administrative knowledge
    By Becky in forum Database
    Replies: 1
    Last Post: 07-11-2000, 04:27 PM
  5. How to up load Sql server data base on web
    By Gourav Sharma in forum Database
    Replies: 8
    Last Post: 06-19-2000, 02: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
  •  
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