Project

General

Profile

« Previous | Next » 

Revision 7

Added by Matt Jones over 25 years ago

added db connection code

View differences:

DBWriter.java
17 17
import java.io.InputStream;
18 18
import java.net.URL;
19 19
import java.util.Properties;
20
import java.sql.*;
20 21

  
21 22
import org.xml.sax.ErrorHandler;
22 23
import org.xml.sax.InputSource;
......
37 38
public class DBWriter
38 39
{
39 40
    private XmlDocument			doc;
41
    private Connection			conn;
40 42

  
43
    // Constructer to init the class
44
    public DBWriter(String argv[]) 
45
	throws SAXException, SQLException, IOException, ClassNotFoundException {
46

  
47
	//
48
	// Load the document, using the appropriate custom
49
	// DOM elements.
50
	//
51
	XmlDocument	doc = createDocument (
52
		Resolver.createInputSource (new File (argv [1])),
53
		new FileInputStream (argv [0]),
54
		new ErrorPrinter ()
55
	);
56

  
57
	DBElement		root;
58

  
59
    	root = (DBElement)doc.getDocumentElement ();
60
	root.normalize ();
61
        System.out.println(root.getJunk());
62
       
63
        conn = openDBConnection(
64
		"oracle.jdbc.driver.OracleDriver",
65
		"jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV",
66
		"jones", "kinkaj0u");
67

  
68
        // Prepare a statement to cleanup the emp table
69
        Statement stmt = conn.createStatement ();
70
        try {
71
            	stmt.execute ("delete from xml_elements where nodeid = 1500");
72
        } catch (SQLException e) {
73
        	// Ignore an error here
74
        }
75
        
76
        // Prepare to insert new names in the Elements table
77
        PreparedStatement pstmt = conn.prepareStatement(
78
	        "insert into xml_elements (nodeid, nodename) values (?, ?)");
79
        
80
        // Add an element as employee number 1500
81
        pstmt.setInt (1, 1500);	// The first ? is for NODEID
82
        pstmt.setString (2, "eml-variable");// The second ? is for NODENAME
83
        // Do the insertion
84
        pstmt.execute ();
85
    }
86

  
41 87
    //
42 88
    // usage:  main someprops.props someplay.xml 
43 89
    //
......
50 96
		System.exit (1);
51 97
	    }
52 98

  
53
	    //
54
	    // Load the document, using the appropriate custom
55
	    // DOM elements.
56
	    //
57
	    XmlDocument	doc = createDocument (
58
		Resolver.createInputSource (new File (argv [1])),
59
		new FileInputStream (argv [0]),
60
		new ErrorPrinter ()
61
		);
99
            new DBWriter(argv);
62 100

  
63
	    DBElement		root;
64

  
65
    	    root = (DBElement)doc.getDocumentElement ();
66
	    root.normalize ();
67
            System.out.println(root.getJunk());
68

  
69 101
	} catch (Throwable t) {
70 102
            t.printStackTrace (System.out);
71 103
	}
......
122 154
	} catch (IOException e) {
123 155
	    e.printStackTrace (System.out);
124 156
	    throw e;
125

  
126 157
	}
127 158
    }
159
    
160
    private Connection openDBConnection(String dbDriver, String connection, 
161
		String user, String password) 
162
		throws SQLException, ClassNotFoundException {
163
      // Load the Oracle JDBC driver
164
      Class.forName ("oracle.jdbc.driver.OracleDriver");
128 165

  
166
      // Connect to the database
167
      // You can put a database name after the @ sign in the connection URL.
168
      Connection conn = DriverManager.getConnection(
169
	"jdbc:oracle:thin:@penelope.nceas.ucsb.edu:1526:DEV", 
170
	"jones", "kinkaj0u");
171
      return conn;
172
    }
173

  
129 174
    static class ErrorPrinter implements ErrorHandler
130 175
    {
131 176
	private void message (String level, SAXParseException e)
......
165 210
    }
166 211

  
167 212
}
213

  
Makefile
11 11
	#java -classpath ".$(SEP)$(CPATH)" \
12 12
		#main ./book-order.xml
13 13
		
14
	java -classpath ".$(SEP)$(CPATH)" \
15
		DBWriter DBElement.props ./book-order.xml
14
#	java -classpath ".$(SEP)$(CPATH)" \
15
#		DBWriter DBElement.props ./book-order.xml
16 16

  
17 17
clean:
18 18
	-rm -f *.class Log

Also available in: Unified diff