• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

servlets and xml

 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. I thought that was a statement, not a question. I'm not really sure what are you asking? The assignment given to you is telling that the database name will be passed as a request parameter. So, when making a connection to the database server, you will need to use this database name as part of the connection string.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In ur previous reply u mentioned the database name which i was asking u about.how do I do that.retrieve table name by passing the database name.
Having problem with the particular stuff as to how should the value of flag be true once the program is re run again.For the testing purpose,as agents is in the database it is deleted.Now when I create a
table agents in the database again and run the program the agents table is deleted as flag is false.How can I preserve the value of flag to be true.Will the persistant modifier be of any use.

Again the database name has to be passed in the parameter "tt".How do I
do that?Passing the table name is pretty easy and just for testing purpose I'm doing that,but ultimately as per the question I'll have to pass the database name.
The database name is agentpro.mdb and agents table resides here.So when I pass the parameter I will have to pass the complete path "c:\clydemelly\agentpro.mdb".Correct me if I am going wrong.
Code:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class agentjdbcServlet extends HttpServlet
{

String str,str1;
boolean flag = false ;

public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
str1=req.getParameter("tt");
System.out.println("str1 : "+str1);

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:agent");
Statement stmt=con.createStatement();

if(str1.equals("agents") && flag==false)
{
flag=true;
stmt.executeUpdate("drop table agents");

}
stmt.close();
con.close();
System.out.println("flag is :"+flag);
}
catch(Exception e)
{
System.out.println("E is "+e.toString());

}
}
}
-----------------------------------------------------------------------
For the insert and rejected records how do i go about.The code

import java.sql.*;
public class DuplicateInsert
{
static int results,cnt,rec_load,rec_rej;
DuplicateInsert()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:xjdbc");
Statement stmt=con.createStatement();
results=stmt.executeUpdate("insert into ord(oid) values(97)");
if(results>0)
{
rec_load++;
System.out.println("No of records inserted : "+rec_load);
}
else
{
rec_rej++;
}

System.out.println("Total No of records inserted : "+cnt);
stmt.close();
con.close();
}
catch(Exception e)
{
++rec_rej;
System.out.println("No of records rejected : "+rec_rej);
}
}
static public void main(String g[])
{
DuplicateInsert u=new DuplicateInsert();
System.out.println("Total No of records rejected : "+rec_rej);
}
}
-----------------------------------------------------------------------
Now lets say there are no records in the database.So when i run the program i get 1 as the number of record inserted.When I re run the program i get 1 as the number of record rejected.
When I re run the program I will again get 1 as the number of record rejected.How to I save the particular value for total number of records rejected.The same for the case with total no. of records inserted.Hope i am clear with my query.
Lasse please help me find something on the schema and java mail attachment stuff(combination).Some sample and running program,close enough to my assignment will really help me out.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In ur previous reply u mentioned the database name which i was asking u about.how do I do that.retrieve table name by passing the database name.

I still don't get it. As I see it, you don't need to "fetch the table name". The table name is always the same, "agents", right?

Having problem with the particular stuff as to how should the value of flag be true once the program is re run again.

If you have a static boolean variable in your servlet class, which you set to true/false during the first execution, then all subsequent executions know that they shouldn't try to delete and recreate the table.

Again the database name has to be passed in the parameter "tt".How do I do that?Passing the table name is pretty easy and just for testing purpose I'm doing that,but ultimately as per the question I'll have to pass the database name.

What's the difference between passing the table name and passing the database name? You can use HttpServletRequest.getParameter(...). Although, I still think you should just hardcode the table name to "agents".

The database name is agentpro.mdb and agents table resides here.So when I pass the parameter I will have to pass the complete path "c:\clydemelly\agentpro.mdb".Correct me if I am going wrong.

I don't know a damn thing about MS Access so I can't say whether the database name needs to be a full path or something else. Ask this in the JDBC forum (and only this, not the whole XXX lines of text and code!).

For the insert and rejected records how do i go about.The code
...
Now lets say there are no records in the database.So when i run the program i get 1 as the number of record inserted.When I re run the program i get 1 as the number of record rejected.

Does the executeUpdate() throw an exception? Is "OID" a primary key field?

Lasse please help me find something on the schema and java mail attachment stuff(combination).Some sample and running program,close enough to my assignment will really help me out.

