JDBC和JavaEE

时间:2018-08-06 12:03:16

标签: java java-ee classnotfoundexception

我像这样在javaSE中创建了sipmle项目:

package com.company;

import java.sql.*;

public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
    final String driver = "com.mysql.jdbc.Driver";
    Class.forName(driver);

    final String dbPath = "jdbc:mysql://localhost:3306/world";

    Connection conn = DriverManager.getConnection(dbPath, "root", "root");

    Statement statement = conn.createStatement();
    final String sqlQuery = "SELECT Name, Population FROM city";
    ResultSet resultSet = statement.executeQuery(sqlQuery);

    String cityName = null;
    int cityPopulation = 0;
    while(resultSet.next()) {
        cityName = resultSet.getString("Name");
        cityPopulation = resultSet.getInt("Population");
        System.out.printf("%-30s %-10s\n",cityName, cityPopulation);
    }


    if(statement != null) {
        statement.close();
    }
    if(resultSet != null) {
        resultSet.close();
    }
    if(conn != null) {
        conn.close();
    }
}

我添加了mysql-connector-java-8.0.12.jar。 一切工作正常,但是当我想使JavaEE项目Intellij出现ClassNotFoundException

package com.company.jdbc.servlet;

import com.company.jdbc.data.City;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/SqlServlet")
public class SqlServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        String param = request.getParameter("get");
        if("show".equals(param)) {
            List<City> cityList = getCities();
            request.setAttribute("cityList", cityList);
            request.getRequestDispatcher("citylist.jsp").forward(request, response);
        } else {
            response.sendError(403);
        }
    }

    private List<City> getCities() {
        final String driver = "com.mysql.jdbc.Driver";
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        List<City> cityList = null;
        final String dbPath = "jdbc:mysql://localhost:3306/world";
        final String sqlQuery = "SELECT Name, Population FROM city";
        Connection conn = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            conn = DriverManager.getConnection(dbPath, "root", "root");
            statement = conn.createStatement();
            resultSet = statement.executeQuery(sqlQuery);

            String cityName = null;
            int cityPopulation = 0;
            cityList = new ArrayList<>();
            while (resultSet.next()) {
                cityName = resultSet.getString("Name");
                cityPopulation = resultSet.getInt("Population");
                City city = new City(cityName, cityPopulation);
                cityList.add(city);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return cityList;
    }

}

看起来servlet比标准类还有另一种连接数据库的方法...

我试图制作Maven项目并添加外部库,结果是JavaSE可以正常工作,而JavaEE却不能。

人们到处建议添加jar或对pom文件添加依赖项。

还有其他事情要做吗,或者我在此代码中有错误吗?

0 个答案:

没有答案