类没有找到异常com.mysql.jdbc.driver [英] class not found exception com.mysql.jdbc.driver

查看:297
本文介绍了类没有找到异常com.mysql.jdbc.driver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的文件结构是:

File structure of my project is:

         -src
            |
           -pkg
            |
             -CoreServlet.java(servlet)
             -Main.java
             -Core.java(jdbc code is here)

core.java类:

core.java class:

package com.pkg;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class core{
    private Connection connect = null;
    private Statement statement =null;
    private PreparedStatement preparedStatement = null;
    private ResultSet resultSet = null;
    String qwerty;

    public void readDataBase() {
        String userName = "ansh";
        String password = "12345";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/glbitm", userName,password);
            statement = connect.createStatement();
            resultSet = statement.executeQuery("select * from teachers");
            resultSet.next();      
            qwerty = resultSet.getString(1);

           } catch (Exception e) {
            System.out.println(e);
        }
    }

}

coreServlet。 java类:

coreServlet.java class :

   package com.pkg;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class coreServlet extends HttpServlet{ 
  /**
     * 
     */
    private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request, 
  HttpServletResponse response)
  throws ServletException,IOException{
        core dao = new core();
        dao.readDataBase();
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  pw.println("<html>");
  pw.println("<head><title>Hello World</title></head>");
  pw.println("<div>"+dao.qwerty+"</div>");
  pw.println("<body>");
  pw.println("<h1>Hello World</h1>");
  pw.println("</body></html>");
  }
}

当我访问 dao我的tomcat服务器中的 coreServlet.java 中的.qwerty 我得到类未找到异常com.mysql.jdbc.driver ,值 dao.qwerty 打印为null 。我在做错什么?

When I am accessing dao.qwerty in my coreServlet.java in my tomcat server. I am getting class not found exception com.mysql.jdbc.driver and value of dao.qwerty is printed as null. Where I am doing wrong ?

推荐答案

你没有设置 mysql-connector 在你的路径中。首先和其他一切都将完成

You have not set the mysql-connector in your path.Set that first and everything else will be done


什么是 JConnector

Java不知道SQL,所以要使用sql驱动程序执行的每个语句都将转换为sql结构返回我的mysql的结果也被转换成java可理解的结构。

Java does not know SQL so every statement you want to execute using the sql driver will get converted to sql constructs and the result that is returned my mysql is also converted into java understandable constructs.

希望它有助于

这篇关于类没有找到异常com.mysql.jdbc.driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