连接客户端java程序来访问数据库

时间:2013-07-31 21:18:17

标签: java sql database eclipse ms-access

我正在使用java和访问数据库制作练习程序。 该程序是一个终极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驱动程序管理器]未找到数据源名称且未指定默认驱动程序

这是我的代码。数学在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) {
}

}

3 个答案:

答案 0 :(得分: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等)
  • 处理错误
  • 重新抛出异常
  • 和儿子在

答案 1 :(得分:1)

陈述......

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

......有两个问题:

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

  2. 没有名为Microsoft Access Driver (*.accdb)

  3. 的ODBC驱动程序

    请改为尝试:

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

答案 2 :(得分:0)

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