By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,665 Members | 1,576 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,665 IT Pros & Developers. It's quick & easy.

Fetch values from database and store it in array

P: 25
Hi,

I am doing send some values(signup form::firstname,lastname,title,organization)
to mail id using javamail at the same time it should store in database, my problem is how to fetch values from database and store it in array.

I need urgently ,Please help me.




thanks in advance.
Aug 22 '07 #1
Share this Question
Share on Google+
20 Replies


10K+
P: 13,264
Hi,

I am doing send some values(signup form::firstname,lastname,title,organization)
to mail id using javamail at the same time it should store in database, my problem is how to fetch values from database and store it in array.

I need urgently ,Please help me.






thanks in advance.
You can connect to a database using JDBC. Do you know anything about it?
Aug 22 '07 #2

dmjpro
100+
P: 2,476
You can connect to a database using JDBC. Do you know anything about it?
Post the Code so far you did.
Then experts are here to solve your problem.
And remember, while you posting the code then don't forget to use Code Tag.
Please read the guidelines before Posting.
OK.

Best of luck with your try.

Kind regards,
Dmjpro.
Aug 22 '07 #3

P: 25
Hi,
I post the code here,

Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import java.io.*;
  4. import java.sql.*;
  5. import javax.mail.*;
  6. import javax.mail.internet.*;
  7. import javax.mail.event.*;
  8. import java.net.*;
  9. import java.util.*;
  10.  
  11.  
  12. public class mail3 extends HttpServlet
  13.  
  14. {
  15.     public  void doGet(HttpServletRequest request,HttpServletResponse response)  throws ServletException, IOException
  16.     {
  17.  
  18.         PrintWriter out=response.getWriter();
  19.         response.setContentType("text/html");
  20. try
  21. {
  22.            Properties props=new Properties();
  23.            props.put("mail.smtp.host","mail.cygnussoftwares.com");
  24.            Session session =  Session.getDefaultInstance(props,null);
  25.            String fname = request.getParameter("firstname");
  26.            String lname = request.getParameter("lastname");
  27.            String tit = request.getParameter("Title");
  28.            String org = request.getParameter("organization");
  29.            String add1 = request.getParameter("Address1");
  30.            String add2 = request.getParameter("address2");
  31.            String city = request.getParameter("city");
  32.            String state = request.getParameter("state");
  33.            String country = request.getParameter("country");
  34.            String organizationSize= request.getParameter("turnover");
  35.            String phone= request.getParameter("phone");
  36.            String email= request.getParameter("emailid");
  37.            String product= request.getParameter("product");
  38.  
  39.            //String recipients= "devteam@yahoo.com" 
  40.  
  41.            //={"devteam@yahoo.com","sathya@gmail.com","pnithya@yahoo.co.in"}; 
  42.  
  43.  
  44.            String k=fname+lname+tit+org+add1+add2+city+state+country+organizationSize+product+phone+email;
  45.            String subject="Product Inquiry Form";
  46.  
  47.             String FirstName="FirstName:";
  48.             String LastName="LastName:";
  49.             String Title="Title:";
  50.             String Organization="Organization:";
  51.             String Address1="Address1:";
  52.             String Address2="Address2:";
  53.             String City="City:";
  54.             String State="State:";
  55.             String Country="Country:";
  56.             String OrganizationSize="OrganizationSize:";
  57.             String Phone="Phone:";
  58.             String Email="E-Mail:";
  59.             String Product="Product";
  60.  
  61.             //String recipients;
  62.  
  63.            try
  64.         {
  65.     //Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  66.     //Connection cn = DriverManager.getConnection("jdbc:microsoft:sqlserver://CSISERVER2:1433;DatabaseName=web Hosting","sa","sa") ;
  67.     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  68.     Connection cn = DriverManager.getConnection("jdbc:odbc:send", " ", " ");
  69.     Statement st = cn.createStatement();
  70.     ResultSet rs = st.executeQuery("select * from send ");
  71.  
  72.     //st.executeUpdate("insert into sendmail values('"+fname+"','"+lname+"','"+tit+"','"+org+"',
  73. '"+add1+"','"+add2+"','"+city+"','"+state+"',
  74. '"+country+"','"+organizationSize+"','"+product+"','"+phone+"','"+email+"')");
  75.     //out.println("Record inserted successfully ........");
  76.  
  77.  
  78.     String to=""; 
  79.  
  80.    while(rs.next())
  81.    {
  82.    to = rs.getString(1);
  83.  
  84.    }  
  85.    System.out.println("the name is :"+to);
  86.  
  87.    }
  88.    catch(Exception e)
  89.    {
  90.       e.printStackTrace();
  91.    }
  92.  
  93.            MimeMessage message = new MimeMessage(session);
  94.            message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
  95.  
  96.            /*InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
  97.            for (int i = 0; i < recipients.length; i++)
  98.            {
  99.            addressTo[i] = new InternetAddress(recipients[i]);
  100.            }
  101.            message.setRecipients(Message.RecipientType.TO, addressTo);*/
  102.  
  103.  
  104.             //message.setFrom(new InternetAddress(from));
  105.  
  106.             if(subject!=null)
  107.             {
  108.             message.setSubject(subject);
  109.             }
  110.             else
  111.             {
  112.             message.setSubject("");
  113.             }
  114.  
  115.  
  116.             if (k!=null)
  117.             {
  118.             message.setContent("<html><body ><table border=1><tr><td>"+FirstName+"</td><td>"+fname+"</td></tr>
  119. <tr><td>"+LastName+"</td><td>"+lname+"</td></tr><tr><td>"+Title+"</td><td>
  120. "+tit+"</td></tr><tr><td>"+Organization+"</td>
  121. <td>"+org+"</td></tr><tr><td>"+Address1+"</td>
  122. <td>"+add1+"</td></tr><tr><td>"+Address2+"</td>
  123. <td>"+add2+
  124.             "</td></tr><tr><td>"+City+"</td>
  125. <td>"+city+"</td></tr><tr><td>"+State+"</td>
  126. <td>"+state+"</td></tr><tr><td>"+Country+"</td>
  127. <td>"+country+"</td></tr><tr><td>"+OrganizationSize+"</td>
  128. <td>"+organizationSize+ "</td></tr><tr><td>"+Product+"</td><td>"+product+"</td></tr>
  129. <tr><td>"+Phone+"</td><td>"+phone+"</td></tr><tr>
  130. <td>"+Email+"</td><td>"+email+"</td></tr></table>
  131. </body></html>","text/HTML");
  132.             }
  133.             else
  134.             {
  135.             message.setContent("","text/HTML");
  136.             }
  137.  
  138.  
  139.             Transport.send(message);
  140.             //out.println("The Mail has been sent");
  141.             response.sendRedirect("../signUp/success.html");
  142.         }
  143.         catch (Exception e)
  144.         {
  145.             out.println("ERROR....."+e);
  146.              System.out.println("ERROR....."+e);
  147.         }
  148.  
  149. }
  150. }
  151.  
  152.  
