Java, XML and Database Programming Practice (3)

zhaozj2021-02-16  48

Document dbinput.java is a real data copy section:

Import java.sql. *;

Public Class Dbinput

{

CONNECTION SRC = NULL;

Connection dest = NULL;

Connection connformax = null;

Connpara Srcpara;

CONNPARA DESTPARA;

Public dbinput () {}

Public void dbinit ()

{

DBXMLPARSER XMLPARSER = New DBXMLPARSER ("dbs.xml");

Srcpara = xmlparser.getsource ();

destpara = XmlParser.getDest ();

Try {

Class.Forname ("Sun.jdbc.odbc.jdbcodbcdriver");

Class.Forname ("com.microsoft.jdbc.sqlser.sqlserverdriver);

SRC = DriverManager.getConnection (srcpara.getUsername (), srcpara.getpassword ());

Dest = drivermanager.getConnection (destpara.getUrname (), destpara.getpassword ());

CONNFORMAX = drivermanager.getConnection (destpara.getUsername (), destpara.getpassword ());

}

// Capture the loading driver exception

Catch (classnotfoundexception cnfex) {

System.err.println ("Load JDBC / ODBC driver failed.");

CNFEX.PrintStackTrace ();

System.exit (1); // Terminate Program

} catch (SQLException EX)

{

System.err.println ("Connection Fail");

EX.PrintStackTrace ();

System.exit (1); // Terminate Program

}

}

Public void copyproduct ()

{

Statement ST = NULL;

ResultSet Rset = NULL;

String sqlstr;

Try {

/ / Execute SQL statement

String query = "select * from produuction";

ST = src.createstatement ();

Rset = st.executeQuery (query);

INT PID, LJID, CID, CIID

While (RSET.NEXT ())

{

String pnumber = RSET.GETSTRING (1);

String Ljnumber = RSET.GETSTRING (2);

String cnumber = RSET.GETSTRING (3);

// Insert the product table

PID = GetFromNumber ("Project", "Number", PNumber;

IF (PID == 0) // Insert a new record

{

PID = GetMax ("Project"); // system.out.println (PID);

SQLSTR = "Insert Into Project (ID, Number) VALUES (" PID ", '" PNumber ")"; Execute (Destpara, SQLSTR);

}

// Insert into the part table

Ljid = getIDFromNumber ("Product", "Number", LJNumber;

IF (ljid == 0) // Insert a new record

{

Ljid = getmax ("product");

SQLSTR = "INSERT INTO Product (ID, PID, Number) VALUES (" Ljid "," PID ", '" Ljnumber ")"

Execute (Destpara, SQLSTR);

}

// Insert the tool table

CID = GetFromNumber ("ComponentType", "Number", cnumber;

IF (CID == 0) // Insert a new record

{

CID = GetMax ("ComponentType");

SQLSTR = "Insert INTO ComponentType (ID, AID, Number) VALUES (" CID "," Ljid ", '" CNUMBER ")"

Execute (Destpara, SQLSTR);

}

}

}

Catch (sqlexception sqlex) {

SQLEX.PrintStackTrace ();

}

}

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

New Post(0)