簡體   English   中英

無法連接到數據庫 java jsp,servlet

[英]Can't connect to database java jsp, servlet

我必須使用 netbeans 連接到數據庫(MySQL 工作台),我有文件 DBContext 到 dbcontext 文件來建立連接

DBcontext 連接成功

package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class DBContext {

    //USE BELOW METHOD FOR YOUR DATABASE CONNECTION FOR BOTH SINGLE AND MULTILPE SQL SERVER INSTANCE(s)
    //DO NOT EDIT THE BELOW METHOD, YOU MUST USE ONLY THIS ONE FOR YOUR DATABASE CONNECTION*/
    public Connection connection;

    public Connection getConnection() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
        String url = "jdbc:mysql://" + serverName + "/" + dbName;
        return DriverManager.getConnection(url, user, password);
    }

    //Insert your other code right after this comment
    //Change/update information of your database connection, DO NOT change name of instance variables in this class
    private final String serverName = "localhost";
    private final String dbName = "db_ite1";
    private final String user = "root";
    private final String password = "buiduclong"; // password here

    public static void main(String[] args) {
        // Test connection
        try {
            Connection test = new DBContext().getConnection();
            PreparedStatement testStatement = test.prepareStatement("SELECT * FROM db_ite1.blog;");
            System.out.println("suces");
        } catch (Exception e) {
            System.out.println("Fail");
        }
    }
}

我有一個 function 從數據庫中獲取數據,它可以工作並返回數據
        DAO dao = new DAO();
        List<User> list = dao.getAllUser();
        request.setAttribute("UserList", list);
        request.getRequestDispatcher("userlist.jsp").forward(request, response);

然后我在 servlet 中調用此 function 但它不返回任何數據,我的列表大小 = 0 即使我檢查了我的 getAllUser() function 在 servlet 中返回數據代碼
 DAO dao = new DAO(); List<User> list = dao.getAllUser(); request.setAttribute("UserList", list); request.getRequestDispatcher("userlist.jsp").forward(request, response);

我已修復它,我將此字符串添加到 DBContext 的 url "?useSSL=false" 並且它的工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM