0

i'm trying a code found with google which allows to retrieve emails from gmail and store to mysql.

When i launch the code, it's working but at the end i have an error message: Cannot connet to database.

It's normal because i didn't knwow how to create the default.properties file: i know the content but in which format must be the file and where must i put this file, in Netbeans??

Do i have to create a folder? which file format txt, java?? the name of the file will be default.properties

The code will be found here: sakthimaharai.hubpages.com

I need a hlep please.

Thank you

Pacific
  • 195
  • 2
  • 6
  • 17

3 Answers3

1

In Netbeans you can create a properties file using the contextual menu to create new elements, as per this answer. Be careful of entering default as the name since NB will add the .properties to whatever you write and you might end with default.properties.properties.

The most common thing is to read properties files from the classpath or from the working directory, in the first case you should create the file in the root of the Source folder. In the second case you can create the file directly in the Project node but in this case the file won't be added to the final jar/war if you want to distribute your program.

Examples of the format (and even some code to handle properties files).

Community
  • 1
  • 1
madth3
  • 7,275
  • 12
  • 50
  • 74
0

I don't know from where the code is trying to load the properties, you have not said much, but under normal conditions the file should be in the project's classpath.

A property file (.properties) looks like this:

key=value
key2=value2
#comment1

Provide more info so we can help you.

epoch
  • 16,396
  • 4
  • 43
  • 71
0

OK here is the complete code found in google and removed by the author today, so it's free here for anyone is interrested.

 package inboxreader;

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.util.HashSet;
    import java.util.Properties;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import javax.mail.Address;
    import javax.mail.Flags;
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.NoSuchProviderException;
    import javax.mail.Part;
    import javax.mail.Session;
    import javax.mail.Store;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMultipart;

    public class InboxReader {
    static HashSet<String> mails;
        public static void main(String args[])
        {
            while(true)
            {
                try {
                    System.out.println("Started.......");
                    Start();
                    System.out.println("...Read completed.......");


                    try {
                        Thread.sleep(1000*60*5);
                    } catch (InterruptedException e1) {


                    }

                } catch (Exception e) {

                    try {connecttoMySql();

                        e.printStackTrace();
                        System.out.println("..Error in connection Sleeping...");

                    } catch (Exception e1) {


                    }
                }
            }

        }
        public static void Start() throws Exception {
            Properties props = System.getProperties();

            props.setProperty("mail.store.protocol", "imaps");
                try {
                    Session session = Session.getDefaultInstance(props, null);
                    Store store = session.getStore("imaps");
                    store.connect("imap.gmail.com", "email@gmail.com", "password");
                    System.out.println(store);
                    int cout=0;

                    Folder inbox = store.getFolder("Inbox");
                    inbox.open(Folder.READ_WRITE);
                    Message messages[] = inbox.getMessages();
                    for(Message message:messages) {
                        mails=new HashSet<String>();
                        System.out.println("Reading:"+ (messages.length-cout));
                        cout++;
                        InboxReader.storeAddresses(message);
                    dumpPart(message);
                    for(String temp:mails)
                        System.out.println(temp);
                    connecttoMySql();
                    message.setFlag(Flags.Flag.DELETED, true);
                }



            } catch (NoSuchProviderException e) {
                connecttoMySql();
                e.printStackTrace();

            } catch (MessagingException e) {
                connecttoMySql();
                e.printStackTrace();

            }

        }
        public static void storeAddresses(Message msg)
        {
            try {
                for(Address adr:msg.getAllRecipients())
                {
                    addAddresses(adr.toString());

                }

            } catch (Exception e) {

                e.printStackTrace();
            }

        }
        public static void addAddresses(String input_text)
        {
            Pattern p= Pattern.compile("[A-Z0-9\\._%\\+\\-]+@[A-Z0-9\\.\\-]+\\.[A-Z]{2,4}",Pattern.CASE_INSENSITIVE);

            Matcher m=p.matcher(input_text);
            while(m.find())
            {
                mails.add(m.group());

            }

        }
        public static void dumpPart(Part p) throws Exception {



            if (p.isMimeType("text/plain")) {

               try{
                addAddresses((String)p.getContent());
               }catch(Exception e){}
            } else {

                MimeMultipart   mb = null;
                try{
                   mb=(MimeMultipart ) (p.getContent());
            }
            catch(Exception e)
            {  try{
                if(p.getContent() instanceof String)


                    addAddresses((String)p.getContent());
                    }catch(Exception e1){}
                return;
            }

                MimeBodyPart  mb1=(MimeBodyPart) mb.getBodyPart(0);
                mb1.saveFile("emailtext.html");
                BufferedReader br = new BufferedReader(new FileReader("emailtext.html"));
                StringBuffer content = new StringBuffer();
                String line ="";
                while((line = br.readLine())!= null )
                {
                    if(line.length()>=2)if(line.substring(line.length()-1).equals("="))
                    {
                        content.append(line.substring(line.length()-1) );
                    }else
                    content.append(line+"\n");
                }
                addAddresses(content.toString());


            }
        }
        public static void connecttoMySql()
        {
            Connection conn = null;

            try
            {
                Properties details= new Properties();
                details.load(new FileInputStream("details.properties"));
                String userName = details.getProperty("root");
                String password = details.getProperty("password_of-mysql");
                String url = details.getProperty("jdbc:mysql://localhost/Test");
                Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                conn = DriverManager.getConnection (url, userName, password);
                System.out.println ("Database connection established");
                PreparedStatement st= conn.prepareStatement("insert into `Email_list` values(?)");
                for(String mail:mails)
                {
                try{
                      st.setString(1, mail);
                      st.execute();
                    }catch(Exception e){}
                }



            }
            catch (Exception e)
            {
                System.err.println ("Cannot connect to database server");
                e.printStackTrace();
            }
            finally
            {
                if (conn != null)
                {
                    try
                    {
                        conn.close ();
                        System.out.println ("Database connection terminated");
                    }
                    catch (Exception e) {  }
                }
            }
        }



    }

and the error message is:

 Cannot connect to database server
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:169)
        at inboxreader.InboxReader.connecttoMySql(InboxReader.java:180)
        at inboxreader.InboxReader.main(InboxReader.java:47)
    com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 2 before EOF, the 10 most recent characters were: "AKxCo9RUjD"
    ..Error in connection Sleeping...

And create the file default.properties.properties as explained in the example of Madth3 Thank you

And the default.properties.properties is:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class App 
{
    public static void main( String[] args )
    {
        Properties prop = new Properties();

        try {
            //set the properties value
            prop.setProperty("database", "localhost");
            prop.setProperty("dbuser", "root");
            prop.setProperty("dbpassword", "password");

            //save properties to project root folder
            prop.store(new FileOutputStream("default.properties.properties"), null);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Pacific
  • 195
  • 2
  • 6
  • 17