JSP+JDBC+XAMPP(MySQL)+NetBeans

JSP+JDBC+XAMPP(MySQL)+NetBeans

GitHub URL
Text
Tags
jsp
java
mysql
Live
Created
Aug 17, 2022 05:05 PM
  • Create a new project in NetBeans
  • Add my sql connector under the Libraries folder by clicking Add JAR/Folder
notion image
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
notion image
  • Create database and table of your choice
  • Then , in netbeans create database to get the connection link for your jsp file
    notion image

    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>
    index.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(); } %>
    login.jsp
    <!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>
    register.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(); } %>
    registrationProcess.jsp
    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..⚡