-1

I have wrote a method called "Get_connection" to connect SQL server with my Servlet and i can't use "out.println();" method inside this method even though i passed http servlet Responce as parameters..

Could anyone please explain how to rectify..

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
   response.setContentType("text/html;charset=UTF-8");
   try (PrintWriter out = response.getWriter()) {

   }
 }

 public Connection Get_connection() {
   try {
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     String url = "jdbc:sqlserver://localhost:1433; databaseName = Colombo_Health; integratedSecurity=true;";
     con = DriverManager.getConnection(url, "", "");
     out.println("Connection Established");
   } catch (ClassNotFoundException e) {

     out.println("Class not Found       " + e.toString());
     e.toString();

   } catch (Exception e) {
     out.println("Driver not Found    " + e.toString());
     e.toString();
   }

   return con;

 }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You obviously have to pass `out` as parameter to the method `GET_connection` which by the way is never called from `processRequest`. – thokuest Jan 28 '16 at 06:09
  • To FIx the Change the method like this *public Connection Get_connection(PrintWriter out)* – soorapadman Jan 28 '16 at 07:26

1 Answers1

1

Your Get_connection method should have below signature.

public Connection Get_connection(PrintWriter out) { }

call this from processRequest method's try block as Get_connection(out);

Also in processRequest method your try should follow a catch block or finally. Cheers.

DoctorAV
  • 1,189
  • 1
  • 14
  • 40