|
|||||||||
| PREV NEXT | FRAMES NO FRAMES | ||||||||
database 󸮿¡ ÀÖ¾î JDBC¿Í SQL¹®À» Á÷Á¢ ÀÌ¿ëÇÒ ¶§ ¹ß»ýÇÏ´Â ±¸Çö¿¡ Ä¡ÁßÇÑ ÄÚµù, ¹Ýº¹ÀûÀÎ ÀÛ¾÷(Ä÷³¸í ¸ÂÃß±â, type validation, ¹®ÀÚ¿ 󸮵î)±×¸®°í ±×·Î ÀÎÇØ ¹ß»ýÇÏ´Â ¿©·¯°¡Áö ¿¡·¯Ã³¸®, µð¹ö±ëÀÛ¾÷, À¯Áöº¸¼ö ÀÛ¾÷ µî ¿©·¯°¡Áö ¿À¹öÇìµå°¡ ÀÖ½À´Ï´Ù. ±×·¡¼ ÀÌ·± ¿À¹öÇìµå¸¦ ÇÇÇϱâ À§ÇØ SQL¹® ´ë½Å Ãß»óÈµÈ Class¸¦ ÀÌ¿ëÇÏ¿© °ü³äÀûÀÎ ÇÁ·Î±×·¡¹Ö ¹æ¹ýÀ¸·Î DB 󸮸¦ ´ë½ÅÇϱâ À§ÇÑ Framework API ÀÔ´Ï´Ù.
| Packages | |
| org.metaworks | |
| org.metaworks.inputter | |
| org.metaworks.query | |
| org.metaworks.ui | |
| org.metaworks.validator | |
| org.metaworks.viewer | |
| org.metaworks.web | |
| org.metaworks.wizard | |
¼ºñ½º
ÀÌ FrameworkÀº ´ÙÀ½ÀÇ ¼ºñ½º¸¦ Áö¿øÇÕ´Ï´Ù.ÇÁ·Î±×·¡¸Ó °¡À̵åÇÑ ¸¶µð·Î SQL¹®ÀåÀ¸·Î ó¸®ÇÏ´ø ¸ðµç °ÍÀ» JAVA ÀڷᱸÁ¶¿Í ¹®¹ý±¸Á¶¿¡ ¿¡¹Ä·¹À̼ÇÇÑ´Ù°í »ý°¢ÇÏ¸é ½±°Ô ÀÌÇØÇÏ½Ç ¼ö ÀÖ½À´Ï´Ù. ´ÙÀ½ Ç¥´Â ±×·¯ÇÑ ¸ÊÇÎ ±¸Á¶¸¦ º¸¿©ÁÝ´Ï´Ù.
- Hashtable ±â¹ÝÀÇ Row´ÜÀ§·Î Record °Ë»ö, ÀúÀå, ¼öÁ¤, »èÁ¦
- »õ ·¹ÄÚµå ÀúÀå°ú ±âÁ¸ µ¥ÀÌÅÍ ¼öÁ¤À» À§ÇÑ ÀÔ·Â/¼öÁ¤ Æû »ý¼º(Swing Dialog / panel)
- ÀϰüÈµÈ ÀԷ¹æ¹ý/°ËÁõ¹æ¹ý Á¦°ø (Inputter / Validator)
- Database ÀÇ meta data ¸¦ Á÷Á¢ ºÒ·¯¿Í Data󸮿¡ ¹Ù·Î Ȱ¿ë
- Query Form ÀÇ Á¦°ø
¡Ø Metaworks Class and Database Mapping
class ¸í Database Mapping ÁÖ¿ä ¸Þ¼µå Database Mapping Table Table load, find select Record Record (tuple) save, update, delete insert, update, delete Field Data get, put select <fieldname>, update set <field> FieldDescriptor Field Declaration get, put desc <table> FieldAttribute Field ¼Ó¼º (size, default...) Inputter ÀԷ¹æ¹ý Validator Check Constraint isValid check (...)
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@165.186.52.29:1526:iman5", "infodba", "ckddnjs5");
// tableÀ» »ý¼ºÇÑ´Ù. dbÀÇ 'create'¹®À» ¿¬»ó
Table px_part_newparts = new Table(
"px_part_newparts", // table¸í
// column title type iskey
new FieldDescriptor[]{
new FieldDescriptor("SEQNO", "¼ø¹ø", Types.INTEGER, true),
new FieldDescriptor("DESCRIPTION", "¼³¸í"),
new FieldDescriptor("DEVELOPMENTDATE", "°³¹ßÀÏÁ¤", Types.DATE),
new FieldDescriptor("DIVISION", "»ç¾÷ºÎ")
},
con // ÀÌ tableÀÌ »ç¿ëÇÒ db connection
);
°Ë»ö
// À§¿¡¼ Á¤ÀÇÇÑ table¿¡¼ seqno=10000ÀÎ ·¹Äڵ带 °Ë»ö
Record [] rec = px_part_newparts.find("SEQNO=10000");
// Çϳª¶óµµ ã¾ÒÀ¸¸é ±× °ªÀ» ȸ鿡 »Ñ¸²
if(rec.length> 0){
System.out.println("DESCRIPTION = " + rec[0].get("DESCRIPTION"));
System.out.println("SEQNO = "+rec[0].get("SEQNO"));
}
¼öÁ¤
rec[0].put("DESCRIPTION", "¹Ù²Û³»¿ë");
rec[0].update();
»èÁ¦
rec[0].delete();»õ ·¹ÄÚµå »ý¼ºÇؼ insert(save)Çϱâ
Record newRec = new Record(px_part_newparts);
newRec.put("SEQNO", "5555");
newRec.put("DESCRIPTION", "»õ ·¹ÄÚµåÀÇ description");
newRec.put("DIVISION", "CNZ");
newRec.save();
ÁÖ¾îÁø TableÀÇ ÇÑ Row¸¦ ÀԷ¹ÞÀ» ¼ö ÀÖ´Â Form Panel (Swing JPanel)À» »ý¼ºÇÑ´Ù.
Example 1) Æû »ý¼º
public static void main(String args[]) throws Exception{
Class.forName("oracle.jdbc.driver.OracleDriver");
// ~/orawin95/network/admin/tnsnames.ora file ÂüÁ¶.
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@165.186.52.29:1526:iman5", "infodba", "ckddnjs5");
// tableÀ» »ý¼ºÇÑ´Ù. dbÀÇ 'create'¹®À» ¿¬»ó
Table px_part_newparts = new Table(
"px_part_newparts", // table¸í
// column title type iskey
new FieldDescriptor[]{
new FieldDescriptor("SEQNO", "¼ø¹ø", Types.INTEGER, true),
new FieldDescriptor("description", "¼³¸í"),
new FieldDescriptor("developmentdate", "°³¹ßÀÏÁ¤", Types.DATE),
new FieldDescriptor("DIVISION", "»ç¾÷ºÎ")
},
con
);
Record [] rec = px_part_newparts.find("seqno=10000");
if(rec.length> 0){
System.out.println("description = " + rec[0].get("description"));
System.out.println("seqno = "+rec[0].get("seqno"));
}
final InputForm newForm = new InputForm(px_part_newparts, "10000");
JFrame frame = new JFrame("test");
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add("Center", newForm); // ¢¸¢¸¢¸¢¸¢¸¢¸
JButton saveBtn = new JButton("ÀúÀå");
frame.getContentPane().add("South", saveBtn); // ¢¸¢¸¢¸¢¸¢¸¢¸
saveBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
newForm.getRecord().save();
}catch(Exception ex){}
}
});
frame.pack();
frame.setVisible(true);
}
Example 2) ´ÙÀ̾ó ·Î±×¸¦ ¸¸µé¾î ÀÔ·Â Æû ¶ç¿ì±â
InputForm testForm2 = new InputForm(
new Table("test2",
new FieldDescriptor[]{
new FieldDescriptor("column1"),
new FieldDescriptor("column2"),
new FieldDescriptor("column3")
}
)
){
public void onSaveOK(Record rec, JDialog dialog){
com.ugsolutions.util.MessageBox.post("'"+ rec.get("column1") +"' ·¹Äڵ尡 ÀúÀåµÇ¾ú½À´Ï´Ù.", "ÀúÀå¿Ï·á", 1);
}
public void onSaveFailed(Exception e, JDialog dialog){
com.ugsolutions.util.MessageBox.post("ÀúÀå ½ÇÆÐ Çß½À´Ï´Ù.\n ¿¡·¯=" + e.getMessage(), "½ÇÆÐ", 1);
}
};
testForm2.postInputDialog(frame);
Swing ÀÔ·Â UI ComponentµéÀ» ÀϰüÀûÀÎ ¹æ¹ýÀ¸·Î ó¸®Çϱâ À§ÇÑ Interface·Î ÀçȰ¿ë¼º°ú Ãß»óȰ¡ ½¬¿ö UI°³¹ß ¹× È®ÀåÀÌ À¯¿¬ÇÏ°í ¿ëÀÌÇÕ´Ï´Ù.
±âº»À¸·Î Á¦°øµÇ´Â Inputterµé(com.cwpdm.util.db.inputter.*)
| Inputter class¸í | ¼³¸í | default type(¼¼ÆÃÇÏÁö ¾Ê¾Æµµ ÀÚµ¿À¸·Î ¼±ÅõǴ Type |
|---|---|---|
| TextInput | JTextField ÀÔ·Ââ | Types.VARCHAR |
| NumberInput | ¼ýÀÚ¸¸ ÀԷµǰí right ¹èÄ¡ÀÎ JTextField | Types.INTEGER |
| DateInput | ´Þ·Â DateButton | Types.DATE |
| SelectInput | JComboBox·Î µÇ°í ¿É¼ÇÁß¿¡ Çϳª¸¸ ¼±Åà | ¾ø½¿ |
| RadioInput | RadioButton·Î µÇ°í ¿É¼ÇÁß¿¡ Çϳª¸¸ ¼±Åà | ¾ø½¿ |
| ReferenceInput | ¼±Åà PopupButtonÀ¸·Î µÇ°í db¿¡¼ ¿É¼ÇÀ» Àоî¿Í Çϳª¸¸ ¼±Åà | ¾ø½¿ |
»ç¿ë¿¹Á¦
FieldDescriptorÀÇ 6¹øÂ° parameter¿¡ ÀÎÀÚ·Î ³ÖÀ¸¸é µÈ´Ù.
new FieldDescriptor("COMMON", "°ø¿ë¿©ºÎ", 12, false,
new FieldAttribute[]{
new FieldAttribute("default", new String("°³Á¶")),
},
new SelectInput(new String[]{"", "½ÅÀÛ", "°³Á¶", "¼ö·®Ãß°¡"}) //¢¸¢¸¢¸¢¸¢¸
),
new FieldDescriptor("TARGETPRICE", "¸ñÇ¥´Ü°¡", 2, false,
null,
new NumberInput() //¢¸¢¸¢¸¢¸¢¸
),
...
À§¿Í °°ÀÌ tableÀ» Á¤ÀÇÇÑ ÈÄ¿¡ InputForm class¸¦ ÀÌ¿ëÇØ Form panelÀ» ¸¸µì´Ï´Ù.
InputForm inputForm = new InputForm(table); someFrame.add(inputForm); someFrame.setVisible(true); ...³ª¸¸ÀÇ Inputter ¸¸µé±â
public class AbstractComponentInputter{
// InputFormÀÌ °ªÀ» ¾ò¾î³¾ ¶§ »ç¿ëÇÒ ·ÎÁ÷
abstract public Object getValue();
// InputFormÀÌ °ªÀ» ¼¼ÆÃÇØ¼ º¸¿©ÁÙ ¶§(¼öÁ¤ Æû/ÃʱâÄ¡ ¼¼ÆÃ½Ã) »ç¿ëÇÒ ·ÎÁ÷
abstract public void setValue(Object obj);
// InputFormÀÌ »õ·Î ÄÄÆ÷³ÍÆ®¸¦ ¾ò¾î¾ß ÇÒ ¶§ »ç¿ëÇÒ ·ÎÁ÷
abstract public Component getNewComponent();
}
¾Æ·¡ÀÇ ¿¹Á¦´Â DateInputÀÇ ¼Ò½ºÄÚµåÀÔ´Ï´Ù. (inputter.*¸¦ º¸½Ã¸é ´Ù¾çÇÑ ¿¹Á¦¸¦ º¼ ¼ö ÀÖ½À´Ï´Ù)
package com.cwpdm.util.db.inputter;
import java.awt.Component;
import javax.swing.*;
import com.ugsolutions.util.*;
import java.util.Date;
import java.text.*;
public class DateInput extends AbstractComponentInputter{
public DateInput(){
super();
}
/////////// implemetations //////////////
// InputFormÀÌ °ªÀ» ¾ò¾î³¾ ¶§ »ç¿ëÇÒ ·ÎÁ÷
public Object getValue(){
Date srcDate = ((DateButton)getComponent()).getDate();
if(srcDate==null) return null;
Date rtnDate = new Date(srcDate.getTime()){
public String toString(){
SimpleDateFormat displayFormatter = new SimpleDateFormat("yyyy-MM-dd");
String making = displayFormatter.format(this);
return making;
}
};
return rtnDate;
}
// InputFormÀÌ °ªÀ» ¼¼ÆÃÇØ¼ º¸¿©ÁÙ ¶§(¼öÁ¤ Æû/ÃʱâÄ¡ ¼¼ÆÃ½Ã) »ç¿ëÇÒ ·ÎÁ÷
public void setValue(Object obj){
if(obj instanceof Date && obj!=null)
((DateButton)getComponent()).setDate((Date)obj);
}
// InputFormÀÌ »õ·Î ÄÄÆ÷³ÍÆ®¸¦ ¾ò¾î¾ß ÇÒ ¶§ »ç¿ëÇÒ ·ÎÁ÷
public Component getNewComponent(){
DateButton dateButton = new DateButton();
dateButton.setDisplayFormat("yyyy-MM-dd");
return dateButton;
}
}
¾à°£ÀÇ º¯ÇüÀ¸·Î °·ÂÇÑ ÀûÀÀ¼ºÀ» º¸¿©ÁÖ´Â Inputter!
...
new FieldDescriptor("LEVELNO", "LEVEL", 12, false, null,
// level no´Â '.'°ú ¼ýÀÚ¸¸ ¹ÞÀ½
new TextInput(){
public Component getNewComponent(){
CodeField codeField = new CodeField(10);
codeField.setAllowAlphabet(false);
codeField.setAllowSpace(false);
codeField.setAllowingCharset(".");
return codeField;
}
}
)
...
ÀԷ¹ÞÀº Çʵ尪À» °Ë»ç(invalidate; validation)Çϱâ À§ÇÑ ÀϰüÀûÀÎ interface¸¦ Á¦°øÇÑ´Ù. ƯÈ÷, Validator¸¦ ¿©·¯°³ ¼¼ÆÃÇÑ Á¶ÇÕ¿¡ ÀÇÇÑ validationÀº °ÅÀÇ ¸ðµç validation ruleÀ» Ä¿¹öÇÒ ¼ö ÀÖÀ¸¸ç InputForm¿¡ ÀÇÇØ ÀÚµ¿À¸·Î ¿¡·¯ ¸Þ½Ã¡ÀÌ Ã³¸®µÈ´Ù.
±âº»À¸·Î Á¦°øµÇ´Â Validatorµé(com.cwpdm.util.db.validator.*)
| Validator class¸í | ¼³¸í | default type(¼¼ÆÃÇÏÁö ¾Ê¾Æµµ ÀÚµ¿À¸·Î ¼±ÅõǴ Type |
|---|---|---|
| NotNullValid | ³»¿ëÀÌ ÀÖ´ÂÁö(nullÀÎÁö) üũ (dbÀÇ "not null" Á¦¾à) | ¾÷½¿ |
| NumberValid | ¼ýÀÚ°¡ ÀԷµǾú´ÂÁö üũ | Types.INTEGER |
»ç¿ë¹ý
Field Descriptor ÀÇ 7¹øÂ° ÀÎÀÚ·Î ÀÔ·ÂÇÑ´Ù.
³» Validator ¸¸µé±â
AbstractValidator ÀÇ Flow Diagram
1. isValid(data)¸¦ callÇØ¼ data°¡ ¸Â´ÂÁö È®ÀÎÇÑ´Ù.
2. Ʋ¸®¸é throw new Exception(getErrorMessage())ÇÑ´Ù.
3. InputFormÀº (validator¸¦ »ç¿ëÇÑ class) ExceptionÀÌ ³ª°Ô µÈ´Ù.
±¸Çö¿¹Á¦
public static void main(String args[]){
new InputForm(
new Table(
"validator test",
new FieldDescriptor[]{
new FieldDescriptor("userid", "»ç¿ëÀÚ ID", 0, true, null, null,
new Validator[]{
// ±æÀÌ Ã¼Å©
new AbstractValidator(){
public boolean isValid(Object data){
return ((String)data).length() > 10;
}
public String getErrorMessage(){
return "10ÀÚ ÀÌÇÏ¿©¾ß ÇÕ´Ï´Ù";
}
},
// ºóÄ Ã¼Å©
new AbstractValidator(){
public boolean isValid(Object data){
return ((String)data).indexOf(" ") == -1;
}
public String getErrorMessage(){
return "ºóÄÀÌ Çã¿ëµÇÁö ¾Ê½À´Ï´Ù.";
}
}
}
)
}
)
).postInputDialog(null);
}
FieldAttribute´Â ´ÙÀ½°ú °°Àº ¿ëµµ·Î »ç¿ëµË´Ï´Ù.
Example)
public static void main(String args[]) throws Exception{
// tableÀ» »ý¼ºÇÑ´Ù. dbÀÇ 'create'¹®À» ¿¬»ó
Table px_part_newparts = new Table(
"px_part_newparts", // table¸í
// column title type iskey
new FieldDescriptor[]{
new FieldDescriptor("NAME", "À̸§", Types.VARCHAR, true,
new FieldAttribute[]{
new FieldAttribute("default", new String('pongsor')), //¢¸¢¸¢¸¢¸¢¸
new FieldAttribute("size", new Integer(10)) //¢¸¢¸¢¸¢¸¢¸
},
),
new FieldDescriptor("DATE", "ÀÔ·ÂÀÏ", Types.DATE, false,
new FieldAttribute[]{
new FieldAttribute("source", new String('sysdate')), //¢¸¢¸¢¸¢¸¢¸
}
)
}
);
}
Attibute Keys| key °ª | ¼³¸í | »ç¿ë¿¹ |
|---|---|---|
| default | InputForm »ý¼º½Ã ÃʱⰪ | new FieldAttribute("default", "³²ÀÚ")) |
| size | ÀԷ¹޴ ÄÄÆ÷³ÍÆ®ÀÇ ÃÖ´ë ÀԷ°ª »çÀÌÁî | new FieldAttribute("size", new Integer(10)) |
| hidden | ¼û°ÜÁø Çʵå·Î InputForm»ý¼º½Ã ³ªÅ¸³ªÁö ¾ÊÀ½. ÁÖ·Î °ªÀ» ó¸®Çؼ ³ÖÀ» ¶§ »ç¿ë. ¼¼ÆÃÇÑ °ªÀº µðÆúÆ®·Î »ç¿ëµÊ | new FieldAttribute("hidden", "test") |
| mandatory | Çʼö ÀÔ·Â ÇʵåÀÓ. ÀÔ·Â ÄÄÆ÷³ÍÆ®¿¡ ºÓÀº ¼¼¸ð°¡ »ý±è | new FieldAttribute("mandatory", new Boolean(true)) |
| source | SQL󸮽à ¼¼ÆÃÇÑ StringÀ» ±×´ë·Î SQL·Î¼ »ç¿ëÇÑ´Ù. ÁÖ·Î DB server ³¯Â¥(sysdate), ½ÃÄö½ºµîÀÇ Ç¥ÇöÀ» ¹Ù·Î ¾²¸é À¯¿ëÇÏ´Ù. | new FieldAttribute("source", "sysdate") |
| dontcare | ¸ðµç 󸮿¡ ´ë»óÀÌ µÇÁö ¾Ê´Â´Ù | new FieldAttribute("dontcare", new Boolean(true)) |
¡Ø ¸ðµç °ªÀº Object ÇüÀ¸·Î ÀԷµǾî¾ß Çϱ⠶§¹®¿¡ primitive typeÀ» »ç¿ëÇØ¼´Â ¾ÈµÈ´Ù. ¿¹¸¦ µé¾î ¼öÄ¡¸¦ ÀÔ·ÂÇÒ ¶§´Â 'new Integer(°ª)'À¸·Î ÀÔ·ÂÇØ¾ß ÇÑ´Ù.
new Table(...){
public void save(Record rec) throws Exception{
try{
rec.update();
}catch(Exception e){
super.save(rec);
}
}
}
...
private class ConfigTable extends Table{
ConfigTable(){
super(
"Table ¼¼ÆÃ",
new FieldDescriptor[]{
new FieldDescriptor("connStr"),
new FieldDescriptor("uid"),
new FieldDescriptor("pwd")
}
);
}
public void save(Record rec) throws Exception{
setConnection(""+get("connStr"), ""+get("uid"), ""+get("pwd"));
}
public void update(Record rec) throws Exception{
save(rec);
}
}
....
new InputForm(new ConfigTable()){
public void onSaveOK(Record newConfig){
MessageBox.post("¼³Á¤ÀÌ ¿Ï·áµÇ¾ú½À´Ï´Ù", "¼³Á¤¿Ï·á", 1);
}
}.postInputDialog(null, "È®ÀÎ", "¼öÁ¤");
...
|
|||||||||
| PREV NEXT | FRAMES NO FRAMES | ||||||||