DBConnector + LoginBean (SQLite)


//...
//DBConnector.java

import java.sql.*;

//load program
public class DBConnector {
	
	 /*public static void main(String[] args) throws Exception {
		 //Use this block to test as Java program
		 getConnection();
		 System.out.println("Program Successful...");
	 }*/

	//get a connection
	public static Connection getConnection() {
		 System.out.println("Connection Started...");
		//declare and initialize 
		Connection con = null;
		//entering try catch
		try {
			//load driver class
		Class.forName("org.sqlite.JDBC").newInstance();
		 System.out.println("Class obtained...");
		 //connect to SQLite
		 con = DriverManager.getConnection("jdbc:sqlite:C:\\temp\\WEB_FILES\\SQLite\\sqlite-3_7_3\\Test.db");
		 //...
		 System.out.println("Connection fowarded...");
		} catch (Exception e) {
		e.printStackTrace();
		}
		return con;
		 
		 
		}
}





LoginBeanSQL.java


//...
//add getters and setters

//Xpages button code..
   public String doGetUserCreds() {
		
	System.out.println("Entering Try catch Block...");
        try {
        	
            //Declare and initialize variables
        	PreparedStatement preparedStmt=null;
        	//bring in connection from DBConnector class
            Connection con = DBConnector.getConnection();            
        	System.out.println("DataSource Obtained, connection made...");       	
        	//set up query string (as a PreparedStatement)     	
        	String query = "SELECT * FROM USER_LOGINS WHERE UserName=? AND Password =?";        	 
        	preparedStmt = con.prepareStatement(query);  
        	//Grab values from user, pass to program    
        	preparedStmt.setString(1,UserID);
        	preparedStmt.setString(2,PassWord);
        	//run query
        	ResultSet resultSt = preparedStmt.executeQuery();  
        	
        	System.out.println("Query ran...");        	
        	//do stuff based on user access
            while (resultSt.next()) {
            	System.out.println("searching resultSet...");
            	//Load the results to these variables to begin comparing
            	//values entered against values received
                String DBUserName = resultSt.getString("UserName");
                String DBPassword = resultSt.getString("Password");
                //2012.04.10.10.29.PM
                //TO DO: Log userID, time in, time out for debugging purposes
                //compare user submission against values returned from SQLite
            	if (UserID.equals(DBUserName) && PassWord.equals(DBPassword)) {
            		//...
        			System.out.println("It's a hit!");
        			//...
        			//load the appropriate Xpage
        			return "xsp-success";
        		} else {
        			System.out.println("Hummm... Let's take a look at that!");
        			//...
        			//load the appropriate Xpage
        			return "xsp-failure";
        		}
            	
            }
        } 
        //...
        catch(Exception ex) {
            ex.printStackTrace();
        }
        //...
        return UserID;
	
}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...