Mga 'tol, Series #4. Continuation.
- because we want to be an OOP Java Programmer, this is an overview of OBJECT-ORIENTED PROGRAMMING in Java.
- how to process database data using a CLASS Object or TEMPLATE or POJO....whatever you want to call it.
- this is how PROFESSIONALS do it.
*****Download the overview PDF.... visual lang.*****
*****And save these two java files separately in the SAME project folder.****
*****THIS IS OUR STUDENTTEMPLATE class*********
***save it as StudentTemplate.java******************
*****THIS IS THE MAINAPP class********************
*****Take note: We used ArrayList to store student objects*********
- because we want to be an OOP Java Programmer, this is an overview of OBJECT-ORIENTED PROGRAMMING in Java.
- how to process database data using a CLASS Object or TEMPLATE or POJO....whatever you want to call it.
- this is how PROFESSIONALS do it.
*****Download the overview PDF.... visual lang.*****
*****And save these two java files separately in the SAME project folder.****
*****THIS IS OUR STUDENTTEMPLATE class*********
***save it as StudentTemplate.java******************
Code:
/**
* Ito ang ating blueprint or class or pojo
* Ito ang magiging template natin everytime
* na mag query tayo ng student data from database
* Take note of proper naming convention
*/
public class StudentTemplate {
private String id;
private String firstName;
private String lastName;
private String course;
private String gender;
private String age;
//ito yung mga GETTER methods
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getCourse() {
return course;
}
public String getGender() {
return gender;
}
public String getAge() {
return age;
}
//ito yung mga SETTER methods
public void setId(String id) {
this.id = id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setCourse(String course) {
this.course = course;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAge(String age) {
this.age = age;
}
}
*****THIS IS THE MAINAPP class********************
*****Take note: We used ArrayList to store student objects*********
Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class MainApp4 {
static Connection con;
static Statement statement;
static ResultSet resultSet;
static ResultSetMetaData rm;
static ArrayList<StudentTemplate> studentsCollection = new ArrayList<StudentTemplate>(); //we use this to store student obj
static String format = "%-5s %-15s %-15s %-35s %-15s %-15s"; //pampaganda lang
/****************** This is the main entry of application ***************/
/************Let's keep it clean and easy to read ***********************/
/*******So ilagay natin sa ibat ibang methods and mga tools natin *******/
public static void main(String[] args) throws SQLException {
initDB();
String query = "SELECT * from students";
performQuery(query);
//creating studenttemplate objects
//this is where we need the StudentTemplate class to create objects
while(resultSet.next()){
StudentTemplate newStudent = new StudentTemplate();
newStudent.setId(resultSet.getString(1));
newStudent.setFirstName( resultSet.getString(2) );
newStudent.setLastName( resultSet.getString(3) );
newStudent.setCourse( resultSet.getString(4) );
newStudent.setGender( resultSet.getString(5) );
newStudent.setAge(resultSet.getString(6));
//then i-add natin isa isa ang mga student sa ating array
studentsCollection.add(newStudent);
}
//output: print natin yung mga column names (optional)
printColumNames();
//output: and just like before, we print all students data
for(StudentTemplate stud : studentsCollection) {
System.out.println(String.format(format,stud.getId(), stud.getFirstName(), stud.getLastName(),
stud.getCourse(),stud.getGender(),stud.getAge()));
}
System.out.println("------------------------------------------------------");
System.out.println("There are " + studentsCollection.size() + " students in total.");
}
/*************************end of Main area********************************/
/*************************************************************************/
static void initDB() throws SQLException {
String url = "jdbc:mysql://localhost/phpcorner_db";
String user = "cody";
String password = "secret";
con = DriverManager.getConnection(url, user, password);
}
static void performQuery(String query) throws SQLException {
statement = con.createStatement();
resultSet = statement.executeQuery(query);
}
static void printColumNames() throws SQLException {
rm = resultSet.getMetaData();
System.out.println(String.format(format, rm.getColumnLabel(1), rm.getColumnLabel(2),
rm.getColumnLabel(3),rm.getColumnLabel(4),rm.getColumnLabel(5),rm.getColumnLabel(6)));
}
}
Attachments
-
You do not have permission to view the full content of this post. Log in or register now.