Java, XML and Database Programming Practice

zhaozj2021-02-16  55

Author: sirix Email: nevinguo@163.com

When you start learning XML and database programming, everyone will start with a lot of documents and materials. The author is working in the work, and it is necessary to use it. Its feature is very simple, you have to use the Java language, import some data into the SQL database from the Access database.

demand:

Original Access database table structure:

Table: Production

Product model string type ......... Product number

Part map number string ......... Part number

Timber string .......................................

SQL data table structure:

Project product table

ID INT logo

Number ... varchar64 number

Product Part Table

ID INT logo

PID INT product identification number (associated with ID in the Project table)

Number ... varchar64 number

Production tool table corresponding to ComponentType and Parts

ID INT logo

AID INT Part Identification (association with the IDUCT table)

Number ... varchar64 number

The original numbers are required to be placed in the SQL three tables, and associated with the ID.

Considering that database connections may change, decide to use XML to configure data connections. The DBS.XML file content is as follows:

sun.jdbc.odbc.jdbcodbcdriver

JDBC: ODBC: ASRS

"

"

com.microsoft.jdbc.sqlserver.sqlserverdriver

JDBC: Microsoft: SQLServer: // localhost: 1433; DatabaseName = VXMS

vxms

Vxms

File Connpara.java, indicating database connection parameters represented by a class form.

Public Class Connpara

{

String dbclass = NULL;

String Url = NULL;

String username = null;

String password = NULL;

Public connpara () {}

Public Connpara (String Pdbclass, String Purl, String Puser, String PPassword)

{

dbclass = pdbclass;

URL = PURL;

UserName = PUSERNAME;

Password = ppassword;

}

Public string getdbclass () {return dbclass;} public string geturl () {return url;}

Public string getUsername () {return username;}

Public string getpassword () {return password;}

Public void setdbclass (string str) {dbclass = Str;}

Public void seturl (string str) {url = Str;}

Public void setusername (string str) {username = Str;}

Public void setpassword (string str) {password = Str;}

}

Document DBXMLPARSER.JAVA encapsulates the operation of the XML file.

Import javax.xml.parsers. *;

Import org.w3c.dom. *;

Import org.xml.sax. *;

Import java.io. *;

Public Class DbxmlParser

{

STATIC STRING XMLFILE;

Public dbxmlparser (String filename)

{

XMLFILE = filename;

}

Public static element loadingdocument ()

{

Try

{

//factory

DocumentBuilderFactory dcfactory = documentbuilderfactory.newinstance ();

// Document constructor

DocumentBuilder DB = dcfactory.NewDocumentBuilder ();

// Context document

Document doc = db.parse (XMLFile);

/ Root elements

Element root = doc.getdocumentelement ();

Return root;

} catch (ParserConfigurationException E) {

System.out.println ("ParserConfigurationException");

E.PrintStackTrace ();

} catch (ioexception e) {

System.out.println ("IOEXCEPTION");

E.PrintStackTrace ();

} catch (saxException e) {

System.out.println ("SAXException");

E.PrintStackTrace ();

} catch (exception e) {

E.PrintStackTrace ();

}

Return NULL;

}

Public connpara getsource ()

{

Element root = loadingDocument ();

IF (root == null) {return null;}

Nodelist nodes = root.getElementsBytagname ("Source");

IF (Nodes.getLength ()> 0)

{

Node node = nodes.Item (0);

String connclass = getChildElementValue (Node, "Class");

String url = getChildElementValue (Node, "URL");

String username = getChildElementValue (Node, "User"); string password = getChildElementValue (Node, "Password");

Return New Connpara (CONNCLASS, URL, Username, Password);

}

Return NULL;

}

Public connpara getDest ()

{

Element root = loadingDocument ();

IF (root == null) Return NULL;

Nodelist nodes = root.getElementsBytagname ("dest");

IF (Nodes.getLength ()> 0)

{

Node node = nodes.Item (0);

String connclass = getChildElementValue (Node, "Class");

String url = getChildElementValue (Node, "URL");

String username = getChildElementValue (Node, "User");

String password = getChildElementValue (Node, "Password");

Return New Connpara (CONNCLASS, URL, Username, Password);

}

Return NULL;

}

// Get the value of the child element

Private string getChildElementValue (Node Node, String Subtagname)

{

String Returnstring = ""

IF (Node! = NULL)

{

Nodelist children = node.getchildnodes ();

For (int innerloop = 0; innerloop

{

Node child = children.item (innerloop);

IF (child == null || child.getnodename () == null ||! child.getnodename (). Equals (subtagname))

CONTINUE;

Node GrandChild = child.getfirstchild ();

IF (grandchild.getnodevalue ()! = null)

Return grandchild.getnodeValue ();

}

}

Return Returnstring;

}

}

转载请注明原文地址:https://www.9cbs.com/read-21319.html

New Post(0)