Official Site

click here to view our new site launched

Pages

Tuesday, February 8, 2011

Random number generation in Java

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RandomGen {

public String genRandom() throws Exception {

String random = "";

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSS");

random+=sdf.format(date);

return random;
}

}

Friday, February 4, 2011

Store and Read image from Database Using Java



/* To Store image in database- Store.java */
import java.io.*;
import java.sql.*;

public class Store {


public static void main(String args[])throws Exception


{Connection con=null;
String url="";



try{
// Step 1. Load the Type 1 database driver:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Step 2. Create a connection to the database
// using pre-defined Data Source:
File f=new java.io.File("c:/Temp.accdb");
File c=new java.io.File(System.getProperty("user.dir")+"/Temp.accdb");

if(f.exists())
url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +f.getPath();
else if(c.exists())
url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +c.getPath();

con = DriverManager.getConnection(url,"","");
File imgfile = new File("D:/semanticweb.jpg");

if(!imgfile.exists())
imgfile=new File("semanticweb.jpg");

FileInputStream fis = new FileInputStream(imgfile);
PreparedStatement psmnt = con.prepareStatement("INSERT INTO IMAGESTORAGE(IMAGE) VALUES(?)");
psmnt.setBinaryStream(1, (InputStream)fis, (int)(imgfile.length()));
int insertCount= psmnt.executeUpdate();
System.out.println(insertCount);
psmnt.close();
con.close();
}

catch(Exception e)
{
System.out.print(""+e);
}
}
}



/* To retrieve image from database and store in local directory- Retrieve.java */

import java.io.*;
import java.sql.*;

public class Retreival
{


public static void main(String args[])throws Exception


{
Connection con=null;
String url="";

int c;


// Step 1. Load the Type 1 database driver:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Step 2. Create a connection to the database
// using pre-defined Data Source:
File f=new java.io.File("c:/Temp.accdb");
File ff=new java.io.File(System.getProperty("user.dir")+"/Temp.accdb");

if(f.exists())
url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +f.getPath();
else if(ff.exists())
url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +ff.getPath();

con = DriverManager.getConnection(url,"","");
PreparedStatement psmnt = con.prepareStatement("SELECT IMAGE FROM IMAGESTORAGE");
ResultSet rs = psmnt.executeQuery();
if(rs.next())
{

InputStream fis1;
FileOutputStream fos;
fis1 = rs.getBinaryStream("image");
fos = new FileOutputStream(new File("SunsetImageRead.jpg"));

while ((c = fis1.read()) != -1)
{
fos.write(c);
}

fis1.close();
fos.close();

}

}
}


Click here to download Complete workspace (database,image,Retrieve.java, Store.java)




Send Mail in Java


package mail;

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 SendMail {
String MAIL_SERVER = "smtp.gmail.com";


public void Sendmail(String USERNAME,String PASSWORD,String fromAddress,String toAddress,String subject,String message) {
try {

Properties properties = System.getProperties();
properties.put("mail.smtps.host", MAIL_SERVER);
properties.put("mail.smtps.auth", "true");

Session session = Session.getInstance(properties);
MimeMessage msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(fromAddress));
msg.addRecipients(Message.RecipientType.TO, toAddress);
msg.setSubject(subject);
msg.setText(message);

Transport tr = session.getTransport("smtps");
tr.connect(MAIL_SERVER, USERNAME, PASSWORD);
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
} catch (AddressException ex) {
System.out.println(ex.getMessage());
}
catch (MessagingException ex) {
System.out.println(ex.getMessage());
}

}



public static void main(String[] args){

String USERNAME="xxxx@gmail.com";
String PASSWORD="your password";
String fromAddress="xxx@gmail.com";
String toAddress="toaddress";
String subject="your subject";
String message="your message";

s.Sendmail(USERNAME, PASSWORD, fromAddress, toAddress, subject, message);

}


}



click here to download mail.jar and SendMail Java bean
$2.49 .COM at GoDaddy.com! Expires 5/14/13