English 中文(简体)
JDBC联线标的回报
原标题:JDBC connection object returns null in Servlet/Web

我 st着一个保守的问题——我的数据库连接正在翻开。 顺差部分是,同一数据库代码在另一个类别中进行罚款。 我对我的组合进行了双重检查,装上了亚行的驾驶员,并在Tomcat测试了这一配置,但该信封的连接却被搁置。

Type Exception Report

Message Cannot invoke "java.sql.Connection.prepareStatement(String)" because "connection" is null

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "connection" is null

单州Connection.java:

    public class SingletonConnection {
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/CATALOGUE";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";
    private static Connection connection;
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException | SQLException e) {
    
            e.printStackTrace();
        }
    }
    public static Connection getConnection() {
        return connection;
    }
}

ProduitdaoImp.java

    public class ProduitdaoImp implements IProduitdao {
    @Override
    public List<Produit> chercher(String mc) {
        Connection connection = SingletonConnection.getConnection();
    
        List<Produit> produits = new ArrayList<Produit>();
        try {
    
    PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM PRODUITS                                                                      WHERE DESIGNATION LIKE ?");
            preparedStatement.setString(1, "%" + mc + "%");
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                Produit p = new Produit();
                p.setId(rs.getInt("ID"));
                p.setPrix(rs.getDouble("PRIX"));
                p.setQuantite(rs.getLong("QUANTITE"));
                p.setDesignation(rs.getString("DESIGNATION"));
                produits.add(p);
    
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return produits;
    }  
}

ControleurServlet.java

public class ControleurServlet extends HttpServlet {
    public IProduitdao iProduitdao;
    
    @Override
    public void init() throws ServletException {
        iProduitdao = new ProduitdaoImp();
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws   ServletException, IOException {
        String path = request.getServletPath();
        if (path.equals("/index.do")) {
            request.getRequestDispatcher("Produits.jsp").forward(request, response);
    
        } else if (path.equals("/chercher.do")) {
            String mot = request.getParameter("motcle");
    
            ProduitModel produitModel = new ProduitModel();
    
            produitModel.setMotCle(mot);
    
            List<Produit> produits = iProduitdao.chercher(mot);
            produitModel.setProduits(produits);
            request.setAttribute("model", produitModel);
            request.getRequestDispatcher("Produits.jsp").forward(request, response);
    
        }
    
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

Produits.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Produits</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
</head>
<body>
<p></p>
<div>
    <div>
        <div > Recherche des produits</div>
        <div ></div>
        <form method="get" action="chercher.do">
        <label>Mot CLe</label>
            <input type="text" name="motcle"/>

        <button type="submit">Chercher</button>
        </form>
        <table>
            <tr>
            <th>ID</th><th>DESIGNATIONNs</th><th>PRIX</th><th>QUANTITE</th>
            </tr>
            <c:forEach items="${model.produits}" var="p">
                <tr>
                    <td>${p.id}</td>
                    <td>${p.designation}</td>
                    <td>${p.prix}</td>
                    <td>${p.quantite}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</div>
</body>
</html>
问题回答

https://en.m.wikipedia.org/wiki/Facial_tissue”rel=“nofollow noreferer”>facial tissue。 重新分类、使用和处置。 从未在校内外同时使用单一链接。 你的单一州做法是不明智的,注定要失败,难以混淆。

储存您的链接全权证书详情,如用户名、密码、服务器地址、港口号码等,均载于 rel=“nofollow noreferer”编码>。 标 标 标 题 Ask that castalt DataSourcebject for a new connection each time You app needs access to the database.

在学习时,你可以难以编码你的链接证书。 在真正的工作中,我们将这些细节外部化,以便能够改变这些细节,而不必再作改动。 使用JNDI,以检索证书为

多年来,装载JDBC驾驶员的<代码>Class.forName是不必要的。 JDBC 司机通过。 我怀疑你在亚行使用过过时和错误的教学方法。

学习更多,search Stack Overflow。 你们会发现很多信息,包括我撰写的全活例子。

Tip: Use static otherwisely. 它没有目标。 经常使用可能表明设计不好。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签