I'll see if I have time.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes lasse OID is primary key and no exception is thrown.I tried keeping the
static stuff in case of insert and rejected records,but then getting total rejected and inserted was a problem.just go thru the code and let me know where do i need to make changes.Will work On JAMES today as tomorrow is the deadline.Still have to work on schema and attachment part.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So MS Access is so "smart" that it just silently ignores inserts which would compromise the primary key constraint... Nice. Anyway, the reason why only the first insert is considered success is probably that you're always inserting the hardcoded value "97".
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse i'm not always inserting the value"97".It was just an example.The next time i try to insert it wud give an exception.Lasse and that static stuff doesn't work up.On re-running the servlet the flag value will be again set to false i.e whatever set in the program.Same for the insert and rejected record stuff.I just want to know that what changes should i make so that it works fine.Still waiting for mail servlet,schema and attachment.Lasse wud be thankful if u can help me out today and i give the assignment on time.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "re-running the program"? If your servlet has a static variable, the value of that variable does persist over subsequent incoming HTTP requests.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cud u provide me with an example and how to go about as per ur previous reply
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse having problems with these :
Just a favour,check for this schema and see if its valid.
<?xml version="1.0"?>
<xs:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xs:element name="agents" type="agType">
<xs:complexType name="agType">
<xs:sequence>
<xs:element name="agent"
minOccurs="1"
maxOccurs="unbounded"/>
<xs:complexType>
<xs:sequence>
<xs:element name="Address" type="addressType"
minOccurs="1"
maxOccurs="unbounded">
<xs:complexType name="addressType">
<xs:sequence>
<xs:element name="line1" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="line2" type="xsd:string"/>
<xs:element name="postcode" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="region" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="country" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="tradingaddress" type="addressType"/>
<xs:element name="legaladdress" type="addressType"/>
<xs:attribute name="iatanumber" use="required" type="xs:integer"/>
<xs:attribute name="name" use="required" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
As per the question
The file must be validated against the XML Schema - any validation errors should be EMAILED from the server to the account [email protected], the from address should be [email protected], the subject should be "XML Parser Error in File: "+filename
How do I use this schema stuff for the attachment part.Just can't get through.
-----------------------------------------------------------------------
The code for MailServlet:
import java.io.*;
import java.util.*;
import java.text.*;
import javax.mail.*;
import javax.servlet.*;
import javax.activation.*;
import javax.servlet.http.*;
import javax.mail.internet.*;

public class MailServlet extends HttpServlet {
String str1;

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html");
PrintWriter out = response.getWriter();
str1=request.getParameter("thefile");
System.out.println("str1 : "+str1);

//Sending Email describing

sendEmail();

}

//End of doGet Method

public void sendEmail(){

boolean debug = false; // change to get more information


try {
Properties prop = new Properties();
prop.put("mail.host", "your_stmp_goes_here"); //SMTP goes here

Session mailConnection = Session.getDefaultInstance(prop, null);
mailConnection.setDebug(debug);
Message msg = new MimeMessage(mailConnection);

InternetAddress myaddress = new InternetAddress("[email protected]", "YourName");
InternetAddress sendingTo = new InternetAddress("[email protected]", "OtherPersonsName");

msg.setContent(msg, "text/plain");
msg.setFrom(myaddress);
msg.setRecipient(msg.RecipientType.TO, sendingTo);
msg.setSubject("XML Parser Error in File: ");




// Create the msg part
BodyPart msgBodyPart = new MimeBodyPart();
// Fill the msg
msgBodyPart.setText("XML File");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(msgBodyPart);
// Part two is attachment
msgBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("filename");
msgBodyPart.setDataHandler(new DataHandler(source));
msgBodyPart.setFileName("filename");
multipart.addBodyPart(msgBodyPart);
// Put parts in msg
msg.setContent(multipart);


// Send the msg
Transport.send(msg);
}
catch (Exception e) {
e.printStackTrace();
}


}

}

