Fetch values from database and store it in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sruthini
    New Member
    • Aug 2007
    • 25

    Fetch values from database and store it in array

    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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sruthini
    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?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by r035198x
      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.

      Comment

      • sruthini
        New Member
        • Aug 2007
        • 25

        #4
        Hi,
        I post the code here,

        [code=java]
        import javax.servlet.* ;
        import javax.servlet.h ttp.*;
        import java.io.*;
        import java.sql.*;
        import javax.mail.*;
        import javax.mail.inte rnet.*;
        import javax.mail.even t.*;
        import java.net.*;
        import java.util.*;


        public class mail3 extends HttpServlet

        {
        public void doGet(HttpServl etRequest request,HttpSer vletResponse response) throws ServletExceptio n, IOException
        {

        PrintWriter out=response.ge tWriter();
        response.setCon tentType("text/html");
        try
        {
        Properties props=new Properties();
        props.put("mail .smtp.host","ma il.cygnussoftwa res.com");
        Session session = Session.getDefa ultInstance(pro ps,null);
        String fname = request.getPara meter("firstnam e");
        String lname = request.getPara meter("lastname ");
        String tit = request.getPara meter("Title");
        String org = request.getPara meter("organiza tion");
        String add1 = request.getPara meter("Address1 ");
        String add2 = request.getPara meter("address2 ");
        String city = request.getPara meter("city");
        String state = request.getPara meter("state");
        String country = request.getPara meter("country" );
        String organizationSiz e= request.getPara meter("turnover ");
        String phone= request.getPara meter("phone");
        String email= request.getPara meter("emailid" );
        String product= request.getPara meter("product" );

        //String recipients= "devteam@yahoo. com"

        //={"devteam@yaho o.com","sathya@ gmail.com","pni thya@yahoo.co.i n"};


        String k=fname+lname+t it+org+add1+add 2+city+state+co untry+organizat ionSize+product +phone+email;
        String subject="Produc t Inquiry Form";

        String FirstName="Firs tName:";
        String LastName="LastN ame:";
        String Title="Title:";
        String Organization="O rganization:";
        String Address1="Addre ss1:";
        String Address2="Addre ss2:";
        String City="City:";
        String State="State:";
        String Country="Countr y:";
        String OrganizationSiz e="Organization Size:";
        String Phone="Phone:";
        String Email="E-Mail:";
        String Product="Produc t";

        //String recipients;

        try
        {
        //Class.forName(" com.microsoft.j dbc.sqlserver.S QLServerDriver" );
        //Connection cn = DriverManager.g etConnection("j dbc:microsoft:s qlserver://CSISERVER2:1433 ;DatabaseName=w eb Hosting","sa"," sa") ;
        Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
        Connection cn = DriverManager.g etConnection("j dbc:odbc:send", " ", " ");
        Statement st = cn.createStatem ent();
        ResultSet rs = st.executeQuery ("select * from send ");

        //st.executeUpdat e("insert into sendmail values('"+fname +"','"+lname+"' ,'"+tit+"','"+o rg+"',
        '"+add1+"','"+a dd2+"','"+city+ "','"+state +"',
        '"+country+"',' "+organizationS ize+"','"+produ ct+"','"+phone+ "','"+email+"') ");
        //out.println("Re cord inserted successfully ........");


        String to="";

        while(rs.next() )
        {
        to = rs.getString(1) ;

        }
        System.out.prin tln("the name is :"+to);

        }
        catch(Exception e)
        {
        e.printStackTra ce();
        }

        MimeMessage message = new MimeMessage(ses sion);
        message.setReci pient(Message.R ecipientType.TO ,new InternetAddress (to));

        /*InternetAddres s[] addressTo = new InternetAddress[recipients.leng th];
        for (int i = 0; i < recipients.leng th; i++)
        {
        addressTo[i] = new InternetAddress (recipients[i]);
        }
        message.setReci pients(Message. RecipientType.T O, addressTo);*/


        //message.setFrom (new InternetAddress (from));

        if(subject!=nul l)
        {
        message.setSubj ect(subject);
        }
        else
        {
        message.setSubj ect("");
        }


        if (k!=null)
        {
        message.setCont ent("<html><bod y ><table border=1><tr><t d>"+FirstName+" </td><td>"+fname+ "</td></tr>
        <tr><td>"+LastN ame+"</td><td>"+lname+ "</td></tr><tr><td>"+Ti tle+"</td><td>
        "+tit+"</td></tr><tr><td>"+Or ganization+"</td>
        <td>"+org+"</td></tr><tr><td>"+Ad dress1+"</td>
        <td>"+add1+"</td></tr><tr><td>"+Ad dress2+"</td>
        <td>"+add2+
        "</td></tr><tr><td>"+Ci ty+"</td>
        <td>"+city+"</td></tr><tr><td>"+St ate+"</td>
        <td>"+state+" </td></tr><tr><td>"+Co untry+"</td>
        <td>"+country+" </td></tr><tr><td>"+Or ganizationSize+ "</td>
        <td>"+organizat ionSize+ "</td></tr><tr><td>"+Pr oduct+"</td><td>"+produc t+"</td></tr>
        <tr><td>"+Phone +"</td><td>"+phone+ "</td></tr><tr>
        <td>"+Email+" </td><td>"+email+ "</td></tr></table>
        </body></html>","text/HTML");
        }
        else
        {
        message.setCont ent("","text/HTML");
        }


        Transport.send( message);
        //out.println("Th e Mail has been sent");
        response.sendRe direct("../signUp/success.html");
        }
        catch (Exception e)
        {
        out.println("ER ROR....."+e);
        System.out.prin tln("ERROR..... "+e);
        }

        }
        }

        [/code]

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Which values do you want to store in an array?
          You do know how to declare arrays, right?

          Comment

          • sruthini
            New Member
            • Aug 2007
            • 25

            #6
            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
            [code=java]
            String recipients=={"d evteam@yahoo.co m","sathya@gmai l.com","pnithya @yahoo.co.in"};

            InternetAddress[] addressTo = new InternetAddress[recipients.leng th];
            for (int i = 0; i < recipients.leng th; i++)
            {
            addressTo[i] = new InternetAddress (recipients[i]);
            }
            message.setReci pients(Message. RecipientType.T O, addressTo);
            [/code]

            now I want, how to store the resultSet values in array

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by sruthini
              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
              [code=java]
              String recipients=={"d evteam@yahoo.co m","sathya@gmai l.com","pnithya @yahoo.co.in"};

              InternetAddress[] addressTo = new InternetAddress[recipients.leng th];
              for (int i = 0; i < recipients.leng th; i++)
              {
              addressTo[i] = new InternetAddress (recipients[i]);
              }
              message.setReci pients(Message. RecipientType.T O, addressTo);
              [/code]

              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

              [CODE=java]list.add(rs.get String(1));[/CODE]

              Comment

              • sruthini
                New Member
                • Aug 2007
                • 25

                #8
                Hi,

                Thanks for your solution, if i put this code like list.add(rs.get string(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


                [code=java]
                ArrayList list = new ArrayList();


                while(rs.next() )
                {
                list.add(rs.get String(1));
                }

                MimeMessage message = new MimeMessage(ses sion);
                message.setReci pient(Message.R ecipientType.TO ,new InternetAddress (list));

                [/code]

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by sruthini
                  Hi,

                  Thanks for your solution, if i put this code like list.add(rs.get string(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


                  [code=java]
                  ArrayList list = new ArrayList();


                  while(rs.next() )
                  {
                  list.add(rs.get String(1));
                  }

                  MimeMessage message = new MimeMessage(ses sion);
                  message.setReci pient(Message.R ecipientType.TO ,new InternetAddress (list));

                  [/code]
                  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.

                  Comment

                  • sruthini
                    New Member
                    • Aug 2007
                    • 25

                    #10
                    Hi,

                    I put the code like

                    [code=java]
                    while(rs.next() )
                    {
                    list.add(rs.get String(1));
                    }
                    }
                    catch(Exception e)
                    {
                    out.println(e);
                    }


                    MimeMessage message = new MimeMessage(ses sion);
                    message.setReci pient(Message.R ecipientType.TO ,new InternetAddress (list));
                    [/code]
                    means I got the following error,

                    cannot resolve symbol
                    symbol:constrac tor InternetAddress (java.util.Arra yList)

                    If I put the code like
                    [code=java]
                    while(rs.next() )
                    {
                    list.add(rs.get String(1));
                    }
                    }
                    catch(Exception e)
                    {
                    out.println(e);
                    }


                    MimeMessage message = new MimeMessage(ses sion);
                    message.setReci pient(Message.R ecipientType.TO ,new InternetAddress ("list"));
                    [/code]

                    this means I didnot get compile time error,
                    but I got run time error like

                    ERROR.....javax .mail.SendFaile dException: Invalid Addresses; nested exception is: class com.sun.mail.sm tp.SMTPAddressF ailedException: 550 5.7.1 Hello [125.22.243.43], Policy rejection on the target address:

                    Please help me

                    Thanks in advance

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      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.

                      Comment

                      • sruthini
                        New Member
                        • Aug 2007
                        • 25

                        #12
                        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(lis t) , 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(lis t).

                        If I pass the values like this
                        [code=java]]
                        Strint to={"dev@yahoo. com","a@yahoo.c om"}
                        message.setReci pient(Message.R ecipientType.TO ,new InternetAddress (to));
                        [/code]
                        means I got the correct answer.

                        but I didnot have any solution how to convert [a@yahoo.com] to "a@yahoo.co m", Please the solution.

                        Comment

                        • madhoriya22
                          Contributor
                          • Jul 2007
                          • 251

                          #13
                          Originally posted by sruthini
                          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(lis t) , 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(lis t).

                          If I pass the values like this
                          [code=java]]
                          Strint to={"dev@yahoo. com","a@yahoo.c om"}
                          message.setReci pient(Message.R ecipientType.TO ,new InternetAddress (to));
                          [/code]
                          means I got the correct answer.

                          but I didnot have any solution how to convert [a@yahoo.com] to "a@yahoo.co m", Please the solution.
                          Hi,
                          It is very simple..... Use get method of list as
                          Code:
                           
                          list.get(i);//where i is the position of element in the list
                          If list contain class object or String then typeCast it..
                          Code:
                           
                          (object)list.get(i); // ..or
                          (String)list.get(i);

                          Comment

                          • sruthini
                            New Member
                            • Aug 2007
                            • 25

                            #14
                            Hi,
                            I tried that way even then I didnot get the answer, I put the code like this

                            [code=java]

                            ArrayList list = new ArrayList();
                            while(rs.next() )
                            {
                            String to=rs.getString (1);
                            list.add(to);
                            }
                            String m="";
                            for(int i=0;i<list.size ();i++)
                            {
                            m += list.get(i).toS tring();
                            }
                            String to=m;
                            out.println(to) ;
                            }
                            catch(Exception e)
                            {
                            out.println(e);
                            }


                            MimeMessage message = new MimeMessage(ses sion);
                            message.setReci pient(Message.R ecipientType.TO ,new InternetAddress ("to"));
                            [/code]


                            I got the following error,

                            "sruthi@yahoo.c om" ERROR.....javax .mail.SendFaile dException: Invalid Addresses; nested exception is: class com.sun.mail.sm tp.SMTPAddressF ailedException: 550 5.7.1 Hello [125.22.243.43], Policy rejection on the target address:

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by sruthini
                              Hi,
                              I tried that way even then I didnot get the answer, I put the code like this

                              [code=java]

                              ArrayList list = new ArrayList();
                              while(rs.next() )
                              {
                              String to=rs.getString (1);
                              list.add(to);
                              }
                              String m="";
                              for(int i=0;i<list.size ();i++)
                              {
                              m += list.get(i).toS tring();
                              }
                              String to=m;
                              out.println(to) ;
                              }
                              catch(Exception e)
                              {
                              out.println(e);
                              }


                              MimeMessage message = new MimeMessage(ses sion);
                              message.setReci pient(Message.R ecipientType.TO ,new InternetAddress ("to"));
                              [/code]


                              I got the following error,

                              "sruthi@yahoo.c om" ERROR.....javax .mail.SendFaile dException: Invalid Addresses; nested exception is: class com.sun.mail.sm tp.SMTPAddressF ailedException: 550 5.7.1 Hello [125.22.243.43], Policy rejection on the target address:
                              Remove the quotes in [CODE=java]message.setReci pient(Message.R ecipientType.TO ,new InternetAddress ("to"));[/CODE]
                              So you have [CODE=java]message.setReci pient(Message.R ecipientType.TO ,new InternetAddress (to));[/CODE]

                              Comment

                              Working...