0

我正在使用 java 和 access 数据库制作一个练习程序。该程序是一个终极的tictactoe板,数据库用于跟踪玩家的姓名和他们的分数。我遇到的麻烦是我不断收到这些错误。

Exception in thread "main" java.lang.NullPointerException
at AccessDatabaseConnection.getName(AccessDatabaseConnection.java:39)
at ultimate.<init>(ultimate.java:39)
at ultimate.main(ultimate.java:82)

通过进一步研究,我还发现:[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

这是我的代码。sql 语句中的数学有点未完成,但我还不是很担心。我需要在程序和数据库之间建立这种连接。

这是连接到 accessdatabaseconnections 类的程序的构造函数中的代码区域:

  AccessDatabaseConnection DB = new AccessDatabaseConnection();

  Font f = new Font("Dialog", Font.BOLD, 80);

  public ultimate() {
    super("Testing Buttons");

    String dbname = DB.getName();
    String wins = DB.getWins();
    String losses = DB.getLosses();

    Container container = getContentPane();
    container.setLayout(null);
    ButtonHandler handler = new ButtonHandler();

    for (int j = 0; j < 9; j++) {// set the rows
        x = 10;

        for (int i = 0; i < 9; i++) {// set the columns
            button[j][i] = new JButton();
            container.add(button[j][i]);
            button[j][i].setName(Integer.toString(j) + "_"
                    + Integer.toString(i));
            button[j][i].addActionListener(handler);
            button[j][i].setSize(100, 100);
            button[j][i].setVisible(true);
            button[j][i].setFont(f);
            button[j][i].setText(null);
            if ((i > 2 && j < 3 && i < 6) || (j > 2 && j < 6 && i  < 3)
                    || (j > 2 && j < 6 && i < 9 && i > 5)
                    || (j > 5 && j < 9 && i < 6 && i > 2)) {
                button[j][i].setBackground(Color.LIGHT_GRAY);
            } else {
                button[j][i].setBackground(Color.WHITE);
            }

            button[j][i].setLocation(x, y);
            x = x + 110;

        }

        y = y + 110;
    }
    setSize(1024, 1050);
    setVisible(true);
    container.setBackground(Color.BLACK);

}

public static void main(String args[]) {
    ultimate application = new ultimate();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    PlayerOne = JOptionPane.showInputDialog("Player 1: Enter Your Name");
    PlayerTwo = JOptionPane.showInputDialog("Player 2: Enter Your Name");

    while(PlayerOne == PlayerTwo){
        PlayerTwo = JOptionPane.showInputDialog("Player 2: Re-Enter Your Name (Cannot be the same!)");
    }
}

这是访问数据库的代码:

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.logging.Level;
 import java.util.logging.Logger;

 public class AccessDatabaseConnection {

public static Connection connect() {
    Connection con;
    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String database ="jdbc:odbc:Driver{Microsoft Access Driver (*.accdb)};DBQ=C:\\Users\\McKenzieC\\Documents\\tictactoeRecords.accdb;";
        con = DriverManager.getConnection(database, "", "");
    } catch (Exception ex) {
        return null;
    }
    return con;
}

public void addData(String nameOne, int win, String nameTwo,int loss){
    try {
    Statement stmt = connect().createStatement();
    stmt.executeQuery("INSERT INTO t_Records (Name, Wins) " +
            "VALUES (" + nameOne + ", " + Integer.toString(win));

    /*stmt.executeQuery("INSERT INTO t_Records (Name, Wins) " +
            "VALUES (" + nameTwo + ", " + Integer.toString(loss));
     + ", " + Integer.toString(loss)*/
    }
 catch (SQLException ex) {
}
}

public String getName() {
    try {
        Statement stmt = connect().createStatement();
        ResultSet rset = stmt.executeQuery("SELECT * FROM t_Records");
        if (rset.next()) {
            String name = rset.getString("Name");
            return name;
        }
    } catch (SQLException ex) {
    }
    return null;
}

public String getWins() {
    try {
        Statement stmt = connect().createStatement();
        ResultSet rset = stmt.executeQuery("SELECT * FROM t_Records");
        if (rset.next()) {
            String wins = rset.getString("Wins");
            return wins;
        }
    } catch (SQLException ex) {
    }
    return null;
}

public String getLosses() {
    try {
        Statement stmt = connect().createStatement();
        ResultSet rset = stmt.executeQuery("SELECT * FROM t_Records");
        if (rset.next()) {
            String losses = rset.getString("Losses");
            return losses;
        }
    } catch (SQLException ex) {

    }
    return null;
}

public static void main(String[] args) {
}

}

4

3 回答 3

3

我假设您看不到真正的错误,因为您隐藏了真正的错误:

永远不要这样做:

catch (Exception ex) {
    return null;
}

您至少可以对此进行更改(同样不推荐,但比上面的代码更好):

catch (Exception ex) {
    ex.printStackTrace();
    return null;
}
//After this change the program will fail again but you will got a better error message

但是您始终必须管理异常:

  • 打印错误信息
  • 放一条日志消息(java logging、log4j等)
  • 处理错误
  • 重新抛出异常
  • 和儿子
于 2013-07-31T21:23:51.283 回答
1

该声明...

String database ="jdbc:odbc:Driver{Microsoft Access Driver (*.accdb)};DBQ=C:\\Users\\McKenzieC\\Documents\\tictactoeRecords.accdb;";

...有两个问题:

  1. 您在关键字后缺少等号 ( =) 。Driver

  2. 没有名为 的 ODBC 驱动程序Microsoft Access Driver (*.accdb)

试试这个:

String database ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\McKenzieC\\Documents\\tictactoeRecords.accdb;";
于 2013-07-31T22:32:21.403 回答
0

之后是否为Statement stmtconnect().createStatement();?如果是,stmt.executeQuery则将导致 null ptr 异常。

于 2013-07-31T21:24:21.033 回答