Share on Google+Share on Google+

javacoder
How does this example work even if no oracle to be used and only access is available?
0 Answer(s)      5 years and 3 months ago
Posted in : JDBC

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;

  }

}

Ads

View Answers









Tutorials   
Java Spring Hibernate Struts Training how to retrieve text and images from mysql database and show on html page using jsp servlet How to install Neo4j on Ubuntu 15.10? please help me in these prog MYSQL - mysql copy table to another table example by creating new table solution for mapping hibernate and pojos from net beans through database Connect GWT application with Oracle inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store This code have created jTable that connected with a database.every cell of last column have a button.But this button doesn't work properly.Where is the error java program Java Database Program jdbc Database connection class in a live project ..?? What is JDBC architecture? Unable to Create DSN for excel to ms access importing data and edit that data Database Scrolling Buttons how to use foreign key in java How to delete content of a log file from linux terminal? JSP-Oracle connectivity Student Management System Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same crud operations with foreign key constraint. I really need help with this assignment question Please help me out Please Backup Failed Mysqldump Got Errno 28 On Write Upgrade MySQL 5.1 to 5.5 in cpanel mysqldump all databases com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist persistence xml class element persistence.xml for MySQL MySQL JDBC Driver pom dependency code Blob column jdk6 and oracle 8i connexion oracle 8i et jdk6 java [ cannot retrive date from sql ] why?? Order Entry System How to threow timeout exception when DB is not responding How to threow timeout exception when DB is not responding This is the employee form to add data to mysql database but I got error of NullPointerException, Is this write code please give me feedback as soon as possible make a method which takes user name & pwd as parameter & validate them against the database. If the corresponding user exist return true otherwise false. prabhakar validating credentials and displaying error message in login .jsp if not valid JDBC jdbc jdbc code register servlet vibhu connectiondetails from vibhu

Ads

 
Advertisement null

Ads