So I have a Dynamic Web Project that queries a MySQL database and returns employee information based on user demand.
Ex: htp://localhost:8080/Employees/123 //would return info on employee 123 htp://localhost:8080/Employees //would return info on all employees
Thing is, if the database gets updated and someone asks for a newly inserted employee the program is going to throw a null pointer. Is there anyway to notify or check for updates only after there is a change in the database?
This is the only relevant code concerning mySQL, the rest does it from the the two hashmaps I'm using.
package resources;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.HashMap;
import pojo.Party;
public class DBConnection {
private Connection con;
private static Statement statement;
private static ResultSet resultSet;
public static DBConnection connection;
private static ResultSetMetaData meta;
private static HashMap<String,Party> map;
public Party party;
private DBConnection()
{
try
{
map = new HashMap<String,Party>();
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(//not relevant, assume this works);
statement = con.createStatement();
readData();
}
catch (Exception e)
{
System.out.print("Error: "+e);
}
}
public void readData()
{
try
{
String query = "(SELECT * FROM PureServlet)";
resultSet = statement.executeQuery(query);
meta = resultSet.getMetaData();
String columnName, value, partyName;
while(resultSet.next())
{
partyName = resultSet.getString("PARTY_NAME");
map.put(partyName, new Party()); //this is the map that keeps track of all parties
party = map.get(partyName);
//getColumn...() irritatingly starts at 1 and not 0 thus j=1
for(int j=1;j<=meta.getColumnCount();j++)
{
columnName = meta.getColumnLabel(j);
value = resultSet.getString(columnName);
party.getPartyInfo().put(columnName, value); //this is the hashmap within the party that keeps
//track of the individual values. The column Name = label, value is the value
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}
public static HashMap<String,Party> getPartyCollection()
{
if(connection == null)
{
connection = new DBConnection();
}
return map;
}
}