001    //Title:       XML-Tabellenimport
002    //Version:
003    //Copyright:   Copyright (c)  2002
004    //Author:      Altmann
005    //Company:     GTDS
006    //Description:
007    
008    import java.sql.Connection;
009    import java.sql.PreparedStatement;
010    import java.sql.SQLException;
011    import java.sql.Timestamp;
012    import java.util.Hashtable;
013    
014    import org.apache.xerces.parsers.DOMParser;
015    import org.w3c.dom.Document;
016    import org.w3c.dom.Element;
017    import org.xml.sax.SAXException;
018    
019    public class XMLTabImp {
020    
021    public static void main (String args[]) 
022            throws java.io.IOException, SQLException, SAXException {
023    
024            String usrpass = args[0];
025    
026            String username = usrpass.substring(0, usrpass.indexOf("/"));
027            String password = usrpass.substring(usrpass.indexOf("/")+1, usrpass.indexOf("@"));
028            String database = usrpass.substring(usrpass.indexOf("@")+1);
029    
030            Document xml;
031    
032            DOMParser dp =  new DOMParser();
033            dp.parse(args[1]);
034            xml = dp.getDocument();
035    
036    
037            Connection conn = DBSession.getConnection(username, password, database);
038            Hashtable dataTypes = new Hashtable();
039            ChildElementWalker cew = new ChildElementWalker(xml.getDocumentElement(), null);
040            Element tab  = cew.getNextElement();
041    
042            String stat = "INSERT INTO " + tab.getTagName();
043            String colList = "";
044            String valList = "";
045            
046            cew = new ChildElementWalker(tab, null);
047            Element desc = cew.getNextElement();
048            ChildElementWalker descW = new ChildElementWalker(desc, null);
049            while(true) {
050                    Element column = descW.getNextElement();
051                    if(column == null) {
052                            break;
053                    };
054                    String colName  = column.getTagName();
055                    String dataType = column.getAttribute("datatype");
056                    dataTypes.put(colName, dataType);
057                    colList += ", " + colName;
058                    valList += ", ?";
059                    //System.err.println(colName);
060            }
061            stat = stat + "(" + colList.substring(2) + ") VALUES (" + valList.substring(2) + ")";
062            System.err.println(stat);
063            PreparedStatement ins = conn.prepareStatement(stat);
064            int count = 0;
065            while(true) {
066                    Element row = cew.getNextElement();
067                    if(row == null) {
068                            break;
069                    };
070                    ChildElementWalker colw = new ChildElementWalker(row, null);
071                    int i = 0;
072                    while(true) {
073                            Element column = colw.getNextElement();
074                            if(column == null) {
075                                    ins.execute();
076                                    ins.clearParameters();
077                                    break;
078                            };
079                            String colName  = column.getTagName();
080                            // System.err.print(colName);
081                            String dataType = (String) dataTypes.get(colName);
082                            i++;
083                            String value = null;
084                            if(column.getFirstChild() != null) {
085                                    value = column.getFirstChild().getNodeValue();
086                            }
087                            if(dataType.equals("DATE")) {
088                                    if(value==null) {
089                                            ins.setTimestamp(i, null);
090                                    } else {
091                                            ins.setTimestamp(i, Timestamp.valueOf(value));
092                                    }
093                            } else if(dataType.equals("NUMBER")) {
094                                    if(value==null) {
095                                            ins.setString(i, null);
096                                    } else {
097                                            ins.setDouble(i, Double.valueOf(value).doubleValue());
098                                    }
099                            }else {
100                                    ins.setString(i, value);
101                            }
102                    }
103                    count++;
104            }
105            System.err.print(count+ " rows inserted");
106            ins.close();
107    }
108    
109    }