- Create a new project in NetBeans
- Add my sql connector under the Libraries folder by clicking Add JAR/Folder
download the mysql connector jar file (Iโm using 8.0.17 version)
- Add this jar file to the Tomcat/lib folder (to avoid 500 HTTP code errors in JSP file)
- Then , create a database in by initializing the xampp server by starting the MySql
- Create database and table of your choice
- Then , in netbeans create database to get the connection link for your jsp file
All Set good to go!!๐
ย
Now , create html or jsp files for your project on your own to connect JSPโJDBC(MySql)
To start off letโs create registration forms and make entries and store it in the database for demo to check whether everything is working properly
ย
Eg:
<!DOCTYPE html> <html> <head> <title>Login Page</title> </head> <body> <form action="login.jsp" method="post"> User name :<input type="text" name="userid" /><br><br> password :<input type="password" name="password" /><br> <br><input type="submit" /> </form> <p> New user. <a href="register.html">Register Here</a> </body> </html>
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page import="java.sql.,java.util."%> <% String userid = request.getParameter("userid"); session.putValue("userid", userid); String password = request.getParameter("password"); Class.forName("com.mysql.cj.jdbc.Driver"); java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsplogin", "root", ""); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from userdata where username='" + userid + "' and password='" + password + "'"); try { rs.next(); if (rs.getString("password").equals(password) && rs.getString("username").equals(userid)) { out.println("<h2>Welcome " + userid+ "</h2>"); } else { out.println("Invalid password or username."); } } catch (Exception e) { e.printStackTrace(); } %>
<!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <form action="registrationProcess.jsp" method="post"> User name :<input type="text" name="userid" /><br><br> password :<input type="password" name="password" /> <br><br><input type="submit" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.sql.,java.util."%> <% String userid = request.getParameter("userid"); String password = request.getParameter("password"); try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsplogin", "root", ""); Statement st = conn.createStatement(); int i = st.executeUpdate("insert into userdata(username,password)values('" + userid + "','" + password + "')"); out.println("Thank you for register ! Please <a href='index.html'>Login</a> to continue."); } catch (Exception e) { System.out.print(e); e.printStackTrace(); } %>
keep the file names as it is as they invoked inside the code suits.If you wish to change the file name donโt forget to update the file names inside the code blocks
ย
Once all set, now run the file from index.html page .VOILA!!๐ Everything works perfect..โก