📄 xml2dbtables.java
字号:
package MyNa.xml;
import MyNa.utils.Env;
import MyNa.utils.Logger;
import java.io.*;
import java.util.Stack;
import java.util.Hashtable;
import java.util.Enumeration;
import org.xml.sax.*;
import org.xml.sax.helpers.ParserFactory;
import com.sun.xml.parser.Resolver;
import java.sql.*; // communicate with database
public class Xml2DBTables extends SAXMiniLanguage {
// by construction, echo an input source to a database,
// assuming the dbdata.dtd in which the xml itself
// specifies the target database and provides user/pwd
Connection theDBConnection=null;
Statement theCreateTableStatement;
PreparedStatement theInsertPreparedStatement;
public Xml2DBTables (String fileName){
super(fileName);
theHandlerSet.put("row",new EndRowHandler());
theHandlerSet.put("user-info",new EndUser_Info());
theHandlerSet.put("header",new EndHeaderHandler());
theHandlerSet.put("dbdata",new EnddbDataHandler());
theHandlerSet.put("table",new EndTableHandler());
theHandlerSet.put("headers",new EndHeadersHandler());
lg.logIt("handlers placed");
}
class EndUser_Info extends EndHandler{
/*
the /user_info function,will open theDBConnection to
E.db_url with E.driver for E.uname with E.upass;
it will create theCreateTableStatement; these will be class vars rather than
in the environment. theCreateTableStatement will be used to create tables.
*/
public EndUser_Info(){}
public void doIt() throws SAXException {
CallStackItem cSI=(CallStackItem)theCallStack.pop();
lg.logIt("EndUserInfo doIt on "+cSI.name+"; kidnum="+cSI.kidNum);
String driver=theEnv.getStr("driver");
String db_url=theEnv.getStr("db_url");
String uname=theEnv.getStr("uname");
String upass=theEnv.getStr("upass");
lg.logIt("driver="+driver+";db_url="+db_url+";uname="+uname+";upass="+upass);
try{
Class.forName(driver);
theDBConnection= DriverManager.getConnection(db_url,uname,upass);
theCreateTableStatement=theDBConnection.createStatement();
lg.logIt("tableCreatestatement created.");
}catch(ClassNotFoundException e)
{throw new SAXException ("driver not found error", e);}
catch(SQLException e)
{throw new SAXException ("SQL error", e);}
}
}
class EndHeaderHandler extends EndHandler{
public EndHeaderHandler(){} // leaves
public void doIt() throws SAXException {
CallStackItem cSI=(CallStackItem)theCallStack.pop();
lg.logIt("EndHeaderHandler doIt on "+cSI.name+"; kidnum="+cSI.kidNum);
cSI.attrs.put("name",cSI.name); // attrs never null for header
pushVal(cSI.attrs);
}
}
class EndTableHandler extends EndHandler{
public EndTableHandler(){}
public void doIt() throws SAXException {
try{
CallStackItem cSI=(CallStackItem)theCallStack.pop();
lg.logIt("EndTableHandler doIt on "+cSI.name+"; kidnum="+cSI.kidNum);
theInsertPreparedStatement.close();
}catch(SQLException e)
{throw new SAXException ("SQL error", e);}
}
}
class EnddbDataHandler extends EndHandler{
public EnddbDataHandler(){}
public void doIt() throws SAXException {
try{
CallStackItem cSI=(CallStackItem)theCallStack.pop();
lg.logIt("EnddbDataHandler doIt on "+cSI.name+"; kidnum="+cSI.kidNum);
theCreateTableStatement.close();
theDBConnection.close();
}catch(SQLException e)
{throw new SAXException ("SQL error", e);}
}
}
class EndRowHandler extends EndHandler{
public EndRowHandler(){}
public void doIt() throws SAXException {
CallStackItem cSI=(CallStackItem)theCallStack.pop();
lg.logIt("EndRowHandler doIt on "+cSI.name+"; kidnum="+cSI.kidNum);
try{
String [] names=(String[])theEnv.get("header_names");
String [] types=(String[])theEnv.get("header_types");
for(int i=1;i<=names.length;i++)
theInsertPreparedStatement.setString(i,
(String)theEnv.get(names[i-1]));
lg.logIt("strings all set");
theInsertPreparedStatement.executeUpdate();
theInsertPreparedStatement.clearParameters();
}catch(SQLException e)
{throw new SAXException ("SQL error", e);}
}
}
class EndHeadersHandler extends EndHandler{
/*
the /headers function picks up the attrlists; creates the table
if needed with theCreateTableStatement; it also opens
theInsertPreparedStatement to insert new rows into the current
statement. This will be closed by /table.
Notice that if there are five headers, then the valstack
consists of five attrlists /headers uses them and pops them off,
but E.header_names becomes the list of names as an array of strings,
and E.header_types becomes the list of types as an array of strings,
while E.tableName becomes the table name itself.
Meanwhile, <table name="thetablename"> is on the stack, right under
the <headers> which we pop off on the first line.
*/
public void createTable(String name,String[]fldnames,
String[]fldtypes,boolean doReset){
//String createQ="CREATE TABLE COURSEBOOKS (SectionID TEXT, ItemCode TEXT);
try{
lg.logIt("createTable "+name+" with "+fldnames.length+" fields");
int N=fldnames.length;
String createStr="CREATE TABLE "+name+" (";
if(N>0)createStr+=fldnames[0]+" "+fldtypes[0];
for(int i=1;i<N;i++)createStr+=", "+fldnames[i]+" "+fldtypes[i];
createStr+=")";
lg.logIt("createStr="+createStr);
try{theCreateTableStatement.execute(createStr);
}catch(SQLException e){lg.logIt("createTable exception: "+e);}
// this table may well already exist;
if(doReset){
String delStr="DELETE * FROM "+name+"";
theCreateTableStatement.execute(delStr);
}
}catch(SQLException e){lg.logIt("createTable exception: "+e);}
}
public void createInserter(String name,String[]fldnames,String[]fldtypes)
throws SAXException {
//String insertQ="INSERT INTO COURSEBOOKS VALUES (?,?)"
try{
int N=fldnames.length;
String insertStr="INSERT INTO "+name+" VALUES (";
if(N>0)insertStr+="?";
for(int i=1;i<N;i++)insertStr+=", ?";
insertStr+=")";
lg.logIt("insertStr="+insertStr);
theInsertPreparedStatement=theDBConnection.prepareStatement(insertStr);
}catch(SQLException e){lg.logIt("createInserter exception: "+e);
throw new SAXException("createInserter error",e);}
// this table may well already exist.
}
public EndHeadersHandler(){}
public void doIt() throws SAXException {
CallStackItem cSI=(CallStackItem)theCallStack.pop();
lg.logIt("EndHeadersHandler doIt on "+cSI.name+"; kidnum="+cSI.kidNum);
int N=cSI.kidNum;
String[]header_names=new String[N];
String[]header_types=new String[N];
for(int i=N-1;i>=0;i--){
Hashtable aL=(Hashtable)theValStack.pop();
header_names[i]=(String)aL.get("name");
header_types[i]=(String)aL.get("fieldType");
}
CallStackItem tableCSI=(CallStackItem)(theCallStack.peek());
String tableName=tableCSI.name;
Hashtable attrs=tableCSI.attrs;
String reset=(null==attrs?null:(String)attrs.get("reset"));
boolean doReset="true".equals(reset);
createTable(tableName,header_names,header_types,doReset);
createInserter(tableName,header_names,header_types);
theEnv.put("header_names",header_names);
theEnv.put("header_types",header_types);
theEnv.put("tableName",tableName);
}
}
public static void main(String[]args) throws Exception{
/* called as
java MyNa.xml.Xml2DBTables DBSrc.xml
where DBSrc.xml is the template file. (samples provided as
MyNa\xml\pbdata.xml and others)
*/
MyNa.utils.Logger lg=new MyNa.utils.Logger();
if(args.length!=1){
System.out.println("Usage:");
System.out.println(" java MyNa.xml.Xml2DBTables DBSrc.xml");
System.out.println("where DBSrc.xml followed dbdata.dtd");
return;}
Xml2DBTables x2dbt=new Xml2DBTables(args[0]);
x2dbt.doIt();
}
} // end Xml2DBTables class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -