Pages

Subscribe:

Ads 468x60px

6/8/11

Analogy of Java - JDBC - MySQL

Dunno why, but it's kind of difficult for me to understand the relationship between Java - JDBC - MySQL and GUI programing. Even I' had studied about it before (in my second semester) but when i review it now-it's still need more time to understand it again. So, I write down here my opinion and my understanding about it wishing that it'll help me a lot when I review it someday.
Let's imagine that java is a man who wants to rent a cupboard on a warehouse and JDBC is a name card of the warehouse's owner, and the warehouse owner itself is MySQL.
  1. First of all, the man should make an connection to warehouse's owner with help from the card name. That's read the information in the name card and try to contact the owner. Same like Java, Java must include the JDBC driver coz Java must know the information about MySQL from JDBC - it like when the man read the information in name card ( Java read the information from JDBC). It's explained in my previous tutorial Connect Java and Database (2) Create Database (part : include the driver for java and mysql in Libraries ).
  2. After read the information from JDBC, Java can contact MySQL use class Connection . Like when the man contact the owner via telephone or internet. Class Connection is provided by Java to make a connection between Java and MySQL, that's why we just need to make an object from that class for it. Connection con = DriverManager.getConnection( url_of_database, mySQL_ussername, mySQL_pass);
  3. After contacting MySQL, java should make a tabel that store data in MySQL. A man who rent a cupboard on wirehouse should arrange his racks on his cupboard that could keep his stuff safely and tidy. Imagine that cupboard is table on MySQL, racks is the coloum and bar and the man's stuff is data from Java. But in my previous tutorial, I made the tabel from phpMyAdmin not from NetBean.
  4. Imagine that the man and the owner are a professional, they would make a formal agreement about what is forbidden and the facility for the man. Java also use an agreement, called JDBC Statement. This statement make java could store or update data to MySQL.Statement stated = con.createStatement();
  5. Then, when the man wants to take his stuff he should know where's the rack that store his stuff. Imagine that his stuff is so many till he made a note where's he store his stuff in the rack and stick it to the racks key. Java also have a class that contains values when we operate MySQL, called ResultSet. But I imagine ResultSet as a key that open the racks which is store the data. Actually we could use it without make an object from ResulSet clas. But it easy to type when it have a simple name variable. ResultSet rs = stated.executeQuery("SELECT table_name" WHERE column_name LIKE '0'"); means that variabel rs would akses values from table named table_name with column called column_name when the key is 0 or line number 0. So when we take values from column line 0, we could type rs.getInt("column_name");.