[Logo]
Forums Register Login
Need help Take out the null values from the ResultSet and Create a XML file
hi,
I am sorry i have to crosspost this but i need to solve this problem ASAP.
I would really appreicate if you can help me.
I wrote something which connects to Database and gets the ResultSet. From that ResultSet I am creating a XML file. IN my program these are the main two classes Frame1 and ResultSetToXML. ResultSetToXML which takes ResultSet & Boolean value in its constructor. I am passing the ResultSet and Boolean value from Frame1 class. I am passing the boolean value to get the null values from the ResultSet and then add those null values to XML File. When i run the program it works alright and adds the null and not null values to the file. But when i pass the boolean value to take out the null values it would not take it out and adds the null and not null values.
Please look at the code i am posing. I am showing step by step where its not adding the null values.
Any help is always appreciated.
Thanks in advance.
============================================================================
Frame1 Class
============
public class Frame1 extends JFrame{
private JPanel contentPane;
private XQuery xQuery1 = new XQuery();
private XYLayout xYLayout1 = new XYLayout();
public Document doc;
private JButton jButton2 = new JButton();
private Connection con;
private Statement stmt;
private ResultSetToXML rstx;
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
xQuery1.setSql("");
xQuery1.setUrl("jdbc dbc:SCANODBC");
xQuery1.setUserName("SYSDBA");
xQuery1.setPassword("masterkey");
xQuery1.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
xQuery1.setSql("Select * from Pinfo where pid=2 or pid=4");
jButton2.setText("Get XML from DB");

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException ex) {
System.err.print("ClassNotFoundException: ");
System.err.println(ex.getMessage());
}
try {
con = DriverManager.getConnection("jdbc dbc:SCANODBC","SYSDBA", "masterkey");
stmt = con.createStatement();
}
catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}

jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
contentPane.add(jButton2, new XYConstraints(126, 113, -1, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton2_actionPerformed(ActionEvent e) {
try{
OutputStream out;
XMLOutputter outputter;
Element root;
org.jdom.Document doc;
root = new Element("PINFO");
String query = "SELECT * FROM PINFO WHERE PID=2 OR PID=4";
ResultSet rs = stmt.executeQuery(query);
/*===========This is where i am passing the ResultSet and boolean=======
===========value to either add the null or not null values in the file======*/
rstx = new ResultSetToXML(rs,true);
} //end of try
catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
======================================================================================
ResultSetToXML class
====================
public class ResultSetToXML {
private OutputStream out;
private Element root;
private XMLOutputter outputter;
private Document doc;
// Constructor
public ResultSetToXML(ResultSet rs, boolean checkifnull){
try{
String tagname="";
String tagvalue="";
root = new Element("pinfo");
while (rs.next()){
Element users = new Element("Record");
for(int i=1;i<=rs.getMetaData().getColumnCount(); ++i){
tagname= rs.getMetaData().getColumnName(i);
tagvalue=rs.getString(i);
System.out.println(tagname);
System.out.println(tagvalue);
/*============if the boolean value is false it adds the null and not
null value to the file =====================*/
/*============else it checks if the value is null or the length is
less than 0 and does the else clause in the if(checkifnull)===*/
if(checkifnull){
if((tagvalue == null) || tagvalue.length() < 0 ){
users.addContent((new Element(tagname).setText(tagvalue)));
}
else{
users.addContent((new Element(tagname).setText(tagvalue)));
}
}
else{
users.addContent((new Element(tagname).setText(tagvalue)));
}
}
root.addContent(users);
}
out=new FileOutputStream("c:/XMLFile.xml");
doc = new Document(root);
outputter = new XMLOutputter();
outputter.output(doc,out);
}
catch(IOException ioe){
System.out.println(ioe);
}
catch(SQLException sqle){
}
}
}
Please don't post the same message in more than one forum. You obviously read more than one forum. The people who might answer your question do the same.
This thread is now closed. All replis to the one in XML
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


Reply locked

All times above are in ranch (not your local) time.
The current ranch time is
Dec 17, 2017 05:14:49.