我的DatabaseConnection吸气剂不起作用/我不能解决这个问题吗?

时间:2019-05-08 15:02:06

标签: java

本质上,我正在创建使用MySQL和Java(通过JDBC)的Football数据库软件。我已经创建了可以正常工作的数据库连接,但是当我尝试将其调用到另一个类时却无法正常工作。 getter是公用的,而我的Connection变量'conn'也是公用的。当我在其他类中使用它时,我在代码的getter行上得到nullpointers,但是,当我测试getter时,却没有得到nullpointer。这是我正在测试的两个类-包括为我提供nullpointers的代码字符串:

CLASS 1

package ns00790_footballproject;
import java.sql.*;

public class DatabaseConnection {

    public Connection conn;
    public DatabaseConnection() {


        try{

            String url = "jdbc:mysql://localhost:3306/?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
            String user = "root";
            String password = "";
        // 1. Get a connection do database
        Connection conn = DriverManager.getConnection(url, user, password);
        System.out.println("Connected ok");
        // 2. Create a statement
        Statement st = conn.createStatement();

        st.execute("CREATE DATABASE IF NOT EXISTS FOOTBALL_LEAGUE;" );
        st.execute("USE FOOTBALL_LEAGUE");

        st.execute("CREATE TABLE TEAMS(" + 
                "     teamID INT (10) NOT NULL AUTO_INCREMENT," + 
                "     teamName VARCHAR (50)," + 
                "     homeWins INT," + 
                "     homeDraws INT," + 
                "     homeLoss  INT," + 
                "     awayWins INT," + 
                "     awayDraws INT," + 
                "     awayLoss INT," + 
                "     homeGoalsFor INT," + 
                "     homeGoalsAgainst INT," + 
                "     awayGoalsFor INT," + 
                "     awayGoalsAgainst INT," + 
                "     gamesPlayed INT," + 
                "     PRIMARY KEY (teamID)" + 
                "     );");

        st.execute(" CREATE TABLE GAME(" + 
                "    gameID INT (10) NOT NULL AUTO_INCREMENT," + 
                "    homeTeam VARCHAR(50)," + 
                "    teamID INT (10) NOT NULL," + 
                "    awayTeam VARCHAR(50)," + 
                "    homeScore INT," + 
                "    awayScore INT," + 
                "    PRIMARY KEY (gameID));");

        st.execute("CREATE TABLE DETAILS(" + 
                "   goalID INT NOT NULL AUTO_INCREMENT," + 
                "   gameID INT (10) NOT NULL," + 
                "   playerID INT (10)NOT NULL," + 
                "   PRIMARY KEY (goalID));");

        st.execute("CREATE TABLE PLAYERS(" + 
                "   playerID INT (10)NOT NULL AUTO_INCREMENT," + 
                "    teamID INT (10) NOT NULL," + 
                "   name VARCHAR(50)," + 
                "   goalsScored INT," + 
                "   penaltyCards INT," + 
                "   PRIMARY KEY (playerID));");

        st.execute("ALTER TABLE game ADD INDEX (`teamID`)");
        st.execute("ALTER TABLE `game` ADD FOREIGN KEY (`teamID`) REFERENCES `teams`(`teamID`) ON DELETE CASCADE ON UPDATE CASCADE;");

        st.execute("ALTER TABLE DETAILS ADD INDEX (`playerID`)");
        st.execute("ALTER TABLE `DETAILS` ADD FOREIGN KEY (`playerID`) REFERENCES `players`(`playerID`) ON DELETE CASCADE ON UPDATE CASCADE");

        st.execute("ALTER TABLE DETAILS ADD INDEX (`gameID`)");
        st.execute("ALTER TABLE `DETAILS` ADD FOREIGN KEY (`gameID`) REFERENCES `game`(`gameID`) ON DELETE CASCADE ON UPDATE CASCADE");

        st.execute("ALTER TABLE PLAYERS ADD INDEX (`teamID`)");
        st.execute("ALTER TABLE `PLAYERS` ADD FOREIGN KEY (`teamID`) REFERENCES `TEAMS` (`teamID`) ON DELETE CASCADE ON UPDATE CASCADE");



        }
        catch(Exception exc) {
            exc.printStackTrace();
        }
    }
    public Connection getConnection(){
        return conn;
    }
}

CLASS 2 :(无效的方法)

public void viewPlayerByTeam()  {

        try {

         conn.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = st.executeQuery("SELECT * FROM PLAYERS");

        rs.last();
        System.out.println("Connected");
        System.out.println(rs.getRow());


        }catch(SQLException e){
            System.out.println("SQL Error");
        }



    }

输出应该是表中当前的行数,即4。我仅收到空指针错误。

1 个答案:

答案 0 :(得分:1)

您正在初始化错误的conn参考。您的代码中应该有两个对象。不要在本地声明conn