Aug 22 '07 #4

10K+
P: 13,264
Which values do you want to store in an array?
You do know how to declare arrays, right?
Aug 22 '07 #5

P: 25
Hi,
Get multiple mail ids from database and store that in array, and then send that as to like new InternetAddress(to),

first I got send to multiple mail ids without database using array like
Expand|Select|Wrap|Line Numbers
  1. String recipients=={"devteam@yahoo.com","sathya@gmail.com","pnithya@yahoo.co.in"};
  2.  
  3. InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
  4.            for (int i = 0; i < recipients.length; i++)
  5.            {
  6.            addressTo[i] = new InternetAddress(recipients[i]);
  7.            }
  8.            message.setRecipients(Message.RecipientType.TO, addressTo);
  9.  
now I want, how to store the resultSet values in array
Aug 22 '07 #6

10K+
P: 13,264
Hi,
Get multiple mail ids from database and store that in array, and then send that as to like new InternetAddress(to),

first I got send to multiple mail ids without database using array like
Expand|Select|Wrap|Line Numbers
  1. String recipients=={"devteam@yahoo.com","sathya@gmail.com","pnithya@yahoo.co.in"};
  2.  
  3. InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
  4.            for (int i = 0; i < recipients.length; i++)
  5.            {
  6.            addressTo[i] = new InternetAddress(recipients[i]);
  7.            }
  8.            message.setRecipients(Message.RecipientType.TO, addressTo);
  9.  
