Hi,
There is something wrong with my code. I can't seem to access the database anymore not can I add new data information. I am suppose to create a registration form with username and password. Then save it to a database on MS Access. Then login using the login form. And if successful a message would be displayed saying welcome. Here are my codes.
TableREG.java
package registration;
import java.sql.*;
public class TableREG {
Connection con;
Statement stmt;
public TableREG() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:Table","","");
} catch (ClassNotFoundException e) {
System.out.println("Driver name error!");
} catch (SQLException e) {
System.out.println("Database cannot be found.");
}
}
public static boolean isExist(String usrname, String pswrd) {
try {
Connection con2 = DriverManager.getConnection("jdbc:odbc:Table","","");
Statement stmtTmp = con2.createStatement();
ResultSet res = stmtTmp.executeQuery("SELECT Username, Password FROM regTable");
while(res.next()) {
if ( (res.getString("Username")).equals(usrname) && (res.getString("Password").equals(pswrd)) )
return true;
}
} catch (SQLException sqlex){
System.out.println(sqlex.getMessage());
return false;
}
return false;
}
public void insertData(UserREG user) throws SQLException {
try {
stmt = con.createStatement();
stmt.executeUpdate("INSERT INTO regTable(Username, Password, FName, LName, Gender) values('"+user.getUsername()+"','"+user.getPassword()+"','"+user.getFName()+"','"+user.getLName()+"','"+user.getGender()+"')");
} catch (SQLException sqlex){
System.out.println(sqlex.getMessage());
}
}
public static ResultSet getRecord(String usrname, String pswrd) {
ResultSet res = null;
try {
Connection con2 = DriverManager.getConnection("jdbc:odbc:Table","","");
Statement stmt2 = con2.createStatement();
res = stmt2.executeQuery("SELECT * FROM regTable WHERE U='"+usrname+"' AND Password='"+pswrd+"'");
} catch (SQLException sqlex) {
System.out.println(sqlex.getMessage());
}
return res;
}
}
UserREG.java
package registration;
public class UserREG {
private String username;
private String password;
private String firstname;
private String lastname;
private String gender;
public UserREG(String usrname, String pswrd, String frstname, String lstname, String gnd) {
username = usrname;
password = pswrd;
firstname = frstname;
lastname = lstname;
gender = gnd;
}
public UserREG(String usrname, String pswrd) {
username = usrname;
password = pswrd;
}
public String getUsername() {
return username;
}
public void setUsername(String usrname) {
this.username = usrname;
}
public String getPassword() {
return password;
}
public void setPassword(String pswrd) {
this.password = pswrd;
}
public String getFName() {
return firstname;
}
public void setFName(String frstname) {
this.firstname = frstname;
}
public String getLName() {
return lastname;
}
public void setLName(String lstname) {
this.lastname = lstname;
}
public String getGender() {
return gender;
}
public void setGender(String gnd) {
this.gender = gnd;
}
}
registration.jsp
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h1 align="center"><b>Registration Form</b></h1><hr>
<form action="success.jsp" method="post">
<p>Username: <input type="text" name="user" size="20"></p>
<p>Password: <input type="password" name="pass1" size="20"></p>
<p>Re-type password: <input type="password" name="pass2" size="20"> </p>
<hr>
<p>First name: <input type="text" name="firstname" size="20"></p>
<p>Last name: <input type="text" name="lastname" size="20"></p>
<p>Gender: <input type="radio" name="gender" value="female">female
<input type="radio" name="gender" value="male">male</p>
<p><input type="reset" name="res" value="Reset">
<input type="submit" name="sub" value="Submit" ></p><hr>
</form>
</body>
</html>
success.jsp
<%@ page import="registration.*" %>
<html>
<head>
<title>Summary Page</title>
</head>
<body>
<%
String uname = request.getParameter("uname");
String pass1 = request.getParameter("pass1");
String pass2 = request.getParameter("pass2");
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String gend = request.getParameter("gend");
if (uname.equals("") || pass1.equals("") || pass2.equals("") || fname.equals("") || lname.equals("") || gend.equals("")) { %>
<b>ERROR: Please fill it out completely!!!</b>
<%@include file = "registration.jsp" %>
<% } else if (!(pass1.equals(pass2))){%>
<hr><b>ERROR!</b><br><br>
Both passwords must be the same.<br>
Please fill in the blanks again.<hr>
<% } else {
UserREG user= new UserREG(uname, pass2, fname, lname, gend);
TableREG connect= new TableREG();
connect.insertRec(user);
%>
<h1 align="center"><b>Success!</b></h1><hr><hr>
<p>USERNAME: <%= uname %></p>
<p>PASSWORD: <%= pass2 %></p><br>
<p>FIRST NAME: <%= fname %></p>
<p>LAST NAME: <%= lname %></p>
<p>GENDER: <%= gend %></p><hr><hr>
<p align="center"><b>You may now <a href="login.jsp">CLICK HERE</a> to log in. Thank you!</b></p>
<%} %>
</body>
</html>
login.jsp
<%@ page import="registration.*" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1 align="center"><b>Login Page</b></h1><hr>
<form action="error.jsp" method="post">
<p>Username: <input type="text" name="user" size="20">
<p>Password: <input type="password" name="pass2" size="20">
<p><input type="submit" value="login"></p>
</form><br>
<form action="registration.jsp" method="post">
<br>Click the button below to register if you haven't yet.<br><br>
<input type="submit" name="submit" value="Register">
</form>
</body>
</html>
error.jsp
<%@ page import="registration.*" %>
<html>
<head>
<title>Error Page</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
UserREG user = new UserREG(username, password);
TableREG connect = new TableREG();
connect.retrieveRec(user);
if(username.equals("") || password.equals("")){
%>
ERROR! Please enter both your username and password in the blanks.<br><br>
<%@include file = "login.jsp" %>
<% }else{ %>
<form action="registration.jsp" method="post">
Error! You have not yet registered.<br><br>
Click the button below to register.<br><br>
<input type="submit" name="submit" value="Register"><br>
</form>
<% } %>
</body>
</html>
last is the "logincheck.jsp" file. after editing my codes, i also don't know where to use this anymore. i also need help as to where to use this.
<%@ page language="java" import="java.sql.*" errorPage="" %>
<%
String error=request.getParameter("error");
if (error==null || error=="null") {
error="";
}
%>
<html>
<head>
<title>Login Check Page</title>
</head>
<body>
<%
Connection conn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn = DriverManager.getConnection("jdbc:odbc:Table","","");
ResultSet rsdoLogin = null;
PreparedStatement psdoLogin = null;
String usrname = request.getParameter("user");
String pswrd2 = request.getParameter("pass2");
String message = "You have logged in successfully.";
try {
String sqlOption="SELECT * FROM usermaster where" + " usrname=? and pswrd2=password(?) and sStatus='A'";
psdoLogin=conn.prepareStatement(sqlOption);
psdoLogin.setString(1,usrname);
psdoLogin.setString(2,pswrd2);
rsdoLogin=psdoLogin.executeQuery();
if(rsdoLogin.next()) {
String uname=rsdoLogin.getString("frstname")+" "+rsdoLogin.getString("lstname");
session.setAttribute("usrname",rsdoLogin.getString("usrname"));
session.setAttribute("iUserType",rsdoLogin.getString("iUserType"));
session.setAttribute("iUserLevel",rsdoLogin.getString("iUserLevel"));
session.setAttribute("uname", uname);
response.sendRedirect("exer03-wel.html?error="+message);
}
else {
message="Username and password do not match or cannot be found in the database." ;
response.sendRedirect("exer03-login.jsp?error="+message);
}
}
catch(Exception e) {
e.printStackTrace();
}
try {
if (psdoLogin!=null) {
psdoLogin.close();
}
if (rsdoLogin!=null) {
rsdoLogin.close();
}
if (conn!=null) {
conn.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
%>
</body>
</html>
For the code "connect.retrieveRec(user);", it also kept telling me that it isn't defined in either the "TableREG.java" or "UserREG.java".
I'm not sure sure if this is the right place to post this thread but since it's related to database, I decided to post it here. I would appreciate anyone that would help me with my problem. Thanks! =)