0% found this document useful (0 votes)
34 views7 pages

Practical

The document provides Java code examples for inserting and retrieving values from a MySQL database using JDBC, as well as creating and handling cookies in a servlet environment. It includes a JSP form for user input and servlet code to process the cookie creation. Error handling is implemented in both database operations and servlet processing.

Uploaded by

mariyappan s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Practical

The document provides Java code examples for inserting and retrieving values from a MySQL database using JDBC, as well as creating and handling cookies in a servlet environment. It includes a JSP form for user input and servlet code to process the cookie creation. Error handling is implemented in both database operations and servlet processing.

Uploaded by

mariyappan s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JDBC

Inserting value into database

------------------------------------------

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

public class jdbc_ex {

void ins() //insert value in mysql database

try

Class.forName("com.mysql.jdbc.Driver");

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/database1","root","system");

PreparedStatement ps = con.prepareStatement("insert into tabl values(?)");

ps.setString(1, "college");

ps.executeUpdate();

con.close();

catch(Exception e)

System.out.println("ins exception===>"+e);

}
public static void main(String args[])

new jdbc_ex().ins();

///////////////////////////////////////////

Retrieving values from database

--------------------------------------------

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

public class jdbc_ex {

void ret()

try

{
Class.forName("com.mysql.jdbc.Driver");

Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/database1","root","system");

Statement st = con.createStatement();

ResultSet rs = st.executeQuery("select * from tabl");

while(rs.next())

String s2 = rs.getString(1);

System.out.println(s2+"\n=>");

con.close();

catch(Exception e)

System.out.println("retrieve exception===>"+e);

public static void main(String args[])

new jdbc_ex().ret();

/////////////////////////////////////////////////////////////////
Cookies and servlet

---------------------------

Home.jsp

-------------

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<form action="cook_serv">

<h1>enter your own cookie</h1>

<br> <input type="text" name="t1" value="" />

<br> <input type="text" name="t2" value="" />

<input type="submit" value="create cookie" />

</form>

</body>

</html>

Cook_suc.jsp

----------------------

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h1>cookie successfully created check your browser </h1>

</body>

</html>

Cook_serv.java // servlet

------------------------------------

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class cook_serv extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {

String s1 = request.getParameter("t1");

String s2 = request.getParameter("t2");

Cookie ck=new Cookie(s1,s2);//creating cookie object

response.addCookie(ck);//adding cookie in the response

RequestDispatcher requestDispatcher = request.getRequestDispatcher("cook_suc.jsp");

requestDispatcher.forward(request, response);

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left


to edit the code.">

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

@Override

public String getServletInfo() {

return "Short description";

}// </editor-fold>
}

You might also like