Coding an Application using JSP with JDBC in Java

Write a JSP Program to Read the Name using a Form and print the Name back to the User


  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
  <!DOCTYPE html>
  <html>
  <head>
  <meta charset="ISO-8859-1">
  <title>Welcome to my First JSP Application</title>
  </head>
  <body>
  <form action="myfirstapplication.jsp" method="post">
  <label for="inputname">Enter your Name</label>
  <input type="text" name="username" required><br>
  <input type="submit" name="submit" value="submit">
  </form>
  </body>
  </html>
  
  <% 
  String sub = request.getParameter("submit");
  if(sub!=null)
  {
    String username = request.getParameter("username");
    out.println("Welcome Mr "+ username);
  }
  %>  

Insert a record in to the Student Table in a Mysql Database using JSP
Insert Name, Password, Country and Mark into the Table.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome to my First JSP Application</title>
</head>
<body>
<form action="myfirstapplication.jsp" method="post">
<label for="inputname">Enter your Name</label>
<input type="text" name="username" required><br>
<label for="inputpass">Enter your Password</label>
<input type="text" name="password" required><br>
<label for="selectcountry">Select your Country</label>
<select name="country">
<option name="India" value="India">India</option>
<option name="US" value="US">US</option>
</select><br>
<label for="mark">Enter your Mark</label>
<input type="text" name="mark" required><br>
<input type="submit" name="submit" value="submit"><br>
</form>
</body>
</html>

<%!
class student
{
  private String username;
  private String password;
  private String country;
  private int mark;
  
  public student(String username,String password,String country,int mark)
  {
    this.username=username;
    this.password=password;
    this.country=country;
    this.mark=mark;
  }
  
  public boolean insertStudentRecord() throws SQLException
  {
    //connect to a database 
    dbmsConnection connect=null;
    PreparedStatement stmt=null;
    Connection con = null;
    boolean insert=false;
    try
    {
      connect = new dbmsConnection("jdbc:mysql://localhost:3306/vit","root","");
      con=connect.getConnection();
      String sql = "insert into student values (?,?,?,?);";
      stmt=con.prepareStatement(sql);
      stmt.setString(1, username);
      stmt.setString(2, password);
      stmt.setString(3, country);
      stmt.setInt(4, mark);
      int i= stmt.executeUpdate();
      if(i>0)
      {
          insert=true;
      }
      else
      {
        insert=false;
      }
    }
    catch(Exception e)
    {
      System.out.println(e.getMessage());
    }
    finally
    {
      connect.closeConnection(stmt, con);
    }
    
    return insert;
  }
}

class dbmsConnection
{
  String url;
  String username;
  String password;
  
  public dbmsConnection(String url,String username,String password)
  {
    this.url=url;
    this.username=username;
    this.password=password;
  }
  
  public Connection getConnection()
  {
    Connection con = null;
    try
    {
      Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
      con=DriverManager.getConnection(url,username,password);
    }
    catch(Exception e)
    {
      System.out.println(e.getMessage());
    }
    
    return con;
  }
  
  public void closeConnection(Statement stmt,Connection con) throws SQLException
  {
    stmt.close();
    con.close();
  }
}
%>

<% 
String sub = request.getParameter("submit");
if(sub!=null)
{
  String username = request.getParameter("username");
  String password= request.getParameter("password");
  String country = request.getParameter("country");
  String mark = request.getParameter("mark");
  if(username!=null && password !=null && country!=null && mark!=null)
  {
    student s = new student(username,password,country,Integer.parseInt(mark));
    boolean result=s.insertStudentRecord();
    if(result)
    {
      out.print("Record Inserted Successfully");
    }
    else
    {
      out.print("Damn it..Got some wierd error...");
    }
  }
}
%>