Mk47
Mk47
Reputation Top 1%
Mk47
3 Snippets  (209th place)
Published
0 Channels
Created
2 Channels
Following
226 points  (134th place)
Reputation
Junior Code Generator
Serious Code Generator
Senior Code Generator
Junior Publisher
Junior Judge
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer
Junior Famous Coder

Recent Snippets See all snippets by Mk47

public by Mk47  2775  0  5  3

Connecting to MySQL database using JAVA

Works only after you add java mysql driver jar into your library
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Dconnector {
	private Connection con;
	private Statement st;
	private ResultSet rs;

	public Dconnector() {
		try {
			Class.forName("com.mysql.jdbc.Driver");

			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/goosehunters", "root", ""); // Connection execution takes place here
			st = con.createStatement();																   // Statement execution takes place here

		} catch (Exception ex) {
			System.out.println("Dconnector exception => " + ex);
		}
	}

	public void getData() {
		try {
			String query = "SELECT * FROM goose";
			rs = st.executeQuery(query);						//ResultSet execution takes place here
			System.out.println("records from database\t");		//goose is the column name of the table goosehunters
			while (rs.next()) {
				String name = rs.getString("name");				
				String age = rs.getString("age");
				System.out.println(name + " \t" + age);
			}
		} catch (Exception ex) {
			System.out.println("getData exception => " + ex);
		}
	}
}

//----------------------------------------MAIN CLASS-------------------------------------------//

public class Main extends Dconnector {

	public static void main(String[] args) {
		 Dconnector a = new Dconnector();
		 a.getData();

	}

}  
;

public by Mk47  2727  3  5  2

Sending multiple email using Java ( Using Gmail SMTP )

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JavaEmail
{
    Session mailSession;

    public static void main(String args[]) throws AddressException,    MessagingException
    {
        JavaEmail javaEmail = new JavaEmail();
        javaEmail.setMailServerProperties();
        javaEmail.draftEmailMessage();
        javaEmail.sendEmail();
    }

    private void setMailServerProperties()
    {
        Properties emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", "587");
        emailProperties.put("mail.smtp.auth", "true");
        emailProperties.put("mail.smtp.starttls.enable", "true");
        mailSession = Session.getDefaultInstance(emailProperties, null);
    }

    private MimeMessage draftEmailMessage() throws AddressException, MessagingException
    {
        String[] toEmails = { "Google@gmail.com" };
        String emailSubject = "Test email subject";
        String emailBody = "This is a test message";
        MimeMessage emailMessage = new MimeMessage(mailSession);
        /**
         * Set the mail recipients
         * */
        for (int i = 0; i < toEmails.length; i++)
        {
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
        }
        emailMessage.setSubject(emailSubject);
        /**
         * If sending HTML mail
         * */
        emailMessage.setContent(emailBody, "text/html");
        /**
         * If sending only text mail
         * */
        //emailMessage.setText(emailBody);// for a text email
        return emailMessage;
    }

    private void sendEmail() throws AddressException, MessagingException
    {
        /**
         * Sender's credentials
         * */
        String fromUser = "user-email@gmail.com";
        String fromUserEmailPassword = "*******";

        String emailHost = "smtp.gmail.com";
        Transport transport = mailSession.getTransport("smtp");
        transport.connect(emailHost, fromUser, fromUserEmailPassword);
        /**
         * Draft the message
         * */
        MimeMessage emailMessage = draftEmailMessage();
        /**
         * Send the mail
         * */
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        System.out.println("Email sent successfully.");
    }
}  
;

public by Mk47  2550  0  5  0

Listing all the files in all directories

import java.io.File;
// author: Sagar Mankari

public class FileNavigator {
	public static void main(String args[])
	{
	String treeRoot = ".";		//default root
	if(args.length >=1 )
		treeRoot = args[0];
	File rootDir = new File(treeRoot);
	System.out.println("Root of navigation: " +rootDir.getAbsolutePath());
	// check if the root exists as a directory
	if(!(rootDir.isDirectory()))
	{
		System.out.println("The root of the navigation subtree does not exist as a directory!");
		System.exit(0);
		
	}
	FileNavigator fn = new FileNavigator();
	fn.navigate(rootDir);
	
	}
	public void navigate(File dir)
	{
		System.out.println("");
		System.out.println("Directory"+dir+":");
		String[] dirContent = dir.list();
		for(int i=0; i<dirContent.length;i++)
		{
			System.out.println(""+dirContent[i]);
			File child = new File(dir,dirContent[i]);
			if(child.isDirectory())navigate(child);
			
		}
	
	}

}
;