import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; import java.sql.*; public class AndmedSisse{ public static void main(String argumendid[]) throws Exception{ Document d=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("lingikogu.xml"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection("jdbc:odbc:baas1", "", ""); Statement st=cn.createStatement(); //teeb uued tabelid baasi1 try { String llause="CREATE TABLE Lingid_2 (ID int, Link text, aadress text)"; st.executeUpdate(llause); String hlause="CREATE TABLE hinded_2 (id int, lingi_id int, hinne int)"; st.executeUpdate(hlause); } catch(Exception s) { System.out.println("Baasid olemas: "+s.getMessage()); s.printStackTrace(); } //paneb andmed andmebaasi NodeList lingid=d.getFirstChild().getFirstChild().getChildNodes(); for(int i=0; i < lingid.getLength(); i++){ kirjutaLink(lingid.item(i), st); } NodeList hinded=d.getFirstChild().getFirstChild().getNextSibling().getChildNodes(); for(int i=0; i < hinded.getLength(); i++){ kirjutaHinne(hinded.item(i), st); } cn.close(); } catch(Exception e) { System.out.println("Viga!: "+e.getMessage()); e.printStackTrace(); } } static void kirjutaLink(Node link, Statement st) throws Exception{ String lause="INSERT into Lingid_2 (ID, Link, aadress) values ('"+link.getFirstChild().getFirstChild().getNodeValue()+"', '"+link.getFirstChild().getNextSibling().getFirstChild().getNodeValue()+"', '"+link.getFirstChild().getNextSibling().getNextSibling().getFirstChild().getNodeValue()+"')"; System.out.println(lause); st.executeUpdate(lause); } static void kirjutaHinne(Node hinne, Statement st) throws Exception{ String lause="INSERT into hinded_2 (id, lingi_id, hinne) values ("+hinne.getFirstChild().getFirstChild().getNodeValue()+", "+hinne.getFirstChild().getNextSibling().getFirstChild().getNodeValue()+", "+hinne.getFirstChild().getNextSibling().getFirstChild().getNodeValue()+")"; System.out.println(lause); st.executeUpdate(lause); } }