File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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

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, the from address should be, 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({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
the code :
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class agentjdbcServlet extends HttpServlet
String str,str1;

public void doPost(HttpServletRequest req,HttpServletResponse res)
throws IOException, ServletException
PrintWriter out = res.getWriter();
System.out.println("str1 : "+str1);
Connection con=DriverManager.getConnection("jdbc dbc:agent");
Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("Select * from "+str1+" ");

}catch(Exception e){System.out.println("E is "+e.toString());}
The html file:
<body bgcolor="#ccffff">
<form action="http://localhost:8080/servlet/agentjdbcServlet" method="post"/>
<input type="text" name="tt"/>
<input type="submit" value="Yes"/>
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("", "ABC");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
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.setContent(message, "text/plain");
static public void main(String g[])
Mail m=new Mail();
}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

Joined: Jan 23, 2002
Posts: 11962
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

Joined: Jan 23, 2002
Posts: 11962
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