-----------------------------------------------------------------------
Finally with the previous insert and rejected part.And then again that flag value.Plz lasse help me out to finish off.Will be very greatful.u have helped me out throughout my assignment.Just want to put everything in running condition.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clyde,
If you would have followed my instructions, you would have found out already what's wrong with your schema. For starters, you declare a namespace "xsd" but all your elements are referring to "xs" and your XML elements are not properly encapsulated ("<foo><bar></foo></bar>" is not valid XML...).
Use the
online validator as I have suggested and you will know immediately what's wrong with your schema.
Also, you really should indent your document properly. This is what it looks like right now:
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse
As per the question
the file must be validated against the XML Schema - any validation errors should be EMAILED from the server to the account [email protected], the from address should be [email protected], the subject should be "XML Parser Error in File: "+filename
The body should contain details of the error, including the line at which the error occurred and the XML file that failed to validate should be attached to the email.
In the previous reply I had mailed the mail servlet.
This is the code for getting validation done along with the error.
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import java.io.IOException;
class SaxError implements ErrorHandler
{
public void warning(SAXParseException ex)

{

System.out.println("Warning ---");
System.out.println("Line number : " +ex.getLineNumber());
System.out.println("Column number : " +ex.getColumnNumber());
System.out.println("Message : "+ex.getMessage());
}

public void error(SAXParseException ex)

{

}


public void fatalError(SAXParseException ex) throws SAXException

{

System.out.println("FatalError ---");
System.out.println("Line number : " +ex.getLineNumber());
System.out.println("Column number : " +ex.getColumnNumber());
System.out.println("Message : "+ex.getMessage());
}

}

public class SAXDemoErrorHandler
{
public static void main(String arg[])
{
try
{
if(arg.length !=1){
System.out.println("Usuage : not Working");
System.exit(0);
}
XMLReader reader=XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
ErrorHandler demo=new SaxError();
reader.setErrorHandler(demo);
reader.setFeature("http://xml.org/sax/features/validation",true);
reader.parse(arg[0]);
}
catch(IOException e){
System.out.println("error reading" + e.getMessage());
}
catch(SAXException ex)
{
System.out.println("Error : " + ex.getMessage());
}
}
}
How do I go about in my mail servlet program to include this particular stuff.i.e if error occurs then my function sendmail() in the mail servlet should be called and then the process for sendmail would be executed.Lasse the problem is I'm just can't get to put the things together.So just look into and advise me how to go about.Hope i'm being clear.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your ErrorHandler, store the error messages into a member variable (a StringBuffer would do) instead of writing them into System.out. Then, copy the code from main() into your servlet's doPost() method and after trying to parse the XML, make your servlet ask the ErrorHandler for the error messages. If it returns something, have your servlet call sendEmail() giving the error message as a parameter.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will work on your previous reply right now.How do i go about this rejected item file.Will send you the code once i'm done thru.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse getting validation error with the validator which u told to check the schema.can't figure out.i made changes in the schema,but still don't know whether its correct.
The '1' character, hexadecimal value 0x31, cannot begin with a name. Line 13, position 63.


<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="agents">
<xs:complexType>
<xs:sequence>
<xs:element name="agent" type="xs:string">
<xs:complexType>
<xs:sequence>

<xs:element name="tradingaddress" type="address" minOccurs=1 maxOccurs="unbounded"/>
<xs:element name="legaladdress" type="address" minOccurs=1 maxOccurs="unbounded"/>

<xs:complexType name="address">
<xs:sequence>
<xs:element name="line1" type="xs:string" minOccurs=1 maxOccurs="unbounded"/>
<xs:element name="line2" type="xs:string"/> <xs:element name="postcode" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>

<xs:element name="region" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="country" type="xs:string" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType>

</xs:sequence>

<xs:attribute name="iatanumber" type="xs:integer" use="required"/>
<xs:attribute name="name" type="xs:string" use="required" />

</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:schema>
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
xml file.just a shorter version.checking for a person's details.
<?xml version="1.0"?>
<agents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="checkag.xsd">

