CSDN博客

img sirix

Java、XML与数据库编程实践(三)

发表于2004/6/15 10:42:00  752人阅读

文件DbInput.java则是真正的数据拷贝部分:

 

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.sqlserver.SQLServerDriver");

           src = DriverManager.getConnection( srcPara.getUrl(), srcPara.getUsername(),srcPara.getPassword());

           dest =DriverManager.getConnection( destPara.getUrl(), destPara.getUsername(),destPara.getPassword());

           connformax= DriverManager.getConnection( destPara.getUrl(), destPara.getUsername(),destPara.getPassword());

        }

        //捕获加载驱动程序异常

        catch ( ClassNotFoundException cnfex ) {

           System.err.println( "装载 JDBC/ODBC 驱动程序失败。" );

           cnfex.printStackTrace();

           System.exit( 1 );  // terminate program

        } catch ( SQLException ex )

        {

            System.err.println( "连接失败" );

            ex.printStackTrace();

            System.exit( 1 );  // terminate program

        }          

    }

   

    public void copyproduct()

    {

        Statement st=null;

        ResultSet rset=null;

        String sqlstr;

        try {

           //执行SQL语句

           String query = "select * from production";

           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);

           //插入到产品表

           pid=getIdfromNumber("project","number",pnumber);

           if(pid==0) //插入一条新记录

           {

                pid=getMax("project");//System.out.println(pid);

                sqlstr="Insert into project(id,number) values("+pid+",'"+pnumber+"')";

                execute(destPara,sqlstr);

           }         

           //插入到零件表

           ljid=getIdfromNumber("product","number",ljnumber);

            if(ljid==0) //插入一条新记录

            {

                ljid=getMax("product");

                sqlstr="Insert into product(id,pid,number) values("+ljid+","+pid+",'"+ljnumber+"')";

                execute(destPara,sqlstr);

            }         

           //插入工具表

            cid=getIdfromNumber("componenttype","number",cnumber);

            if(cid==0) //插入一条新记录

            {

                cid=getMax("componenttype");

                sqlstr="Insert into componenttype(id,aid,number) values("+cid+","+ljid+",'"+cnumber+"')";

                execute(destPara,sqlstr);

            }                    

           }

 

        }

        catch ( SQLException sqlex ) {

           sqlex.printStackTrace();

        }      

    }  

 

0 0

相关博文

我的热门文章

img
取 消
img