I've recently been trying to write to a Database by taking user input from a GUI and I would like to store that user input from the GUI in a derby database on netbeans. At the moment, I have got some code but it does not seem to be functional as when i run the GUI and start inputting the data that is supposed to be stored in the back end of my program in the database, it does not store that data which is being inputted in the GUI. It does not load any data onto the database!
Here is the code for writing to the Database:
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author Hammad
*/
public class WritePlayerDB
{
private final DBConnection dbc;
private final Connection conn;
private Statement stmt;
String insertQuery;
public WritePlayerDB()
{
dbc = new DBConnection();
conn = dbc.getConnection();
}
public void writeToPlayerDB()
{
try {
int playerID = 0;
insertQuery = "INSERT INTO 'PLAYERINFO' ('PLAYER_ID', 'PLAYER_NAME', 'PRIZE_WON', 'CORRECT_ANSWERS')" + "VALUES(1,2,3,4)";
this.stmt = conn.createStatement();
this.checkExistedTable("PLAYERS");
PreparedStatement ps = conn.prepareStatement(insertQuery);
CorrectAnswers.correctAnswers[MillionaireGui.moneyCounter] = PrizeMoney.prizeLadder[MillionaireGui.moneyCounter];
ps.setInt(1, playerID++);
ps.setString(2, MillionaireGui.nameField.getText());
ps.setInt(3, PrizeMoney.prizeLadder[MillionaireGui.moneyCounter]);
ps.setInt(4, CorrectAnswers.correctAnswers[MillionaireGui.moneyCounter]);
ps.executeUpdate();
ps.close();
}
catch (SQLException ex)
{
System.out.println(ex.getMessage());
}
}
public void checkExistedTable(String name)
{
try {
DatabaseMetaData dbmd = this.conn.getMetaData();
String[] types = {"TABLE"};
stmt = this.conn.createStatement();
ResultSet rs = dbmd.getTables(null, null, null, types);
while (rs.next()) {
String table_name = rs.getString("TABLE_NAME");
System.out.println(table_name);
if (table_name.equalsIgnoreCase(name)) {
stmt.executeUpdate("Drop table " + name);
System.out.println("Table " + name + " has been deleted.");
break;
}
}
rs.close();
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
}
and I am calling the writeToPlayerDB()
method in the actionListener for my GUI's JTextField as shown below:
nameField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (nameField.getText().equals(""))
{
blankName.setVisible(true);
blankName.setText("PLEASE DON'T LEAVE NAME BLANK");
}
else
{
playerName.setPlayerName(nameField.getText().toUpperCase());
WritePlayerDB wdb = new WritePlayerDB();
wdb.writeToPlayerDB();
blankNamePanel.setVisible(false);
introRuleScreen();
}
}
});
Would appreciate if i could get a hand with this as I have been stuck on it for a long time!
MillionaireGui.nameField
makes me think you're usingstatic
properties, which is a bad idea,writeToPlayerDB
should taking values from the UI via method parameters – MadProgrammerstatic
in this context, it's not helping you – MadProgrammer