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){}

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) {}
}
commented: finally block, +1 PreparedStatement ??? +8
commented: problem solved +0

thnx for your help.its really help full

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.