Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.servlet.test;
- import java.io.*;
- import java.sql.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class Connect extends HttpServlet
- {
- public Connection con;
- public PrintWriter out;
- public void init(ServletConfig conf) throws ServletException
- {
- super.init(conf);
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost/mybd";
- con = DriverManager.getConnection (url,"root", "root");
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
- {
- res.setContentType("text/html");
- try
- {
- out = res.getWriter();
- out.println("<html><head><title>");
- out.println("JDBC Servlet");
- out.println("</title></head><body>");
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("SELECT * FROM node");
- out.println("<UL>");
- while(rs.next())
- {
- out.println("<LI>" + rs.getString("id"));
- }
- out.println("</UL>");
- rs.close();
- stmt.close();
- }
- catch(SQLException e)
- {
- out.println("Exception SQL");
- }
- catch(IOException e)
- {
- }
- out.println("</body></html>");
- out.close();
- }
- public void destroy()
- {
- try
- {
- con.close();
- }
- catch(SQLException e)
- {
- ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement