1.User.jsp
<form action="register" method="post">
First Name : <input type="text" name="firstName" /> <br />
Last Name : <input type="text" name="lastName" /> <br />
Email : <input type="text" name="email" /> <br />
Username : <input type="text" name="uName" /> <br />
Password : <input type="password" name="password" /> <br />
<input type="submit" value="Register" />
</form>
----------------------------------------------------------------------------------------
2.AddUserServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddUserServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
String userName = request.getParameter("uName");
String password = request.getParameter("password");
//create user
User user = new User();
user.setfName(fName);
user.setLastName(lastName);
user.setEmail(email);
user.setUsername(userName);
user.setPassword(password);
//save user
UserManager manager = new UserManager();
manager.saveUser(user);
}
}
--------------------------------------------------------------------------------------------------
3.UserManager.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLException;
import org.sms.db.DBConnectionManager;
public class UserManager {
public void saveUser(User user) {
// Create a Database connection
Connection connection = DBConnectionManager.getConnection();
PreparedStatement statement = null ;
// Create statement
try {
statement= connection.prepareStatement("insert into user('f_name','l_name','username','pass','email') values (?,?,?,?,?) ;");
statement.setString(1,user.getfName());
statement.setString(2,user.getLastName());
statement.setString(3,user.getUsername());
statement.setString(4,user.getPassword());
statement.setString(5,user.getEmail());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------------------
4.DBConnectionManager.java
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBConnectionManager {
public DBConnectionManager() {
}
public static Connection getConnection() {
Connection con = null;
// Load Driver
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Get the main Connection
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:9999/bit5", "root", "");
}
catch (SQLException e) {
e.printStackTrace();
}
return con;
}
--------------------------------------------------------------------------------------------------
5.User.java
public class User {
private Long userId;
private String fName;
private String lastName;
private String email;
private String username;
private String password;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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;
}
}
--------------------------------------------------------------------------------------------------
This is part of my simple project.....
Can someone help me to slove this.. Code is not working...
there can be simple mistake...plz..show me where that i was made mistake....
I use WAMP phpMyAdmin on localhost on port 9999
Thank You, if you consider this....