Pages

Monday 28 October 2019

ServletDatabase Connection

importjava.io.*;
importjava.sql.*;
importjavax.servlet.*;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.*;
@WebServlet("/IT2017/DBConnReuse")//XMLfilenotrequired
publicclassDBConnReuseextendsHttpServlet{
privateConnectioncon=null;
publicvoidinit(ServletConfigconfig)throwsServletException
{super.init(config);
try{Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
}catch(Exceptione){System.out.println(e);}}


publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException
{response.setContentType("text/html");
PrintWriterout=response.getWriter();
out.println("<html><body><h1>StudentDetails.</h1>");
Statementstmt;
try{stmt=con.createStatement();
ResultSetrs=stmt.executeQuery("Select*fromstud");
out.println("<html><body>");
while(rs.next())
out.println(rs.getString(1)+""+rs.getString(2));
out.println("</body></html>");
}catch(SQLExceptione){out.println(e);}
}
publicvoiddestroy()
{try{
if(con!=null)con.close();
}catch(Exceptione){System.out.println(e);}
}
}

No comments:

Post a Comment