import java.sql.*; import javax.sql.*; import java.util.*; public class StageObject { //Stores information about all Stage Object in Array Lists. private ArrayList ids; private ArrayList names; private ArrayList descriptions; private ArrayList locations; public StageObject(){ ids = new ArrayList(); names = new ArrayList(); descriptions = new ArrayList(); locations = new ArrayList(); //Connecting to the Mysql Database. Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); //Mysql Java Class Path must be defined. String url = "jdbc:mysql://localhost:3306/kinect"; //kinect is the name of the database conn = DriverManager.getConnection(url, "root",""); Statement st = conn.createStatement(); //Storing information from database to Array Lists. ResultSet rs = st.executeQuery("select * from stage_objects"); while (rs.next()) { ids.add(rs.getInt("stage_object_id")); names.add(rs.getString("name")); descriptions.add(rs.getString("description")); locations.add(rs.getString("location")); } } catch (Exception e) { e.printStackTrace(); System.out.println("Exception: " + e.getMessage()); } finally { try { if (conn != null) conn.close(); } catch (SQLException e) { } } } //Get the Description from the object Name public String getDescription(String name){ int index = -1; String value = ""; for (int a = 0; a < names.size(); a++){ if(names.get(a).equals(name)){index=a;} } if(index != -1){value = descriptions.get(index);} return value; } //Get the Location from the object Name public String getLocation(String name){ int index = -1; String value = ""; for (int a = 0; a < names.size(); a++){ if(names.get(a).equals(name)){index=a;} } if(index != -1){value = locations.get(index);} return value; } //Get the ID from the object Name public int getID(String name){ int index = -1; int value = -1; for (int a = 0; a < names.size(); a++){ if(names.get(a).equals(name)){index=a;} } if(index != -1){value = ids.get(index);} return value; } //Get the object Name from object ID public String getName(int id){ int index = -1; String value = ""; for (int a = 0; a < ids.size(); a++){ if(ids.get(a) == id){value= names.get(a);} } return value; } //Print information of all the objects in the database public void showStageObject(){ for (int a = 0; a < ids.size(); a++){ System.out.print(" Name : " + names.get(a)); System.out.print(", Description : " + descriptions.get(a)); System.out.print(", Location : " + locations.get(a)); System.out.println(""); } } }