简体   繁体   中英

I am trying to pull data from a database and store it into a xml file. I am using a Sql query "Select * from "for pulling data

I am working on a project where i need to get data from a database and store it in xml format. I am using the following code to pull data from database.The code is as follows

import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class Main {

      public static void main(String args[]) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Element results = doc.createElement("Results");
        doc.appendChild(results);

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=null;
        con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "system","1234");

        ResultSet rs = con.createStatement().executeQuery("select *from people");

        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();

        while (rs.next()) {
          Element row = doc.createElement("Row");
          results.appendChild(row);
          for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            Element node = doc.createElement(columnName);
            if (value != null)
                   node.appendChild(doc.createTextNode(value.toString()));
               else
                   node.appendChild(doc.createTextNode(""));
               row.appendChild(node);
               row.appendChild(node);

            row.appendChild(node);
          }
        }
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        StringWriter sw = new StringWriter();
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);

        System.out.println(sw.toString());

        con.close();
        rs.close();
      }
    }

I am getting entire data stored in the table people,but i need to pull data of particular person based on where condition in sql query like "select *from people where firstname=?". I tried many ways to get data of a particular person i am failing to get data. Please suggest me how to get data of a particular person from database.And what the changes i need to made for code to get data of a particular person.

What about getting the name as argument:

String searchedName = args[0]; 
ResultSet rs = con.createStatement().executeQuery("select * from people where firstname = '"+searchedName+"'");

As your code is in main method, you can pass the value through command line as: java Main Parameter_personName. Else you can read it from a text file and pass it as a parameter to your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM