Obtain mysql content, generate an xml file, and send it as webservice
In fact, three knowledge points are integrated, including how to access mysql, how to generate xml files, and how to use webservice development.
Okay, let's not talk about it. stick the code directly.
Package com. service; import java. io. fileOutputStream; import java. io. IOException; import java. SQL. *; import org. jdom. *; import org. jdom. output. XMLOutputter; public class NewsImpl implements NewsInterface {public String GetNews (int StartPage, int EndPage) {// Create a root node list; Element root = new Element ("xyt "); // add the root node to the Document; Document Doc = new Document (root); Connection conn = null; Statement stmt = null; // fo The r loop can be replaced with the result set operation for traversing database tables; try {Class. forName ("org. gjt. mm. mysql. driver "). newInstance (); String url_connect = "jdbc: mysql: // localhost/nxu_life? User = root & password = 12345 & useUnicode = true & characterEncoding = gb2312 "; // first name of your database try {conn = DriverManager. getConnection (url_connect);} catch (SQLException e0) {// TODO Auto-generated catch blocke0.printStackTrace ();} try {stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE);} catch (SQLException e00) {// TODO Auto-generated catch blocke00.printStackTrace ();} String SQL = "select page_id, title, url, publishtime, page, department from news where page_id> = "+ StartPage +" and page_id <= "+ EndPage; try {ResultSet rs1_stmt.exe cuteQuery (SQL); while (rs. next () {String page_id = rs. getString ("page_id"); String title = rs. getString ("title"); String url = rs. getString ("url"); String publishtime = rs. getString ("publishtime"); String page = rs. getString ("page"); String department = rs. getString ("department"); // Create a Node user; Element elements = new Element ("news"); // add a property id to the user node; elements. setAttribute ("id", page_id); // add a subnode to the user node and assign a value; // replace "name" in new Element ("name") with the corresponding fields in the table, in setText ("xuehui"), replace "xuehui with the record value in the table; elements. addContent (new Element ("url "). setText (url); elements. addContent (new Element ("title "). setText (title); elements. addContent (new Element ("publishtime "). setText (publishtime); elements. addContent (new Element ("page "). setText (page); elements. addContent (new Element ("department "). setText (department); // add a user subnode to the parent node list; root. addContent (elements) ;}} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} catch (InstantiationException e1) {// TODO Auto-generated catch blocke1.printStackTrace ();} catch (IllegalAccessException e2) {// TODO Auto-generated catch blocke2.printStackTrace ();} catch (ClassNotFoundException e3) {// TODO Auto-generated catch blocke3.printStackTrace ();} XMLOutputter XMLOut = new XMLOutputter (); string XMLString = XMLOut. outputString (Doc); return XMLString; // System. out. println (XMLString );}}