包括mysql-connector-bin.jar和mysql-jdbc.jar

时间:2017-06-12 07:43:55

标签: java mysql jdbc

在Spring工具套件中的动态Web项目中调试此servlet代码时。即使在输入mysql连接器和mysql jdbc jar之后,它也会在此行Class.forName("com.mysql.jdbc.Driver");抛出类未找到异常。请帮忙。

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class dbclassview
 */
public class dbclassview extends HttpServlet {
     private int name;
      private int pcaNo;
      private String ip;

      //getter and setter methods
      public void setName(int name)
      {
          this.name=name;
      }
      public void setPcaNo(int pcaNo)
      {
          this.pcaNo=pcaNo;
      }
      public void setIp(String ip)
      {
          this.ip=ip;
      }
      public int getName()
      {
          return name;
      }
      public int getPcaNo()
      {
          return pcaNo;
      }
      public String getip()
      {
          return ip;
      }
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public dbclassview() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         response.setContentType("text/html");

          PrintWriter out = response.getWriter();
          String title = "Using GET Method to Read Form Data";

          String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " +
             "transitional//en\">\n";
          List<dbclassview> list=new ArrayList();
             try
             {
                    Class.forName("com.mysql.jdbc.Driver");  
                    Connection con=DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/database_name?autoReconnect=true&useSSL=false","root","password");  
                    Statement stmt=con.createStatement(); 
                    //String sql = "Create table students(Student_ID Integer,Student_Name Varchar(20),Student_Age Integer)";
                    //stmt.executeUpdate(sql);
                    //stmt.executeUpdate("Insert into students(Student_ID,Student_Name,Student_Age) values (1,'sid',22)");
                    ResultSet rs=stmt.executeQuery("Select * from products");

              while (rs.next()) {
                 dbclassview type1=new dbclassview();
                 type1.setName(rs.getInt(1));
                 type1.setPcaNo(rs.getInt(2));
                 type1.setIp(rs.getString(3));
                 list.add(type1);
              }
              rs.close();
              con.close();

          }catch(Exception e)
             {
              System.out.println(e.getMessage());
             }
             finally
             {
                 for(int i=0;i<list.size();i++)
                 {
                     System.out.println(list.get(i));
                 }
             }
             out.println(docType + "<html>\n" +
                     "<head><title>" + title + "</title></head>\n" +
                     "<body bgcolor=\"#f0f0f0\">\n" +
                     "<h1 align=\"center\">" + title + "</h1>\n" +
                     "<ul>\n" +
                     "  <li><b>First Name</b>: "
                     + list + "\n" +
                     "  <li><b>Last Name</b>: "
                      + "\n" +
                     "</ul>\n" +
                     "</body></html>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

0 个答案:

没有答案