Advertisement
Guest User

Untitled

a guest
May 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.57 KB | None | 0 0
  1. package com.servlet.test;
  2.  
  3. import java.io.*;
  4. import java.sql.*;
  5.  
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8.  
  9. public class Connect extends HttpServlet
  10.   {
  11.   public Connection con;
  12.   public PrintWriter out;
  13.  
  14.   public void init(ServletConfig conf) throws ServletException
  15.     {
  16.     super.init(conf);
  17.     try
  18.       {
  19.         Class.forName("com.mysql.jdbc.Driver");
  20.        
  21.  
  22.         String url = "jdbc:mysql://localhost/mybd";
  23.  
  24.        
  25.  
  26.  
  27.         con = DriverManager.getConnection (url,"root", "root");
  28.      
  29.       }
  30.     catch(Exception e)
  31.       {
  32.       System.out.println(e);
  33.       }
  34.     }
  35.  
  36.   public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  37.     {
  38.     res.setContentType("text/html");
  39.     try
  40.       {
  41.       out = res.getWriter();
  42.       out.println("<html><head><title>");
  43.       out.println("JDBC Servlet");
  44.       out.println("</title></head><body>");
  45.      
  46.       Statement stmt = con.createStatement();
  47.       ResultSet rs = stmt.executeQuery("SELECT * FROM node");
  48.       out.println("<UL>");
  49.  
  50.       while(rs.next())
  51.         {
  52.         out.println("<LI>" + rs.getString("id"));
  53.         }
  54.       out.println("</UL>");
  55.       rs.close();
  56.       stmt.close();
  57.       }
  58.     catch(SQLException e)
  59.       {
  60.       out.println("Exception SQL");
  61.       }
  62.     catch(IOException e)
  63.       {
  64.       }
  65.  
  66.     out.println("</body></html>");
  67.     out.close();
  68.     }
  69.  
  70.   public void destroy()
  71.     {
  72.     try
  73.       {
  74.       con.close();
  75.       }
  76.     catch(SQLException e)
  77.       {
  78.         ;
  79.       }
  80.     }
  81.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement