/*
* Node.java
* Node class defining methods for creating
* and editing nodes, as well as saving or
* retrieving them from the database. This
* class extends the DBObject class which
* deals with the nitty gritty of the
* database connections.
*
* To Do
* ---------------
* Filter input to remove possible command characters
*/
package base;
import java.sql.ResultSet;
public class Node extends DBObject{
//Node Values
private String label;
private int templateID;
//Create a node object from a specific
//node instance in the Database. i is the
//nodeID you want to retrieve
public Node(int i){
//Set our ID from user Input
id = i;
getConn();
//Query the database and get the result set
ResultSet rs = query("SELECT * FROM node WHERE NodeID = " + id + ";");
//Get the node information from the result set
try {
rs.next();
label = rs.getString("NodeLabel");
templateID = rs.getInt("TemplateID");
} catch(Exception e){e.printStackTrace();}
}
//Create a new node object to be placed in
//the database, get the label and template
//id from the user.
public Node(String l, int t){
label = l;
templateID = t;
//Build the MySQL Query
String query = "INSERT INTO node VALUES (NULL, '" +
label +
"', '" +
templateID +
"');";
//Connect to the Database
getConn();
//Execute the Query and set NodeID
id = create(query);
}
//Setters to make sure that the database
//is kept up to date when things are
//updated. This is probably really ineffecient
//when used in a batch mode, but I'm not quite
//sure how else I should do this.
public void setLabel(String l) {
label = l;
if (DBWrite){
String q = "UPDATE node SET NodeLabel = '" +
label +
"' WHERE NodeID = " +
id +
";";
query(q);
}
}
public void setTemplateID(int t) {
templateID = t;
if (DBWrite){
query("UPDATE node SET TemplateID = " +
templateID +
" WHERE NodeID = " +
"" + id +
";");
}
}
public String getLabel(){
if (DBWrite){
getConn();
ResultSet rs = query("SELECT NodeLabel FROM node WHERE NodeID = " + id + ";");
try {
rs.next();
label = rs.getString("NodeLabel");
}
catch (Exception e){
label = "Label Information Unavailable";
e.printStackTrace();
}
}
return label;
}
public int getTemplateID(){
if (DBWrite){
ResultSet rs = query("SELECT TemplateID FROM node WHERE NodeID = " + id + ";");
try {
rs.next();
templateID = rs.getInt("TemplateID");
}
catch (Exception e){
templateID = 0;
e.printStackTrace();
}
}
return templateID;
}
}
syntax highlighted by Code2HTML, v. 0.93pm6