ලේසියෙන්ම හොයාගන්න මෙතනින්

ITP - Java


01.   Net Beans, MySQL database එකත් එක්ක connect කරන්න class එකක්, එකේ method එක ලියල පෙන්වන්න.
public static Connection createDatabaseConnection() throws ClassNotFoundException, SQLException{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql:///vehiclereservationsystem","root","");
        return conn;
}
සිද්ධ වෙන්නේ මොකක්ද කියලත් දැනගන්න.
මේ ලියන method එකෙන් return කරන්නේ Connection type,  object එකක්. මේ method එකෙන් exception එන්න පුළුවන්. ඒවා අපි throws කියන keyword එක යොදල එන්න පුළුවන් Exception throw කරන්න ඕන.
Class.forName("com.mysql.jdbc.Driver"); කියන දේ අපි යොදන්නේ program එක run වෙන මොහොතේදී, dynamically  “ ” ඇතුලේ කියන class එක  load කරන්න කියලයි. Load උනාට පස්සේ JVM එක, class එකේ static blocks, execute කරාවී. ඉතින් ඔන්න ඔය කියන class එක හොයා ගන්න බැරි උනොත්, Class Not Found Exception එකක් එනවා.
Connection class එකෙන් conn කියල object එකක් හදාගෙන, DriverManager කියන class එකේ getConnection කියන method එක call කරලා මං එයාට parse කරනවා, මං කලින් ලියපු class එක refer කරන Driver එක හරහා connect වෙන්න, මෙනම් database එකට, සහ ඒක access කරන්න ඕන වෙන MySQL username and password. මෙන්න මෙතැනදී අදාල database එක හොයාගන්න බැරි උනොත් හෝ, username, password වැරදුනොත් SQL Exception එනවා.


02. හදාගත්තු ඒ connection එකෙන්, database එකෙන් data ගන්නේ, database එකට data write කරන්නේ කොහොමද? ලියන්න ඕනෑ method මොනවද?
අපි මේ සඳහා තවත් Class එකක් ලියනවා.
public static ResultSet getData(String query) throws SQLException, ClassNotFoundException{
        Connection conn = DB_Connect.createDatabaseConnection();
        Statement statement = conn.createStatement();
        ResultSet resultset = statement.executeQuery(query);
        return resultset;
}   
public static void setData (String query) throws SQLException, ClassNotFoundException{
        Connection conn = DB_Connect.createDatabaseConnection();
        Statement statement = conn.createStatement();
        statement.executeUpdate(query);
}

සිද්ධ වෙන්නේ මොකක්ද කියලත් දැනගන්න.
මෙහිදී
method දෙකක් අපි declare කරනවා. එකක් database එකෙන් data retrieve කරගන්න. මම ඒක නම් කරන්නම් getData කියල. එයාගේ return type එක වෙන්නේ ResultSet. ඒ වගේම parameter එකක් විදියට String දත්තයක් මම parse කරනවා. ඒ තමා මට execute කරවන්න ඕනෑ කරන sql query
එක.
කලින් හදාගත්තු class එකෙන් මං connection එකේ විස්තර තියෙන connection object එකක් හදාගෙන, මං ඒ  connection එකට statement එකක් create කරනවා.(like a file descriptor). ඒ හරහා, මං කියනවා මගේ query එක execute කරන්න කියල (by executeQuery method). Query එක execute උනහම ඒ query එකට අනුව result එකක් ලබෙවිනේ. ඒ Result එක අපි save කරගන්නේ ResultSet එකකටයි. ඒ ResultSet එක අපි return කරනවා ඒ method එක call කරන තැනකට.
අනිත් method එක තමයි database එකට data insert කරන්න, setData method එක. එකෙත් ඉහත පරිදිමයි. එත් return කිරීමක් නෑ. Query එක execute කරවනවා විතරයි.

03. Password Field එකේ type කරපු password එක String variable එකකට save කරගන්නේ කොහොමද? ඒ text එක වෙනම Text Field එකක දක්වන්න.
අපි හිතමු, jPasswordField1 සහ jTextField1 කියල 2 ක් තියෙනවා කියල.
String password = new String(jPasswordField1.getPassword());
jTextField1.setText(password);


04. Query එකක් execute කරවලා එන Results, NetBeans project එකට ගෙන්න ගන්නේ කොහොමද?

ResultSet result = DB_Access.getData("select * from vehicle");
සිද්ධ වෙන්නේ මොකක්ද කියලත් දැනගන්න.
මේ සදහා අපි කලින් ලියපු getData method එක භාවිතා කරනවා. ඒ method එක ලිව්වේ DB_Access කියල නම් කල class එකේ නම්,
ResultSet object එකකට save කරගන්න කියනවා, DB_Access class එකේ තියෙන getData method එකට execute කරන්න ඕනෑ query එක argument එකක් විදියට parse කලාම return වෙන ResultSet එක.
මේක අපට නිකන්ම ලියන්න බෑ. Exception එන්න පුළුවන් නිසා, try catch block එකක් ඇතුලේ ඕනෑ ලියන්න.

