How does this example work even if no oracle to be used and only access is available?

How does this example work even if no oracle to be used and only access is available?

CONNECTION CLASS

package ExamplePackage;

import java.sql.*; import java.util.*;

public class ConnectionManager {

  static Connection con;
  static String url;

  public static Connection getConnection()
  {

     try
     {

        Class.forName("oracle.jdbc.driver.OracleDriver");
        System.out.println(" Defing the URL");

         String url= "jdbc:oracle:thin:@172.24.137.30:1521:ORA10G";
        // assuming "DataSource" is your DataSource name

       // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        try
        {   


            System.out.println(" Defing the username");
            String username= ""; 
            System.out.println(" Defing the password");
            String password= "";
            System.out.println(" Defing the connection");
            con = DriverManager.getConnection(url,username,password); 
            System.out.println(" connection done" +con);





        }

        catch (SQLException ex)
        {
           ex.printStackTrace();
        }
     }

     catch(ClassNotFoundException e)
     {
        System.out.println(e);
     }

  return con;

} }

LOGIN SERVLET

package ExamplePackage;

import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;

/** * Servlet implementation class LoginServlet */ public class LoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {

try {

 UserBean user = new UserBean();
 user.setUserName(request.getParameter("un"));
 user.setPassword(request.getParameter("pw"));

 user = UserDAO.login(user);

 if (user.isValid())
 {

      HttpSession session = request.getSession(true);       
      session.setAttribute("currentSessionUser",user); 
      response.sendRedirect("userLogged.jsp"); //logged-in page             
 }

 else 
      response.sendRedirect("invalidLogin.jsp"); //error page

}

catch (Throwable theException)
{ System.out.println(theException); } } }

USERBEAN

package ExamplePackage;

public class UserBean {

  private String username;
  private String password;
  private String firstName;
  private String lastName;
  public boolean valid;


  public String getFirstName() {
     return firstName;
}

  public void setFirstName(String newFirstName) {
     firstName = newFirstName;
}


  public String getLastName() {
     return lastName;
        }

  public void setLastName(String newLastName) {
     lastName = newLastName;
        }


  public String getPassword() {
     return password;
}

  public void setPassword(String newPassword) {
     password = newPassword;
}


  public String getUsername() {
     return username;
        }

  public void setUserName(String newUsername) {
     username = newUsername;
        }


  public boolean isValid() {
     return valid;
}

  public void setValid(boolean newValid) {
     valid = newValid;
}

}

USERDAO package ExamplePackage;

import java.text.*; import java.util.*; import java.sql.*;

public class UserDAO
{ static Connection currentCon = null; static ResultSet rs = null;

  public static UserBean login(UserBean bean) {

     //preparing some objects for connection 
     Statement stmt = null;    

     String username = bean.getUsername();    
     String password = bean.getPassword();   

     String searchQuery =
           "select * from users where username='"
                    + username
                    + "' AND password='"
                    + password
                    + "'";

  // "System.out.println" prints in the console; Normally used to trace the process
  System.out.println("Your user name is " + username);          
  System.out.println("Your password is " + password);
  System.out.println("Query: "+searchQuery);

  try 
  {
     //connect to DB 
     currentCon = ConnectionManager.getConnection();
     stmt=currentCon.createStatement();
     rs = stmt.executeQuery(searchQuery);           
     boolean more = rs.next();

     // if user does not exist set the isValid variable to false
     if (!more) 
     {
        System.out.println("Sorry, you are not a registered user! Please sign up first");
        bean.setValid(false);
     } 

     //if user exists set the isValid variable to true
     else if (more) 
     {
        String firstName = rs.getString("firstname");
        String lastName = rs.getString("lastname");

        System.out.println("Welcome " + firstName);
        bean.setFirstName(firstName);
        bean.setLastName(lastName);
        bean.setValid(true);
     }
  } 

  catch (Exception ex) 
  {
     System.out.println("Log In failed: An Exception has occurred! " + ex);
  } 

  //some exception handling
  finally 
  {
     if (rs != null)    {
        try {
           rs.close();
        } catch (Exception e) {}
           rs = null;
        }

     if (stmt != null) {
        try {
           stmt.close();
        } catch (Exception e) {}
           stmt = null;
        }

     if (currentCon != null) {
        try {
           currentCon.close();
        } catch (Exception e) {
        }

        currentCon = null;
     }
  }

return bean;

  }

}

View Answers









Related Tutorials/Questions & Answers:
How does this example work even if no oracle to be used and only access is available?
ModuleNotFoundError: No module named 'does-this-even-work-anymore'
Advertisements
How does the Java for each loop work?
What is data analysis, and how does it work?
How many hours does a data scientist work?
How does Hadoop distributed file system work?
How does Social Media Marketing Work
How many hours a week does a data scientist work?
import package.subpackage.* does not work
Jquery form validation does not work
Jquery form validation does not work
How Does the Display Work for the iPad?
Insert option does not work in my jstl... - JDBC
How does LBS work?
How to work with Ajax in spring
How to access Enterprise Beans
in my project, i have to add only vertical scrollbar to a panel,but not horizontal scrollbar. i need some example on it,how to doit.
how to download oracle 9i
How to access Subclass?
how formBackingObject() method will work.
How to list even numbers between 1 and 100?
What does a data scientist Do example?
What is a Proxy Server, and How Does it Work?
How to Access MS Access in jar.
How to Work - JSP-Servlet
How does it take to learn Java
How does it take to learn Java
How does it take to learn Java
Ajax example doesn;t work - Ajax
i need a school appication using struts frame work with oracle 10g using myeclipse6.0
Oracle Books
How java work on Unicode machanish
How does Java handle integer overflows and underflows?
Windows Mobile Development is Easy to Use When the Right Tools Work
how to get only checked data
How to install Oracle JDK 8 on Ubuntu?
Access URL
What is the answer and how the program work?
How the AOP used in Spring?
How are this() and super() used with constructors?
Example of copying and moving Oracle VirtualBox VM to another machine
What is jQuery and how it is used?
What is jQuery and how it is used?
How to Synchronize the Entire collection Frame Work?
how to access the MS ACCESS database with java
How is Python used in AI?
How does getcurrentsession() method works in hibernate sessionfactory?
oracle
how to save images in oracle using JSP
how to save images in oracle using JSP

Ads