//the aim of bellow program is to make a sample jdbc program
//Bellowis my dao database class which has a createConnection() method which returns me connection object
public class oracleDAO {
public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";//database driver name
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:XE";//database url
public static final String DBUSER = "system";//database userid
public static final String DBPASS = "system";//database password
// method to create Oracle connections
public static Connection createConnection() {
// Use DRIVER and DBURL to create a connection
// Recommend connection pool implementation/usage
Connection conn=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//invoking the driver
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//getting connection by passing database url , database password
System.out.println("DATABASE CONNECTION ESTABLISHED!n\n");
}
catch (ClassNotFoundException ce) {
System.out
.println("Sorry error in loading class.....Class not found.");
}
catch (SQLException se) {
System.out
.println("\t!!!!!!!!!!DATABASE CONNECTION IS NOT ESTABLISHED!!!!!!!!!!!\n\n");
}
return conn;
}
}
//Bellow is my bean were i will store the data from front end at servlet and pass to database layer
public class LoginBean {
public String username;
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
//Below is my class which access the database and get the details of login table and checks for username and password and returns true or false based on the credentials which was created by me in oracle database
import java.sql.*;
import javax.swing.text.html.HTMLDocument.Iterator;
import com.tcs.practice.dto.LoginBean;
public class CheckLogin {
public String checkUserCredential(LoginBean ob) {
String dbUsername="";
String dbPassword="";
try{
Connection con = oracleDAO.createConnection();//getting connection object
Statement st=con.createStatement();//getting statement
ResultSet s = st.executeQuery("select * from login");//firring a sql querry
//Iterator it = s.i
while (s.next()) //iterating through the result set
{
dbUsername=s.getString("username");//getting the username column value
dbPassword=s.getString("password");//getting the password column value
System.out.println("username" + s.getString("username"));
}
}
catch(Exception e){
e.printStackTrace();
}
if(dbUsername.equals(ob.getUsername()) && dbPassword.equals(ob.getPassword()))
return "true";
else
return "false";
}
}
//Bellowis my dao database class which has a createConnection() method which returns me connection object
public class oracleDAO {
public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";//database driver name
public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:XE";//database url
public static final String DBUSER = "system";//database userid
public static final String DBPASS = "system";//database password
// method to create Oracle connections
public static Connection createConnection() {
// Use DRIVER and DBURL to create a connection
// Recommend connection pool implementation/usage
Connection conn=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//invoking the driver
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);//getting connection by passing database url , database password
System.out.println("DATABASE CONNECTION ESTABLISHED!n\n");
}
catch (ClassNotFoundException ce) {
System.out
.println("Sorry error in loading class.....Class not found.");
}
catch (SQLException se) {
System.out
.println("\t!!!!!!!!!!DATABASE CONNECTION IS NOT ESTABLISHED!!!!!!!!!!!\n\n");
}
return conn;
}
}
//Bellow is my bean were i will store the data from front end at servlet and pass to database layer
public class LoginBean {
public String username;
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
//Below is my class which access the database and get the details of login table and checks for username and password and returns true or false based on the credentials which was created by me in oracle database
import java.sql.*;
import javax.swing.text.html.HTMLDocument.Iterator;
import com.tcs.practice.dto.LoginBean;
public class CheckLogin {
public String checkUserCredential(LoginBean ob) {
String dbUsername="";
String dbPassword="";
try{
Connection con = oracleDAO.createConnection();//getting connection object
Statement st=con.createStatement();//getting statement
ResultSet s = st.executeQuery("select * from login");//firring a sql querry
//Iterator it = s.i
while (s.next()) //iterating through the result set
{
dbUsername=s.getString("username");//getting the username column value
dbPassword=s.getString("password");//getting the password column value
System.out.println("username" + s.getString("username"));
}
}
catch(Exception e){
e.printStackTrace();
}
if(dbUsername.equals(ob.getUsername()) && dbPassword.equals(ob.getPassword()))
return "true";
else
return "false";
}
}
No comments:
Post a Comment