gpt4 book ai didi

java - 我的数据库连接 getter 不起作用/我无法解决这个问题?

转载 作者:行者123 更新时间:2023-11-30 05:39:03 25 4
gpt4 key购买 nike

本质上,我正在创建使用 MySQL 和 Java(通过 JDBC)的足球数据库软件。我已经创建了工作正常的数据库连接,但是当我尝试将其调用到另一个类时,它不起作用。 getter是公共(public)的,我的连接变量 'conn'也是公开的。我得到nullpointers当我在其他类中使用它时,在代码的 getter 行上,但是,当我测试 getter 时,它没有给我 nullpointer 。这是我正在测试的两个类 - 包括给我空指针的代码字符串:

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。我只收到空指针错误。

最佳答案

您正在初始化错误的conn引用。当你的代码中应该有一个对象时,却出现了两个对象。不要在本地声明 conn

关于java - 我的数据库连接 getter 不起作用/我无法解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56043673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com