JDBC
Inserting value into database
------------------------------------------
import [Link];
import [Link];
import [Link];
public class jdbc_ex {
void ins() //insert value in mysql database
try
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/database1","root","system");
PreparedStatement ps = [Link]("insert into tabl values(?)");
[Link](1, "college");
[Link]();
[Link]();
catch(Exception e)
[Link]("ins exception===>"+e);
}
public static void main(String args[])
new jdbc_ex().ins();
///////////////////////////////////////////
Retrieving values from database
--------------------------------------------
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class jdbc_ex {
void ret()
try
{
[Link]("[Link]");
Connection con =
[Link]("jdbc:mysql://localhost:3306/database1","root","system");
Statement st = [Link]();
ResultSet rs = [Link]("select * from tabl");
while([Link]())
String s2 = [Link](1);
[Link](s2+"\n=>");
[Link]();
catch(Exception e)
[Link]("retrieve exception===>"+e);
public static void main(String args[])
new jdbc_ex().ret();
/////////////////////////////////////////////////////////////////
Cookies and servlet
---------------------------
[Link]
-------------
<%@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 [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class cook_serv extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
String s1 = [Link]("t1");
String s2 = [Link]("t2");
Cookie ck=new Cookie(s1,s2);//creating cookie object
[Link](ck);//adding cookie in the response
RequestDispatcher requestDispatcher = [Link]("cook_suc.jsp");
[Link](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>
}