<agent iatanumber="101" name="kayv">
<tradingaddress>
<line1>418 Dalamal Tower</line1>
<line2>Nariman Point</line2>
<postcode>400101</postcode>
<region>Mumbai</region >
<country>India</country>
</tradingaddress>
<legaladdress>
<line1>Blue Moon</line1>
<line2>Mith Chouki</line2>
<postcode>300101</postcode>
<region>Mumbai</region >
<country>India</country>
</legaladdress>
</agent>
</agents>
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's "line 13, position 63" bolded for you:
<xs:element name="tradingaddress" type="address" minOccurs=1 maxOccurs="unbounded"/>
What's missing? Hint: all the other attributes have them...
[ October 03, 2003: Message edited by: Lasse Koskela ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, here's your schema document's current "hierarchy". You might want to fix that too (the elements in both ends of the vertical lines should be the same...):
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse but what is the error about in the schema.can't figure out.
Regarding my query for MailServlet
Mail Servlet code along with ErrorHandler

"ask the ErrorHandler for the error messages."

But my SaxError class has methods which are void.Can I use the one which u had given earlier.

This function will be for the SaxError class? But my class closes and then MailServlet class begins. Or else close SaxError class at the end. Getting a bit confused.
Suppose this function returns something, then how do I use it in my MailServlet.

EDIT: I added the CODE tags for you...
[ October 04, 2003: Message edited by: Lasse Koskela ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what is the error about in the schema.can't figure out.


minOccurs=1 should be minOccurs="1"
You really should read some tutorial about XML and document well-formedness.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lasse what with the mail servlet program.i had put that minOccurs in quotes
"1".check my previous replies.I also have been reading stuff on xml but this was an error i didn't have an idea about.So i asked u to point out where was the problem.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've commented your code to help you forward:
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="agents">
<xs:complexType>
<xs:sequence>
<xs:element name="agent" type="xs:string">
<xs:complexType>
<xs:sequence>

<xs:element name="tradingaddress" type="address" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="legaladdress" type="address" minOccurs="1" maxOccurs="unbounded"/>

<xs:complexType name="address">
<xs:sequence>
<xs:element name="line1" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="line2" type="xs:string"/>
<xs:element name="postcode" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>

<xs:element name="region" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
<xs:element name="country" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:sequence>

<xs:attribute name="iatanumber" type="xs:integer" use="required"/>
<xs:attribute name="name" type="xs:string" use="required" />

</xs:complexType>
</xs:element>

</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

I made changes in the schema for minOccurs.Now I get an error
The 'http://www.w3.org/2001/XMLSchema:complexType' element is not supported in this context. An error occurred at , (18, 4).
Do i have to declare my address type before I refer to it.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the mail servlet program when
if (demo.getErrorMessages() != null)
{
sendEmail(demo.getErrorMessages(), str1);
}

}

But getErrorMessages will not be accessible.
----------------------------------------------------------------------------
For this part how do I go about.
DataSource source = createDataSource(errorMessage);
Lasse the schema part is the most important one at this period of time.Just don't have much time.Help me out on that also please.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've modified your schema to make it conform to the standard.
Regarding the createDataSource(String) method, have you even tried it? See what methods are declared in the interface and figure out how to implement them.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse with time running out I just was not able to concentrate on particular stuff so that is why I was asking for inputs.This assignment is a learning experience.Still lot to learn and u have just been the right person guiding me thru.I did check up for DataSource stuff.will send u the code and then let me know whether i'm correct.I had another query as how do i access the error method as the SaxError class is closed.Just check.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I had another query as how do i access the error method as the SaxError class is closed.Just check.


What do you mean "the class is closed"? You can invoke methods on an instance of a class as long as you have the reference to it. It has got nothing to do with e.g. where the class is defined in your source files.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse for your previous reply,my mailServlet is not extending this SAXError class.Just check,coz I am getting an error when i run the program stating that the function geterrorcodes is inaccesible.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUE:
Testing for the records inserted and rejected
Code :
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.mail.*;
import javax.servlet.*;
import javax.activation.*;
import javax.servlet.http.*;
import javax.mail.internet.*;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import java.io.IOException;
public class JdbcInsertServlet extends HttpServlet {

StringBuffer errors = new StringBuffer();

static int results,rec_load,rec_rej;

Document agentsDoc = null;

Connection con = null;

Statement stmt = null;

int agid;

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


response.setContentType("text/html");
PrintWriter out = response.getWriter();

//Determine the file location
String agentsFile = "agenttest.xml";

//Create the XML document

try {
//Instantiate the parser and parse the file
DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder();
agentsDoc = docbuilder.parse(agentsFile);
} catch (Exception e) {
System.out.println("Cannot read the agents file: "+e.getMessage());
}


try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:xjdbc");
stmt=con.createStatement();


//Get the root element and all agents elements
Element agentsRoot = agentsDoc.getDocumentElement();
NodeList agents = agentsRoot.getElementsByTagName("agent");
for (int i = 0; i < agents.getLength(); i++) {
//For each agents, get the agent element
Element thisAgent = (Element)agents.item(i);


//Get iatano information
String thisAgentid = thisAgent.getAttribute("iatanumber");
System.out.println("Ino: " + thisAgentid);

agid = Integer.parseInt(thisAgentid);

out.println("Iata No is : " +agid);

//Get name information

String thisAgentname = thisAgent.getAttribute("name");
System.out.println("Name: "+thisAgentname);


//Loop through each trading address
NodeList traddress = thisAgent.getElementsByTagName("tradingaddress");
for (int j=0; j < traddress.getLength(); j++) {
Element thisAddress = (Element)traddress.item(j);

System.out.println("j" + j+ " "+"i "+ i);

//Get trading adress information from child
//elements

String l1 = thisAddress.getElementsByTagName("line1")
.item(0)
.getFirstChild().getNodeValue();

String l2 = thisAddress.getElementsByTagName("line2")
.item(0)
.getFirstChild().getNodeValue();



String pcode = thisAddress.getElementsByTagName("postcode")
.item(0)
.getFirstChild().getNodeValue();


String reg =thisAddress.getElementsByTagName("region")
.item(0)
.getFirstChild().getNodeValue();


String cty =thisAddress.getElementsByTagName("country")
.item(0)
.getFirstChild().getNodeValue();




//Loop through each legal address for the name and iatano
NodeList lgaddress = thisAgent.getElementsByTagName("legaladdress");
for (int k=0; k < lgaddress.getLength(); k++) {
Element thislgAddress = (Element)lgaddress.item(k);

System.out.println("k" + k+ " "+"i "+ i);

//Get trading adress information from child
//elements

String ll1 = thislgAddress.getElementsByTagName("line1")
.item(0)
.getFirstChild().getNodeValue();

String ll2 = thislgAddress.getElementsByTagName("line2")
.item(0)
.getFirstChild().getNodeValue();


String lpcode = thislgAddress.getElementsByTagName("postcode")
.item(0)
.getFirstChild().getNodeValue();

String lreg =thislgAddress.getElementsByTagName("region")
.item(0)
.getFirstChild().getNodeValue();

String lcty =thislgAddress.getElementsByTagName("country")
.item(0)
.getFirstChild().getNodeValue();



results = stmt.executeUpdate("insert into agents(iatano,trpostcode,lgregion) values("+agid+",'"+pcode+"','"+lreg+"')");


if(results>0)
{
++rec_load;
}
else
{
++rec_rej;
}

}
}


System.out.println("agid is : "+agid);
System.out.println("Errors : "+ errors);
}
System.out.println("Total No of records inserted : "+rec_load);
System.out.println("Total No of records rejected : "+rec_rej);
stmt.close();
con.close();

System.out.println("Over");
} catch (Exception e) {
++rec_rej;
System.out.println("Total no of records rejected : "+rec_rej);

}

} }