now I want, how to store the resultSet values in array
Use an Arraylist instead since you may not know how many times rs.next is going to be called. Then you just do

Expand|Select|Wrap|Line Numbers
  1. list.add(rs.getString(1));
Aug 22 '07 #7

P: 25
Hi,

Thanks for your solution, if i put this code like list.add(rs.getstring(1)) other simple coding means i got all the values from database, but in javamail
I got one error like
cannot resolve symbol,
symbol:variable list


Expand|Select|Wrap|Line Numbers
  1. ArrayList list = new ArrayList();  
  2.  
  3.  
  4.  while(rs.next())
  5.    {
  6.   list.add(rs.getString(1));
  7.     }  
  8.  
  9. MimeMessage message = new MimeMessage(session);
  10.            message.setRecipient(Message.RecipientType.TO,new InternetAddress(list));
  11.  
  12.  
Aug 22 '07 #8

10K+
P: 13,264
Hi,

Thanks for your solution, if i put this code like list.add(rs.getstring(1)) other simple coding means i got all the values from database, but in javamail
I got one error like
cannot resolve symbol,
symbol:variable list


Expand|Select|Wrap|Line Numbers
  1. ArrayList list = new ArrayList();  
  2.  
  3.  
  4.  while(rs.next())
  5.    {
  6.   list.add(rs.getString(1));
  7.     }  
  8.  
  9. MimeMessage message = new MimeMessage(session);
  10.            message.setRecipient(Message.RecipientType.TO,new InternetAddress(list));
  11.  
  12.  
It's your try/catch blocks that are putting list out of scope as soon as you exit the try/catch block. Either declare the ArrayList before the try/catch block or move your catch blocks to the end of the code.
Aug 22 '07 #9

P: 25
Hi,

I put the code like

Expand|Select|Wrap|Line Numbers
  1. while(rs.next())
  2. {
  3. list.add(rs.getString(1));
  4. }  
  5. }
  6. catch(Exception e)
  7. {
  8. out.println(e);
  9. }
  10.  
  11.  
  12.            MimeMessage message = new MimeMessage(session);
  13.           message.setRecipient(Message.RecipientType.TO,new InternetAddress(list));
  14.  
means I got the following error,

cannot resolve symbol
symbol:constractor InternetAddress (java.util.ArrayList)

If I put the code like
Expand|Select|Wrap|Line Numbers
  1. while(rs.next())
  2. {
  3. list.add(rs.getString(1));
  4. }  
  5. }
  6. catch(Exception e)
  7. {
  8. out.println(e);
  9. }
  10.  
  11.  
  12.            MimeMessage message = new MimeMessage(session);
  13.           message.setRecipient(Message.RecipientType.TO,new InternetAddress("list"));
  14.  
this means I didnot get compile time error,
but I got run time error like

ERROR.....javax.mail.SendFailedException: Invalid Addresses; nested exception is: class com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Hello [125.22.243.43], Policy rejection on the target address:

Please help me

Thanks in advance
Aug 23 '07 #10

10K+
P: 13,264
Read the docs for ArrayList class to see how to use the get method to get values out of an arraylist.
Also, you'll have to make sure you're passing the correct address format because the InternetAddress constructor does strict parsing on it.
Aug 23 '07 #11

P: 25
Hi,
I tried many ways, but I didnot get the answer,Please help me.

I think it is typecast error, after fetching the values from database I print that values using out.println(list) , I got the output [dev@yahoo.com].
and then I tried to convert from arraylist to string, I had no compile time error,
but I got runtime error, that time also I print that values using out.println(list).

If I pass the values like this
Expand|Select|Wrap|Line Numbers
  1. ]
  2. Strint to={"dev@yahoo.com","a@yahoo.com"}
  3.  message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
  4.  
means I got the correct answer.

but I didnot have any solution how to convert [a@yahoo.com] to "a@yahoo.com", Please the solution.
Aug 24 '07 #12

madhoriya22
100+
P: 252
Hi,
I tried many ways, but I didnot get the answer,Please help me.

