File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet ,databases & javaMail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet ,databases & javaMail" Watch "Servlet ,databases & javaMail" New topic
Author

Servlet ,databases & javaMail

clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
Write a Servlet to do the following:
The Database name will be supplied in the parameter dbname - this will be passed using either the Get or Post methods so your servlet should cater for both
Check for the existence of the MS Access database table agents and if it exists, delete it
Create a Database table as follows:
table name:agents
field:enonumeric
field:trregionalphanumeric
field:trcountryalphanumeric

If the Servlet encounters any errors it should email details of the errors, including a complete stack trace as follows:
EMAILED from the server to the account xyz@yyy.net, the from address should be abc@yyy.net, the subject should be "Table Creation Failed"
The body should contain a summary of the error.
The stack trace should be attached to the email as an ASCII text file.
1.For the first part I tried using
if(rs.next()){drop table agents;} where rs is the resultset.

Although the table agents get deleted if it exists,but ( java.sql.SQLexception : No ResultSet was produced)
is thrown.Is there any other way out?

2.For the second part I get an SQLException with the following code when I pass the path
("c:\agentpro.mdb") for the input parameter "tt".How do i retrieve the agents table which is in agentpro.mdb,so that I can use it in my
program.
the code :
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class agentjdbcServlet extends HttpServlet
{
String str,str1;

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);
out.println("melly");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:agent");
Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("Select * from "+str1+" ");
while(rs.next())
{
str=rs.getString(1);
System.out.println(str);
}


rs.close();
stmt.close();
con.close();
}catch(Exception e){System.out.println("E is "+e.toString());}
}
}
The html file:
<html>
<body bgcolor="#ccffff">
<form action="http://localhost:8080/servlet/agentjdbcServlet" method="post"/>
<input type="text" name="tt"/>
<br><br/>
<input type="submit" value="Yes"/>
</body>
</html>
3 For the third part I am using JavaMail Api.I am just working on this part.
How do I go about the error and attachment stuff.
the code:

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Mail
{
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "ABC");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
static public void main(String g[])
{
Mail m=new Mail();
try
{
m.postMail("abc@yyy.net","Hi","Ok","xyz@yyy.com",ServerName);
}catch(Exception e){}
}
}
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
C'mon no replies.Help me out.Let me know if I am going right
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Sun and JGuru have provided an excellent JavaMail tutorial, which has a section about working with attachments.
If you're wondering how to get a stack trace into a string, you can use this method:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
clyde melly
Ranch Hand

Joined: Sep 04, 2003
Posts: 152
c'mon help.Where am i going wrong
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You'll have to be more specific. Are you getting exceptions? Is your code not compiling?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet ,databases & javaMail