My sample agentstest.xml file :
<?xml version="1.0"?>
<agents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="agents.xsd">

<agent iatanumber="201" name="rai">
<tradingaddress>
<line1>418 Dalamal Tower</line1>
<line2>Nariman Point</line2>
<postcode>400101</postcode>
<region>Mumbai</region >
<country>India</country>
</tradingaddress>
<legaladdress>
<line1>21, Blue Moon</line1>
<line2>Mith Chouki</line2>
<postcode>300101</postcode>
<region>Mumbai</region >
<country>India</country>
</legaladdress>
</agent>
<agent iatanumber="701" name="tai">
<tradingaddress>
<line1>428 abcTower</line1>
<line2>zzd</line2>
<postcode>400101</postcode>
<region>UPT</region >
<country>India</country>
</tradingaddress>
<legaladdress>
<line1>11, Blue Moon</line1>
<line2>zzs</line2>
<postcode>300101</postcode>
<region>UPT</region >
<country>India</country>
</legaladdress>
</agent>

<agent iatanumber="202" name="pai">
<tradingaddress>
<line1>429 tyuTower</line1>
<line2>zze</line2>
<postcode>500101</postcode>
<region>UPT</region >
<country>India</country>
</tradingaddress>
<legaladdress>
<line1>22, Blue Moon</line1>
<line2>jji</line2>
<postcode>300101</postcode>
<region>SPT</region >
<country>India</country>
</legaladdress>
</agent>

</agents>

When I try to insert for the first time I get 3 as number of records inserted.Thats correct.When try to again insert through the same file,then for ino=201 i get rejected record as 1 and the program terminates.Does not read ahead for ino=701 and 202.In the database the iatanumber field is primary key.I require it reads ahead and rejected number is 3.
-----------------------------------------------------------------------
QUE:
As per the above question
The body should contain the following information:
Records Loaded: xx
Records Rejected: yy
(where xx and yy are the number of errors)
An xml files should be attached to the email as follows which should be of the following format:
<rejectedrecords>
<rejected>
<recordno>xx</recordno>note xx is the record no in the original file
<agent iatanumber="" name="">
<tradingaddress>
<line1></line1>
<line2></line2>
<line3></line3>
<postcode></postcode>
<region></region >
<country></country>
</tradingaddress>
<legaladdress>
<line1></line1>
<line2></line2>
<line3></line3>
<postcode></postcode>
<region></region >
<country></country>
</legaladdress>
</agent>
<reason>
the reason the record was rejected, this may include any
Java exception description
</reason>
</rejected>
</rejectedrecords>

I will get xx in the end.But how do i specify it in the xml format?
-----------------------------------------------------------------------
QUE:
Went through DataSource Interface and checked the methods.But how do I create an instance of it.
-----------------------------------------------------------------------
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by clyde melly:
Lasse for your previous reply,my mailServlet is not extending this SAXError class.Just check,coz I am getting an error when i run the program stating that the function geterrorcodes is inaccesible.

MailServlet shouldn't extend the SAXError class--it should just use it. Also, I'm pretty sure that the compiler should catch method visibility problems at compile-time.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're still not using the CODE tags when posting code snippets!
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by clyde melly:
When I try to insert for the first time I get 3 as number of records inserted.Thats correct.When try to again insert through the same file,then for ino=201 i get rejected record as 1 and the program terminates.Does not read ahead for ino=701 and 202.In the database the iatanumber field is primary key.I require it reads ahead and rejected number is 3.

The problem is that your code doesn't catch the SQLException thrown when the primary key constraint is violated and control jumps to the closest catch block.

Originally posted by clyde melly:
I will get xx in the end.But how do i specify it in the xml format?

What does "specify" mean?

Originally posted by clyde melly:
Went through DataSource Interface and checked the methods.But how do I create an instance of it.

You define a class that has implements DataSource as part of its signature. Then, you add methods that match the ones defined in the DataSource interface. Once you done this, you can say "new MyDataSource()" or something similar.
[ October 06, 2003: Message edited by: Lasse Koskela ]
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse how do i tackle with the problem of the rejected records then.I had checked earlier and it when into the catch block.Senderror method??u try it out if u think there shud not be an error.Regarding the datasource and rejected xml file stuff.Sorry just forget abt the code tags.Will keep in mind further on.
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for datasource the inputstream method??
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lasse just check the rejected xml file.in ur previous reply u asked specify.I have to put it in that format.If its a duplicate record have to capture all the info and dispaly it as a xml file.That wud be used in the body part as an attachment.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by clyde melly:
Lasse how do i tackle with the problem of the rejected records then.I had checked earlier and it when into the catch block.Senderror method??u try it out if u think there shud not be an error.Regarding the datasource and rejected xml file stuff.Sorry just forget abt the code tags.Will keep in mind further on.


You need to catch the SQLExceptions inside the loop so that the first rejected record will not break out of the loop.
In other words:instead of
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by clyde melly:
for datasource the inputstream method??

Yes? You need to return something that extends java.io.InputStream. If you'll take a look at what other classes are available in the java.io package, you'll find something suitable. Similarly, your DataSource implementation needs to implement the getOutputStream() method by returning something that extends java.io.OutputStream.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by clyde melly:
lasse just check the rejected xml file.in ur previous reply u asked specify.I have to put it in that format.If its a duplicate record have to capture all the info and dispaly it as a xml file.That wud be used in the body part as an attachment.


I looked at the XML file that you should return. What I don't see is what do you need to specify? Isn't it enough that you produce an XML document that follows the structure you showed?
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse just simply plug in the values.Thats it.Coz for a rejected record i have to get all info from name to tradingaddress,legaladdress.??.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by clyde melly:
Lasse just simply plug in the values.Thats it.Coz for a rejected record i have to get all info from name to tradingaddress,legaladdress.??.


You need to do something like the following to get access to the rejected records afterwards:
 
clyde melly
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the mail servlet program in order to access the geterrormessage method do i make this mail servlet class an inner class and then proceed or any other way out.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic