我必须在Netbeans 7.1中为java代码复制default.properties文件吗?

时间:2012-01-26 08:25:09

标签: java

我正在尝试使用google找到的代码,该代码允许从gmail检索电子邮件并存储到mysql。

当我启动代码时,它正在运行,但最后我有一条错误消息: 无法连接到数据库。

这是正常的,因为我没有知道如何创建default.properties文件: 我知道内容,但格式必须是文件,我必须把这个文件放在哪里?在Netbeans中?

我必须创建一个文件夹吗? 哪个文件格式为txt,java ?? 该文件的名称将为default.properties

代码可在此处找到:sakthimaharai.hubpages.com

我需要一个hlep 请。

谢谢

3 个答案:

答案 0 :(得分:1)

在Netbeans中,您可以使用上下文菜单创建属性文件,以按this answer创建新元素。请注意输入default作为名称,因为NB会将.properties添加到您撰写的任何内容中,您可能会以default.properties.properties结尾。

最常见的是从类路径或工作目录中读取属性文件,在第一种情况下,您应该在文件夹的根目录中创建文件。在第二种情况下,您可以直接在项目节点中创建文件,但在这种情况下,如果您要分发程序,则不会将文件添加到最终的jar / war中。

Examples of the format(甚至还有一些处理属性文件的代码)。

答案 1 :(得分:0)

我不知道代码在哪里尝试加载属性,你没有说太多,但在正常情况下文件应该在项目的类路径中。

属性文件(.properties)如下所示:

key=value
key2=value2
#comment1

提供更多信息,以便我们为您提供帮助。

答案 2 :(得分:0)

好的,这里是谷歌发现的完整代码,今天由作者删除,所以这里免费为任何人感兴趣。

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



    }

,错误信息为:

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

并创建文件default.properties.properties,如Madth3的示例中所述 谢谢

default.properties.properties是:

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();
        }
    }
}