05. Exception එකක් කියන්නේ මොකක්ද? Try-Catch block ලියන්නේ ඇයි? ලියන්නේ කොහොමද? කොහොමද එකෙන් Exception handle වෙන්නේ?
Exception එකක් කියන්නේ program එකක් run වෙද්දී, ඇති වෙන්න පුළුවන් අක්‍රමික අවස්ථාවන්. 0 න් බෙදන්න බෑ. size එක 5 ක් උණු array එකකට data 6 ක් දාන්න බෑ. String, integer කරගන්න බෑ වගේ අවස්ථා. තව ගොඩාක් තියේ. මේවා developer ට handle කරන්න පුළුවන් දේවල්.
ඉතින් ඒ exception handle කරන්න පුළුවන් විදියක් තමා try-catch block. මෙහෙමයි ලියන්නේ.
try{
  //exception එකක් හට ගන්න පුළුවන් coding ටික සහ ඒ සමග සිදුවන්න ඕනෑ දේවල් වල coding තමා  මෙතන ලියන්නේ.
}catch(Exception e){

 //try block එකේ ලියපු එක line code එකක් හරි වැරදුනාම, එතනින් ඊලගට program එක කෙලින්ම පනින්නේ catch block එකට. එකේ තියෙන code execute කරනවා. අපට catch block ඕනෑ තරම් ලියන්න පුළුවන්. එත් වෙන  වෙනම Exception වර්ග වෙන්න ඕනෑ. ඒක තමා ඔය catch block එකේ වරහන තුල තියෙන්න. හරියටම එන exception දන්නවනම්, ඒ ඒ exception එක අපට වෙන වෙනම handle පුළුවන්. ඒ ඒ exception එකෙන් object එකක් හදන්න. exception එක මොකක්ද කියල දැනගන්න සහ print කරගන්න,
e.printStackTrace();
System.out.println(e);

}


06. ResultSet එකක තියෙන data කියවන්නේ, යොදා ගන්නේ කොහොමද?
අපි resultset එකේ data වල end එකක් දන්නේ නැනේ. ඒ නිසා end එක වෙනකම් කියවන්න while loop එකක් යොදනවා.

String column1;
try{
            ResultSet result = DB_Access.getData("select * from vehicle");
            while(result.next()){
                 column1 = result.getString(1);
                 column2 = result.getString(2);
            }
}catch (Exception e){
            System.out.println(e.printStackTrace);
}
සිද්ධ වෙන්නේ මොකක්ද කියලත් දැනගන්න.
next() කියන method එකෙන් තමා resultset එකේ row by row move වෙන්නේ. ඒ ගමන් අපට පුළුවන් ඒ ඒ row එකේ column data, variable වලට assign කරගන්න. result object එක හරහා ඒ class එකේ next() method එක call කලාම, row එකක් කියවනවා, next row එකට යනවා. read කරපු row එකේ column වල data ගන්න පුළුවන්, result object එකෙන් ඒ class එකේ getString method එක call කරලා. ඒ method එක තුලට, column number එක parse කරන්නත් ඕනෑ.


07. JFrame form එකේ jButton එකක් click කලාම Message Box එකක් display කරලා පෙන්නන්න.
button එකේ actionPerformed (button clicked) event එකේදී,
JOptionPane.showMessageDialog(this, "Button Clicked", "Done" ,1); 

මෙතනදී showMessageDialog කියන method එකට parse කරනවා,
මුලින්ම මේ message window එකට parent කවුද කියන එක. this කියල දුන්නම ඉන්න class එකයි කියල. (දැනට ඉන්න වින්ඩෝව). ඔයාල මේ window එක ආවම, parent jFrameForm එක click කරන්න පුලුවන්ද කියල බලන්න. null කියල දුන්නම පුලුවන්ද කියල බලන්න.
දෙවනුව message box body එකේ display වෙන්න ඕනෑ text එක.
තුන්වෙනුව message box title එකට display වෙන්න ඕනෑ text එක.
සිව්වෙනුව දෙන්න ඕනෑ message box එකේ type එකයී.

0 - Error Message 
1 - Information Message 
2 - Warning Message 
3 - Question Message 
-1 - Plain Message



Error Message 0


Information Message 1



Warning Message 2



Question Message 3


Plain Message -1


