954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

java mail address into database

The value of address is not insert in database varchar2 pno field. tried from.tostring()
but not inserted but its prints through system out perfect as email adress. any idea or suggesstion how to insert it into database..

import java.io.*;
import java.util.Properties;
import javax.mail.*;
import java.sql.*;

public static void insertm(int mid,Address from,String sub){
    
try {
    // Load the JDBC driver
    
    Class.forName ("oracle.jdbc.OracleDriver");

   Connection conn = DriverManager.getConnection
            ("jdbc:oracle:thin:@//localhost:1521/xe", "scott", "tiger");
                           
       
        Statement stmt = conn.createStatement();
      String str = from.toString();

System.out.println(sub+str+mid);  
//sql      
        String sql = "insert into mms(subject,pno,mid) values ('"+sub+"','"+str+"',"+mid+")";

    // Execute the insert statement
    stmt.executeUpdate(sql);
       
    conn.commit();
}catch(Throwable t){}
andreson
Newbie Poster
2 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

You don't need the commit command. It is done automatically, unless specified otherwise.
Also try to print the query and what actually gets executed.
Also the executeUpdate returns an int value. It shows how many rows have been updated/inserted. Try to print that too.
Also your catch block is empty. Why? That is problem. If you have an exception, you print nothing and you don't know if something went wrong.
You also need to close the connection as well as the Statement.
And it is better not to use the from.toString() . If you want to save the address, try to use: from.getMailAddress() or something. Because toString is used for displaying and maybe in the future you decide you want to change the way the object gets displayed. But since you need to save in the DB only the email, then you need to get the email.

import java.io.*;
import java.util.Properties;
import javax.mail.*;
import java.sql.*;

public static void insertm(int mid,Address from,String sub){
    
try {
    // Load the JDBC driver
    
    Class.forName ("oracle.jdbc.OracleDriver");

   Connection conn = DriverManager.getConnection
            ("jdbc:oracle:thin:@//localhost:1521/xe", "scott", "tiger");
                           
       
        Statement stmt = conn.createStatement();
      String str = from.toString();

System.out.println(sub+", "+str+", "+mid);  
//sql      
        String sql = "insert into mms(subject,pno,mid) values ('"+sub+"','"+str+"',"+mid+")";

    System.out.println("Executing: "+sql);    

    int i = stmt.executeUpdate(sql);
       
    System.out.println("Rows Updated: "+i);   

     stmt.close();
     conn.close();
} catch(Exception e) {
   System.out.println("Error: "+e.getMessage());
   System.out.println("Details: ");
   e.printStackTrace();
}


The next step would be to move the close commands in the finally block:

....
try {
   ....
} catch (Exception e) {
   System.out.println("Error: "+e.getMessage());
} finally {
     try {
        if (stmt!=null) stmt.close();
        if (conn!=null) conn.close();
     } catch (Exception e1) {}
}
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

thnx for your help.its really help full

andreson
Newbie Poster
2 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: