开发类调用webservice并插入数据库:
新建个PACKAGE:com.noahark.dao
包括两个类:DBUtil(连接数据库)和ImportData(调用webservice)。
开发过程如下:
finish。
注:由于此处会用到ojdbc6.jar,提前导入ojdbc6.jar,
从网上下载jar包,导入path:
导入成功:
开发代码:
package com.noahark.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
publicclass DBUtil
{
publicstatic Connectionconn;
publicstatic PreparedStatementps;
publicstatic ResultSetrs;
publicstatic Statementst;
privatestatic Stringurl ="jdbc:oracle:thin:@ip:1521:sid";
privatestatic Stringuser ="username";
privatestatic Stringpassword ="password";
public Connection getConnection()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url,user,password);
}
catch (Exception e) {
e.printStackTrace();
}
returnconn;
}
}
新建类同上(略)。
需要导入dom4j-1.6.1.jar包。导入过程略。
开发代码如下:
package com.noahark.dao;
import gn.budget.service.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
public class ImportData {
privatestatic Connection conn;
privatestatic Statement st;
privatestatic ResultSet rs;
publicstatic void main(String[] args) throws IOException_Exception,DocumentException, SQLException{
//客户提供ServiceBudget
ServiceBudgetBudget = new ServiceBudgetService().getServiceBudgetPort();
Stringyear = "2016";
Stringmonth = "08";
//客户提供getBudgetMonthOccurrence方法
Stringresult = Budget.getBudgetMonthOccurrence(year, month);
System.out.println("输出webservice结果之前...!");
System.out.println(result);
Documentdocument =DocumentHelper.parseText(result);
Elementroot = document.getRootElement();
List
DBUtil jdbc = new DBUtil();
conn = jdbc.getConnection();
for(Element child : childElements) {
String sql2 = "insert intoOA_TEST(ACCOUNT,ENTITY,YEARS,PERIOD,PRODUCT,CYCLE,SCENARIO,VERSION,PROJECT,CUSTOM1,CUSTOM2,CUSTOM3,DATA,CREATED)values("+"'"+child.elementText("ysdycode")+"'"+","+"'"+child.elementText("kmcode")+"'"+","+"'"+child.elementText("year")+"'"+","+"'"+child.elementText("month")+"'"+",'PRODUCT'"+",'CYCLE'"+",'SCENARIO'"+",'VERSION'"+",'PROJECT'"+",'CUSTOM1'"+",'CUSTOM2'"+",'CUSTOM3',"+child.elementText("je")+",sysdate"+")"+"";
//String sql2 = "insert into OA_TESTvalues ('a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0,sysdate)";
DBUtil jdbc2 = new DBUtil();
Connection conn2 = jdbc.getConnection();
Statement st2 = conn2.createStatement();
st2.executeQuery(sql2);
st2.close();
conn2.close();
}
System.out.println("Success!");
}
}