I think it is typecast error, after fetching the values from database I print that values using out.println(list) , I got the output [dev@yahoo.com].
and then I tried to convert from arraylist to string, I had no compile time error,
but I got runtime error, that time also I print that values using out.println(list).

If I pass the values like this
Expand|Select|Wrap|Line Numbers
  1. ]
  2. Strint to={"dev@yahoo.com","a@yahoo.com"}
  3. message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
  4.  
means I got the correct answer.

but I didnot have any solution how to convert [a@yahoo.com] to "a@yahoo.com", Please the solution.
Hi,
It is very simple..... Use get method of list as
Expand|Select|Wrap|Line Numbers
  1.  
  2. list.get(i);//where i is the position of element in the list
  3.  
If list contain class object or String then typeCast it..
Expand|Select|Wrap|Line Numbers
  1.  
  2. (object)list.get(i); // ..or
  3. (String)list.get(i);
  4.  
Aug 24 '07 #13

P: 25
Hi,
I tried that way even then I didnot get the answer, I put the code like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. ArrayList list = new ArrayList();  
  3. while(rs.next())
  4. {
  5.   String to=rs.getString(1);
  6.   list.add(to);
  7.  }
  8.  String m="";
  9.  for(int i=0;i<list.size();i++)
  10.  {
  11.   m += list.get(i).toString();
  12.  }
  13. String to=m;
  14. out.println(to);
  15. }
  16. catch(Exception e)
  17. {
  18. out.println(e);
  19. }
  20.  
  21.  
  22.            MimeMessage message = new MimeMessage(session);
  23.            message.setRecipient(Message.RecipientType.TO,new InternetAddress("to"));
  24.  

I got the following error,

"sruthi@yahoo.com" ERROR.....javax.mail.SendFailedException: Invalid Addresses; nested exception is: class com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Hello [125.22.243.43], Policy rejection on the target address:
Aug 24 '07 #14

10K+
P: 13,264
Hi,
I tried that way even then I didnot get the answer, I put the code like this

Expand|Select|Wrap|Line Numbers
  1.  
  2. ArrayList list = new ArrayList();  
  3. while(rs.next())
  4. {
  5.   String to=rs.getString(1);
  6.   list.add(to);
  7.  }
  8.  String m="";
  9.  for(int i=0;i<list.size();i++)
  10.  {
  11.   m += list.get(i).toString();
  12.  }
  13. String to=m;
  14. out.println(to);
  15. }
  16. catch(Exception e)
  17. {
  18. out.println(e);
  19. }
  20.  
  21.  
  22.            MimeMessage message = new MimeMessage(session);
  23.            message.setRecipient(Message.RecipientType.TO,new InternetAddress("to"));
  24.  

I got the following error,

"sruthi@yahoo.com" ERROR.....javax.mail.SendFailedException: Invalid Addresses; nested exception is: class com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Hello [125.22.243.43], Policy rejection on the target address:
Remove the quotes in
Expand|Select|Wrap|Line Numbers
  1. message.setRecipient(Message.RecipientType.TO,new InternetAddress("to"));
So you have
Expand|Select|Wrap|Line Numbers
  1. message.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
Aug 24 '07 #15

P: 25
Hi,
If I remove the quotes , I got the compile time error,

cannot resolve symbol
symbol:variable m
location:message.setRecipient(Message.RecipientTyp e.TO,new InternetAddress(m));


Expand|Select|Wrap|Line Numbers
  1. ArrayList list = new ArrayList();  
  2. while(rs.next())
  3. {
  4.   String to=rs.getString(1);
  5.   list.add(to);
  6.  }
  7.  String m="";
  8.  for(int i=0;i<list.size();i++)
  9.  {
  10.   m += list.get(i).toString();
  11.  }
  12. MimeMessage message = new MimeMessage(session);
  13.            message.setRecipient(Message.RecipientType.TO,new InternetAddress(m));
  14.  
Please help me,
Aug 27 '07 #16

Nepomuk
Expert 2.5K+
P: 3,112
cannot resolve symbol
symbol:variable m
location:message.setRecipient(Message.RecipientTyp e.TO,new InternetAddress(m));
...
Expand|Select|Wrap|Line Numbers
  1.            message.setRecipient(Message.RecipientType.TO,new InternetAddress(m));
  2.  
