Results 1 to 4 of 4
  1. #1
    tedy2808 is offline Member
    Join Date
    Aug 2010
    Posts
    6
    Rep Power
    0

    Default how to send email notification using java

    hi all,
    i really new to this issue..so need your guidance on this matter..Below are my script:

    Java Code:
      import java.sql.*;
    public class javatesting {
    
      public javatesting() throws Exception {
    
                    // Get connSQLServer
                    DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
                    Connection connSQLServer = DriverManager.getConnection("jdbc:microsoft:sqlserver://172.11.17.10:1433;databaseName=java2", "testjava", 
    
    "testjava123");
    
                    Class.forName("oracle.jdbc.driver.OracleDriver");
                    Connection connOracle = DriverManager.getConnection("jdbc:oracle:thin:@172.14.34.22:1521:p3abbc2","abc","abc123");
                 
    
                    String strInsert = "insert into table_1 (my_org, my_name, my_add1, my_add2, phone, fax, state, code_cnty, ur_head,"+
                                       " org_head2, org_head3, status, postcode, region_code, company_no, contact_person, city, sales_rep, sales_rep_status , 
    
    new_date , old_date )" +
                                       " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    String strDelete = "delete from table_1 where my_org in (select my_org from (select count(*), my_org from " +
                                       " table_1 group by my_org having count(*) > 1)) and status = 'S'";
    
    
     if (connSQLServer != null)
                    {
    
                            System.out.println();
                            System.out.println("Successfully connected");
                            System.out.println();
    
                            // Meta data
                            DatabaseMetaData meta = connSQLServer.getMetaData();
    
                            System.out.println("Driver Information");
                            System.out.println("\tDriver Name: "+ meta.getDriverName());
                            System.out.println("\tDriver Version: "+ meta.getDriverVersion());
                            System.out.println("\nDatabase Information ");
                            System.out.println("\tDatabase Name: "+ meta.getDatabaseProductName());
                            System.out.println("\tDatabase Version: "+ meta.getDatabaseProductVersion());
    
                            // Select some data
    
                            Statement select = connSQLServer.createStatement();
                            ResultSet result = select.executeQuery("SELECT my_org, my_name, my_add1, my_add2, phone, fax,  case state when '1' then 'JJ' " +
                                                                   "when '2' then 'KD' when '3' then 'KN' when '4' then 'MK' " +
                                                                   "when '5' then 'NN' when '6' then 'PP' " +
                                                                   "when '7' then 'PG' when '8' then 'PK' when '9' then 'PR' " +
                                                                   "when '10' then 'SH' when '11' then 'SK' "+
                                                                   "when '12' then 'SS' when '13' then 'TT' "+
                                                                   "when '14' then 'WPK' when '15' then 'WPL' "+
                                                                   "when '16' then 'WPP' end as state, "+
                                                                   "code_cnty, ur_head, "+
                                                                   "org_head2, org_head3, o.status, postcode, case region_code when '34' then 'CNTR' "+
                                                                   "when '35' then 'NTR' when '36' then 'STR' when '37' then 'ETR' " +
                                                                   "when '38' then 'SW' when '39' then 'SB' end as region_code, "+
                                                                   "company_no, contact_person, city, staff_name as sales_rep, s.status as sales_rep_status "+
                                                                                                                               " ,o.new_date, o.old_date " 
    
    +
                                                                   "FROM KKM.my_unit o left join ord_stuff s on o.ur_head = s.staff_id and s.status <> 
    
    'D' ");
    
    
     if (connOracle != null)
    
                                {
                               Statement ps = connOracle.createStatement();
                               System.out.println("Truncating table_1 in progress .......");
    
                               ps.executeUpdate("truncate table table_1");
    
                               System.out.println("Done");
    
                               PreparedStatement stat_p = connOracle.prepareStatement(strInsert);
                               System.out.println("Inserting records into table_1 in progress ......");
                               
    
                               int totalRows = 0 ;
                               while (result.next())
                               {
                                 stat_p.setString(1, result.getString(1));
                                 stat_p.setString(2, result.getString(2));
                                 stat_p.setString(3, result.getString(3));
                                 stat_p.setString(4, result.getString(4));
                                 stat_p.setString(5, result.getString(5));
                                 stat_p.setString(6, result.getString(6));
                                 stat_p.setString(7, result.getString(7));
                                 stat_p.setString(8, result.getString(8));
                                 stat_p.setString(9, result.getString(9));
                                 stat_p.setString(10, result.getString(10));
                                 stat_p.setString(11, result.getString(11));
                                 stat_p.setString(12, result.getString(12));
                                 stat_p.setString(13, result.getString(13));
                                 stat_p.setString(14, result.getString(14));
                                 stat_p.setString(15, result.getString(15));
                                 stat_p.setString(16, result.getString(16));
                                 stat_p.setString(17, result.getString(17));
                                 stat_p.setString(18, result.getString(18));
                                 stat_p.setString(19, result.getString(19));
                                 stat_p.setTimestamp(20, result.getTimestamp(20));
                                 stat_p.setTimestamp(21, result.getTimestamp(21));
    
                                stat_p.executeUpdate();
                                int rows = stat_p.executeUpdate(); 
                                totalRows = totalRows + rows; 
                                 }
    
                               connOracle.commit();
                               System.out.println("Done");
                               System.out.println("Row(s) inserted: " + totalRows);
                               int totalRows1 = 0 ; 
                               System.out.println("Removing duplicate records in table_1 in progress (with status = S) ......");
                               PreparedStatement stat_p1 = connOracle.prepareStatement(strDelete);
                               stat_p1.executeUpdate();
                               int rows1 = stat_p1.executeUpdate(); 
                               totalRows1 = totalRows1 + rows1;    
                               connOracle.commit();
                               System.out.println("Done");
                               System.out.printf("%d row(s) deleted!", totalRows1);
                               System.out.println(" ");
                               stat_p1.close();
                               stat_p.close();
                            }
                            select.close();   }
    else {
    System.out.println("Cannot connect to database");
    }
                   connOracle.close();
                   connSQLServer.close();
    }
    
     public static void main (String args[]) throws Exception {
    
        javatesting pr = new javatesting();
      }
    
    }

    the output will be similar like this:

    Successfully connected

    Driver Information
    Driver Name: SQLServer
    Driver Version: 2.2.0022

    Database Information
    Database Name: Microsoft SQL Server
    Database Version: Microsoft SQL Server Yukon - 9.00.1399
    Truncating table_1 in progress .......
    Done
    Inserting records into table_1 in progress ......
    Done
    Row(s) inserted: 94
    Removing duplicate records in table_1 in progress (with status = S) ......
    Done
    0 row(s) deleted!



    and if there are error in database;
    the error message will be something like this:

    Exception in thread "main" java.sql.SQLException: ORA-00001: unique constraint (PIN.my_new_java) violated
    at oracle.jdbc.driver.DatabaseError.throwSqlException (DatabaseError.java:111)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoe r.java:330)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoe r.java:287)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java: 742)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4 CPreparedStatement.java:212)
    at oracle.jdbc.driver.T4CPreparedStatement.executeFor Rows(T4CPreparedStatement.java:951)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTi meout(OracleStatement.java:1159)
    at oracle.jdbc.driver.OraclePreparedStatement.execute Internal(OraclePreparedStatement.java:3284)
    ....... etc..


    thus, i wanted to have email notification if this error occur.. i did try an error but does not help.
    i used to learned based on example and it is good if someone can show me how to do and explain and give detail explanation to me..Thanks in advanced. :)
    Last edited by tedy2808; 08-17-2010 at 10:45 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Sun publishes javax.mail package; Google is your friend here.

    kind regards,

    Jos

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default

    You can try an example I have on the following link: How do I send an email message?. I hope it can give you a starting point to send email message in Java.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 10
    Last Post: 06-06-2010, 06:49 PM
  2. how to send an email
    By Hussain Ali in forum Advanced Java
    Replies: 9
    Last Post: 03-19-2010, 07:44 AM
  3. how to send an email
    By painamrata in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 02-22-2009, 04:28 PM
  4. send email using JMS
    By Heather in forum Advanced Java
    Replies: 9
    Last Post: 01-07-2009, 03:04 PM
  5. send email using apache commons email
    By jnamendi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-14-2008, 05:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •