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....

what error or exception you getting ...

arise NullPointerException

[B]HTTP Status 500 -[/B]

[B]type [/B]Exception report

[B]description [/B]The server encountered an internal error () that prevented it from fulfilling this request.

[B]exception[/B]

[I]java.lang.NullPointerException
	org.sms.user.UserManager.saveUser(UserManager.java:49)
	org.sms.user.AddUserServlet.doPost(AddUserServlet.java:33)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
[/I]
[B]note [/B]The full stack trace of the root cause is available in the Apache Tomcat/7.0.8 logs.

Anyone can help me to solve this.....

You should be connecting to MySQL database not to phpMyAdmin tool, which is just webalized GUI for your database. Use just plain localhost as con = DriverManager.getConnection("jdbc:mysql://localhost/bit5", "root", ""); or with MySQL default port number con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bit5", "root", "");

I tried in that way also....
result was same.not change even one character..

i heard before this we do not want to mention if we are use default..
So thats why i didn't mention that default port....

Anyway I tried to solve this in different way.. I install MySql server 5.0 & try to do it again.. Unfortunately result was same..


Error message mention it as NullPointerException

and also it mention there are some problem withUserManager.saveUser(UserManager.java:51) (highlighted line on UserManager class)

finally{
		try {
		 [B]	statement.close();[/B]
			connection.close();
				
			} catch (SQLException e) {
			 e.printStackTrace();
			}
		 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.