I want to create a login jsp form that connect with servlet and mysql for database. And I'm using netbeans and glassfishV2. Please help me to solve this problem.

Recommended Answers

All 6 Replies

Use a JDBC for connection and call this when connecting to the database. USe a servlet to take the details from the JSP then validate by querying the database by using some check class i.e. Boolean checkUser(username, password).
Return true if they exist and false if they do not. Here is an example JDBC -

package DatabaseConnection;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCConnector {
	String dbDriver = "your_db_connection";
	String userId="db_userId";   // individual user id
        String password="db_password"; // individual password
	String dbURL =
	"db_location"+userId+"&password="+password;

	private Connection dbCon;
	private DatabaseMetaData dbMetaData;
	private Statement stmt;

	public JDBCConnector() {
		super();
	}

	public boolean connect()
		throws
			ClassNotFoundException,
			SQLException,
			InstantiationException,
			IllegalAccessException {

				Class.forName(this.getDbDriver()).newInstance();
				dbCon = DriverManager.getConnection(this.getDbURL());
				return true;
	}

	public void close() throws SQLException {
		dbCon.close();
	}

	public void commit() throws SQLException {
		dbCon.commit();
	}

	public void setAutoCommit(boolean autocommit) throws SQLException {
		dbCon.setAutoCommit(autocommit);
	}

	public void rollback() throws SQLException {
		dbCon.rollback();
	}

	public PreparedStatement prepareStatement(String sql) throws SQLException {
		PreparedStatement s = dbCon.prepareStatement(sql);
		return s;
	}

	public Statement createStatement() throws SQLException {
		Statement s = dbCon.createStatement();
		return s;
	}

	public int executeUpdate(String s) throws SQLException {
		int count = stmt.executeUpdate(s);
		return count;
	}

	public ResultSet execSQL(StringBuffer sqlBuf) throws SQLException {
		Statement s = dbCon.createStatement();
		String sql = sqlBuf.toString();
		ResultSet rs = s.executeQuery(sql);
		return (rs == null) ? null : rs;
	}

	public String getDbDriver() {
		return this.dbDriver;
	}

	public void setDbDriver(String newValue) {
		this.dbDriver = newValue;
	}

	public String getDbURL() {
		return this.dbURL;
	}

	public void setDbURL(String newValue) {
		this.dbURL = newValue;
	}

	public ResultSet doQuery(StringBuffer qry) {
		Connection con = null;
		Statement stmt = null;

		// set variables to take in resultset values
		String returnedUserName = null;
		String returnedPassword = null;
		ResultSet rs = null;

		try {
			connect();
			rs = execSQL(qry);

		} catch (ClassNotFoundException e) {
			System.out.println("ClassNotFound: " + e.getMessage());
		} catch (SQLException e) {
			System.out.println("SQLException: " + e.getMessage());
		} catch (InstantiationException e) {
			System.out.println("InstantiationException: " + e.getMessage());
		} catch (IllegalAccessException e) {
			System.out.println("IllegalAccessException: " + e.getMessage());
		}
		return rs;
	}

}

To pardon_garden

ResultSet rs = s.executeQuery(sql);
return (rs == null) ? null : rs;

First of all, this: ResultSet rs = s.executeQuery(sql); never returns null

And also this is very "not inteligent": return (rs == null) ? null : rs; If it is null return null, if it is not null return itself???????
It is exactly the same as return rs; Also you do this: Statement s = dbCon.createStatement(); but I don't see where this instance can be closed?
The same goes for the "doQuery" method where you define 2 unnecessary variables even though you have global as well.

Peter_budo has created this amazing tutorial:
JSP database connectivity according to Model View Controller (MVC) Model 2
Why did you post this amateur code?

Because my amateur code still works.

By using the command db.close() the following method is invoked

public void close() throws SQLException {
		dbCon.close();
	}

and the connection is closed.

Because my amateur code still works.

By using the command db.close() the following method is invoked

public void close() throws SQLException {
		dbCon.close();
	}

and the connection is closed.

And the Statement? Where do you close it?
Also you have a Statement declared globally and inside the method I described you open one locally, and you don't have methods to close neither of them.

Also you do this:

public int executeUpdate(String s) throws SQLException {
int count = stmt.executeUpdate(s);
return count;
}

Where do you create the "stmt"? I am sorry but I didn't see it. I could be wrong.

I want to create a login jsp form that connect with servlet and mysql for database. And I'm using netbeans and glassfishV2. Please help me to solve this problem.

I am sorry for not contributing much to your question apart for my comments to pardon_garden code.
The link that I provided should be able to help you start. Also try Peter_budo's link

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.