08. ResultSet එකකට ගත්තු data, JTable එකකට දාන්නේ කොහොමද ? 
DefaultTableModel defaultTM = (DefaultTableModel) jTable1.getModel();
while(result.next()){
                Object object[]={result.getString(1),result.getString(2),result.getString(3),result.getString(4)};
                defaultTM.addRow(object);
}
මෙහිදී අපි කලින් කතා කරපු විදියට ResultSet එකකට data අරගෙනයි ඉන්නේ. හිතන්න, ඔයාලට කැමති row, column ගණනකට jTable එකක් jFrameForm එක මතට අරගෙනයි ඉන්නේ කියල. එකේ නම jTable1. jTable1 එකේ row, column model එකට අනුව  Default Table Model, defaultTM යන object එකක් හදාගන්නවා.
Object type Arrays ඔයාලට හුරු පුරුදු නැත්තත්, Arrays දන්නවනේ. Object type කියන්නෙත් ඒ වගේමයි. Arrays වලින් වෙනස් වෙන්නේ, Object type array වලට අවශ්‍යතාව පරිදි තව තවත් විශාල වෙන්න පුළුවන්. ඒ නිසා අපි table එකේ values store කරගන්න Object type  array භාවිත කරනවා. Array එකකට data assign කරනවා වගේමයි. ඊටපස්සේ කරන්නේ හදාගත් defaultTM object එකට row එකක් විදියට add කරන්න කියල ඒ object එක addRow method එක භාවිත කරනවා. ඕකෙන් තමා jFrameForm එකේ තියෙන jTable එකට data row, add වෙන්නේ.

09. Form or Window 2ක් අතර  data parse කරන්නේ කොහොමද ?

මේ සඳහා අපි යොදා ගන්නේ ඒ ඒ class එකේ constructor එකයි. එක් එක් class එකේ තියෙන  default Constructor එක වෙනුවට, අපි Parameters දාලා අපේම Constructor එකක් ලියනවා. ඒ parameter එකේ data type එක අපට ගෙන්න ගන්න ඕනෑ data එකේ type එකෙන් ඕනෑ තියන්න. දැන්, මේ class එක call කරන තැනදී, අපි ඒ අලුතින් ලියපු constructor එකෙන් තමා object එකක් create කරන්න ඕනෑ. උදාහරණ විදියට,
Login

String username = jUserNameField.getText();
MyHomeForm myForm = new MyHomeForm(username);

MyHomeForm

public Home_TG() {
        initComponents();
        this.setLocationRelativeTo(null);       
}
public Home_TG(String username) {
        initComponents();
        jUserNameLabel.setText(username);

        this.setLocationRelativeTo(null);      
}


10. iReport වලින් හදා ගත්තු Report එකක්

14 comments :

  1. Im proud of u dude as a school friend for ur contribution for the assignments and other stuff.......
    Dhanusha

    ReplyDelete
  2. really great work machan!!!!
    keep up d good work!! jayawewa!! :)

    ReplyDelete
  3. thnx alot bro... keep up d gud wrk..best of luck...!!!!

    ReplyDelete
  4. I think ..These stuff will more important to us during VIVA session...
    Thanks Brother!!!!!!!!!!!!!!!!
    GAYANI

    ReplyDelete
  5. Great work bokka!!! :) Keep it up!!!
    ~Tharindu~

    ReplyDelete
  6. Keep it up ur good work..

    ReplyDelete
  7. macho thara nathuwa me visthara tika english walin dapan ko kata patagan karaganna easy ban ethakota :)

    ReplyDelete
  8. Thank u bro............ It is easy to figure out something quit complex....... Keep it up................!!!

    ReplyDelete
  9. thanx for this post. keep up the good work!!!

    ReplyDelete
  10. ResultSet eka api hadissiyewath close kaloth eka return karanna ba neda. I mean even if we close the statement automatically ResultSet also will be closed(As I know). Close nokaloth connection eka digatama open wage scene ekak nadda ?
    If yes,, is there any other better approach to write the 'getData' method ??

    mea wage posts hugak ayata prayojanawath wei. Gd Luck to publish more like this :)

    ReplyDelete
    Replies
    1. yeah. ඔයා හරි. ඒ නිසා ඕන වැඩේ කරගෙන ඉවරවුනාට පස්සේ ResultSet එක Close කලාට අවුලක් නැහැ. ඒ කියන්නේ අදාල method එක call කරපු තැන හදපු ResultSet object එක. ඒ වගේම DB_Access class එකේ ResultSet object එක ගැන Garbage Collector බලා ගනීවි.

      අනිත් දේ, නිතරම Connection Open, Close කරන්න ගියහම Memory එක පිරෙනවා. ඇත්ත. ඒක නැති කරගන්න පුළුවන් Immutable Objects එහෙමත් නැත්නම් synchronized method භාවිතයෙන්.(Singleton Design Pattern). එතැනදී එක Thread එකක් තුල, අදාළ object එකේ, එක instance එකක් පමණක් තියාගන්න පුළුවන්.

      Delete