简体   繁体   中英

How to do JDBC connection of Servlet with MySQL using NetBeans

When I run the project..Only Database result is printed and NO ROWS of table. I am to display when I do not use Servlet. Please help me with the code.

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      //  processRequest(request, response);

        String dr="com.mysql.jdbc.Driver";  
      String url="jdbc:mysql://localhost:3306/world";

      //  Database credentials
      String us = "root";
      String pwd = "root";

      // Set response content type
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String title = "Database Result";
      String docType =
        "<!doctype html public \"-//w3c//dtd html 4.0 " +
         "transitional//en\">\n";
         out.println(docType +
         "<html>\n" +
         "<head><title>" + title + "</title></head>\n" +
         "<body bgcolor=\"#f0f0f0\">\n" +
         "<h1 align=\"center\">" + title + "</h1>\n");
      try{

               // Register JDBC driver
               Class.forName("java.sql.Driver");


         // Open a connection
         Connection conn=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/world", "root", "root");

         // Execute SQL query
         Statement stmt = conn.createStatement();
         String sql;
         sql = "Select * from city LIMIT 0, 10";
         ResultSet rs = stmt.executeQuery(sql);

         // Extract data from result set
         while(rs.next()){
            //Retrieve by column name
            String d1=rs.getString("ID");
                String d2=rs.getString("Name");
                String d3=rs.getString("CountryCode");
                String d4=rs.getString("District");
                String d5=rs.getString("Population");
            //String last = rs.getString("last");

            //Display values
            out.println("ID: " + d1 + "<br>");
           // out.println(", Age: " + age + "<br>");
            out.println(", Name: " + d2 + "<br>");
         //   out.println(", Last: " + last + "<br>");
            out.println(", Code: " + d3 + "<br>");
            out.println(", District: " + d4 + "<br>");
            out.println(", Pop: " + d5 + "<br>");
         }
         out.println("</body></html>");

         // Clean-up environment
         rs.close();
         stmt.close();
         conn.close();
      }
catch(Exception se){
         //Handle errors for JDBC
         se.printStackTrace();}
    }

When I run the project..Only Database result is printed and NO ROWS of table. I am to display when I do not use Servlet. Please help me with the code.

Hope this will help you

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
          //  processRequest(request, response);


          String url="jdbc:mysql://localhost:3306/world";

          //  Database credentials
          String us = "root";
          String pwd = "root";

          // Set response content type
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          String title = "Database Result";
          String docType =
            "<!doctype html public \"-//w3c//dtd html 4.0 " +
             "transitional//en\">\n";
             out.println(docType +
             "<html>\n" +
             "<head><title>" + title + "</title></head>\n" +
             "<body bgcolor=\"#f0f0f0\">\n" +
             "<h1 align=\"center\">" + title + "</h1>\n");
          try{

                   // Register JDBC driver
                   Class.forName("com.mysql.jdbc.Driver");


             // Open a connection
             Connection conn=DriverManager.getConnection(url,us,pwd);

             // Execute SQL query
             Statement stmt = conn.createStatement();
             String sql;
             sql = "Select * from city LIMIT 10";
             ResultSet rs = stmt.executeQuery(sql);

             // Extract data from result set
             while(rs.next()){
                //Retrieve by column name
                String d1=rs.getString("ID");
                    String d2=rs.getString("Name");
                    String d3=rs.getString("CountryCode");
                    String d4=rs.getString("District");
                    String d5=rs.getString("Population");
                //String last = rs.getString("last");

                //Display values
                out.println("ID: " + d1 + "<br>");
               // out.println(", Age: " + age + "<br>");
                out.println(", Name: " + d2 + "<br>");
             //   out.println(", Last: " + last + "<br>");
                out.println(", Code: " + d3 + "<br>");
                out.println(", District: " + d4 + "<br>");
                out.println(", Pop: " + d5 + "<br>");
             }
             out.println("</body></html>");

             // Clean-up environment
             rs.close();
             stmt.close();
             conn.close();
          }
    catch(Exception se){
             //Handle errors for JDBC
             se.printStackTrace();}
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM