sql - connect client side java program to access database -


i making practice program using java , access database. program ultimate tictactoe board , databse meant keeping track of names of players , scores. trouble having keep getting these errors.

exception in thread "main" java.lang.nullpointerexception @ accessdatabaseconnection.getname(accessdatabaseconnection.java:39) @ ultimate.<init>(ultimate.java:39) @ ultimate.main(ultimate.java:82) 

with further research found this: [microsoft][odbc driver manager] data source name not found , no default driver specified

here code. math little unfinished in sql statements im not worried yet. need connection between program , database.

here area of code in constructor program connects accessdatabaseconnections class:

  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();      (int j = 0; j < 9; j++) {// set rows         x = 10;          (int = 0; < 9; i++) {// set 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 && < 6) || (j > 2 && j < 6 &&  < 3)                     || (j > 2 && j < 6 && < 9 && > 5)                     || (j > 5 && j < 9 && < 6 && > 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 name");     playertwo = joptionpane.showinputdialog("player 2: enter name");      while(playerone == playertwo){         playertwo = joptionpane.showinputdialog("player 2: re-enter name (cannot same!)");     } } 

and here code accessing database:

 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 t_records (name, wins) " +             "values (" + nameone + ", " + integer.tostring(win));      /*stmt.executequery("insert 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 * 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 * 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 * t_records");         if (rset.next()) {             string losses = rset.getstring("losses");             return losses;         }     } catch (sqlexception ex) {      }     return null; }  public static void main(string[] args) { } 

}

i assume can't see real error because you're hiding real error:

never this:

catch (exception ex) {     return null; } 

you can change @ least (again not recommended better above code):

catch (exception ex) {     ex.printstacktrace();     return null; } //after change program fail again got better error message 

but must manage exception:

  • print error message
  • put log message (java logging, log4j , on)
  • deal error
  • re-throw exception
  • and son on

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -