0

我得到了这个异常,但只是在第一次运行这个程序时。当我重新加载页面时,一切都按预期工作。如何修复我的代码以避免页面加载错误?我最近开始学习 Java,这是我的实践项目之一。

手动加载驱动程序时,另一个错误说:

Loading class com.mysql.jdbc.Driver. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

这是返回与服务的连接的连接:

package com.gmail.stefan.backend.dbservices;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class TestDBConnection {

    public static Connection getConnection() throws SQLException   {

        System.out.println("Connecting...");
        Connection con = DriverManager.getConnection("jdbc:mysql://42.5.63.191:2204/tod?useUnicode=true&characterEncoding=utf8?autoReconnect=true&useSSL=false", "admin", "admin");
        System.out.println("Connection succsessful!");      

        System.out.println("Connection still open. ");

        return con;
    }
}

服务:

package com.gmail.stefan.ui.views.login;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.gmail.stefan.backend.Author;
import com.gmail.stefan.backend.Message;
import com.gmail.stefan.backend.dbservices.TestDBConnection;
import com.gmail.stefan.ui.views.login.LogoutTest;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.data.provider.DataProvider;

public class Service extends TestDBConnection{                                                              

    public static List<Author> selectAllAuthors() throws SQLException {

        try {

            Connection con = TestDBConnection.getConnection();
            String query = "select * from author";

            Statement st = null;

            st = con.createStatement();

            ResultSet rs = st.executeQuery(query);

            List<Author> list = new ArrayList<>();

            while (rs.next()) {
                String name = "";
                name = rs.getString("firstname");
                Author x = new Author(null, name, null);

                System.out.println(name);
                list.add(x);

            }

            st.close();
            con.close();
            System.out.println("First connection closed!");

            return list;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        // cannot return null
        return new ArrayList<Author>();

    }

    public static List<Author> selectFromAuthors() throws SQLException {
        try {
            Connection con = TestDBConnection.getConnection();
            String authorQuery = "select * from author"; 
            Statement ast = null;
            ast = con.createStatement();
            ResultSet ars = ast.executeQuery(authorQuery);
            List<Author> aList = new ArrayList<>();
            while (ars.next()) {
                int id; 
                String name = "";
                Timestamp createdon = null;

                id = ars.getInt(1);
                long uId = (long) id;
                name = ars.getString(2);
                createdon = ars.getTimestamp(3);
                Date date = new Date(createdon.getTime());

                Author b = new Author (uId, name, date);
                System.out.println(uId+" "+name+" "+createdon);
                aList.add(b);
            }
            ast.close();
            con.close();
            System.out.println("Second connection closed");

            return aList;
            }
        catch (SQLException s) {
            s.printStackTrace();

        }
        return null;
    }
}
4

0 回答 0