Why is it new InternetAddress(m) instead of new InternetAddress(to)? It doesn't recognize m! It used to be "to", didn't it?
Aug 27 '07 #17

10K+
P: 13,264
Hi,
If I remove the quotes , I got the compile time error,

cannot resolve symbol
symbol:variable m
location:message.setRecipient(Message.RecipientTyp e.TO,new InternetAddress(m));


Expand|Select|Wrap|Line Numbers
  1. ArrayList list = new ArrayList();  
  2. while(rs.next())
  3. {
  4.   String to=rs.getString(1);
  5.   list.add(to);
  6.  }
  7.  String m="";
  8.  for(int i=0;i<list.size();i++)
  9.  {
  10.   m += list.get(i).toString();
  11.  }
  12. MimeMessage message = new MimeMessage(session);
  13.            message.setRecipient(Message.RecipientType.TO,new InternetAddress(m));
  14.  
Please help me,
But what you posted is not what your code looks like. Can you post the code that's giving you this error as it is?

P.S It's not a bad time to read something about Java scoping rules.
Aug 27 '07 #18

P: 25
Hi,

I used the following code,
Expand|Select|Wrap|Line Numbers
  1. ArrayList list = new ArrayList();  
  2. while(rs.next())
  3. {
  4.   //String to=rs.getString(1);
  5.   list.add(rs.getString(1));
  6.  }
  7.  String m="";
  8.  for(int i=0;i<list.size();i++)
  9.  {
  10.  m += list.get(i).toString()+"," ;
  11.  }
  12. //String to=m;
  13.  
  14.  
  15.  
  16.            MimeMessage message = new MimeMessage(session);
  17.            message.setRecipient(Message.RecipientType.TO,new InternetAddress(m));
  18.  
If I pass one mail id to database I got the correct answer, If I pass more than one mail ids , I got the following error,

ERROR.....javax.mail.internet.AddressException: Illegal address in string ``dev@gmail.com,anu@gmail.com,a@yaoo.com,b@yahoo.c om,''

If I pass like

message.setRecipient(Message.RecipientType.TO,new InternetAddress("a@yaoo.com","b@yahoo.com"));

I got the correct answer, How to split the mail ids like above, If I use split(",") or "," means , It does not support more than one mail id's.

Please give the solution.
Aug 27 '07 #19

10K+
P: 13,264
Hi,

I used the following code,
Expand|Select|Wrap|Line Numbers
  1. ArrayList list = new ArrayList();  
  2. while(rs.next())
  3. {
  4.   //String to=rs.getString(1);
  5.   list.add(rs.getString(1));
  6.  }
  7.  String m="";
  8.  for(int i=0;i<list.size();i++)
  9.  {
  10.  m += list.get(i).toString()+"," ;
  11.  }
  12. //String to=m;
  13.  
  14.  
  15.  
  16.            MimeMessage message = new MimeMessage(session);
  17.            message.setRecipient(Message.RecipientType.TO,new InternetAddress(m));
  18.  
If I pass one mail id to database I got the correct answer, If I pass more than one mail ids , I got the following error,

ERROR.....javax.mail.internet.AddressException: Illegal address in string ``dev@gmail.com,anu@gmail.com,a@yaoo.com,b@yahoo.c om,''

If I pass like

message.setRecipient(Message.RecipientType.TO,new InternetAddress("a@yaoo.com","b@yahoo.com"));

I got the correct answer, How to split the mail ids like above, If I use split(",") or "," means , It does not support more than one mail id's.

Please give the solution.
You can get an array of InternetAddress objects from using the parse method.
Aug 27 '07 #20

P: 25
Hi,

Thank you very much for your help, Now Its work perfectly with multiple mailid's.
but In some other system its not working. my smtp settings like
props.put("mail.smtp.host","mail.companyname.com") ;

If I give the mailids like a@company.com,b@company.com means
its working proper, otherwise I give a@gmail.com,b@yahoo.com means I got the following error,

ERROR.....javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
class com.sun.mail.smtp.SMTPAddressFailedException: 500 Unauthorized relay msg rejected

Please help me,

thanks in advance.
Sep 3 '07 #21

Post your reply

Sign in to post your reply or Sign up for a free account.