DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2005

    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:
    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 {
    DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
    connection = DriverManager.getConnection(url,"usr","pwd");

    if (connection != null) {
    System.out.println("Successfully connected");
    // 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: "+


    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();


    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 {
    (new URL(getCodeBase()+"accessdenied.html"),"_top");
    catch (Exception e) {e.printStackTrace(); }
    else {

    try {
    (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(""));

    if (login.id)
    CheckUser check = new CheckUser();
    strUsername = login.username.getText();
    strPassword = login.password.getText();
    userValid = check.isValidLogin(strUsername, strPassword);

    ("The password for " + strUsername + " is " + (userValid?"valid":"invalid"));
    ("Zdej pa nebi vec kliku al ka ???");
    catch(Exception e) {System.out.println("Napaka!!"); }
    return userValid;


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

Similar Threads

  1. SQL Mail Problem
    By alex in forum Database
    Replies: 3
    Last Post: 11-03-2005, 05:27 AM
  2. OBDC and SQL Server
    By angelia in forum Database
    Replies: 0
    Last Post: 07-01-2003, 03:42 PM
  3. SQL server vs Access
    By Samantha in forum Database
    Replies: 6
    Last Post: 01-25-2002, 02:15 PM
  4. SQL server administrative knowledge
    By Becky in forum Database
    Replies: 1
    Last Post: 07-11-2000, 03: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, 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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.