Search This Blog

Saturday, July 23, 2011

Wednesday, July 13, 2011

JDBC Connection with Oracle 10g Express Edition

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JdbcConnection
{
    public static Connection createConnection()
    {
        Connection con=null;
        String driverName="oracle.jdbc.driver.OracleDriver";
        String url="jdbc:oracle:thin:@localhost:1521:XE";
        String user="system";
        String password="piyu";
       
       
        try
        {
            Class.forName(driverName);
            con=DriverManager.getConnection(url, user, password);
            if(con!=null)
            {
                System.out.println("Connection Success");
            }
            else {
                System.out.println("Connection  Not Success");
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
       
        return con;
    }
    public static void main(String[] args)
    {
        JdbcConnection connection=new JdbcConnection();
        connection.createConnection();
    }
}

Note : This Program Require classes12.jar Just Go To www.java2s.com and download it..

Friday, July 08, 2011

How To Set JDK CLASSPATH













Note : You Can Download Pdf file from https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0B4Ti1Y9aONzRMzFkMzA4NDQtMDBhNi00MjZiLTlmNjUtOTAzNTU5ZmUzYzIy&hl=en_US

Communicate with Two Applet

AppletToApplet.html

<html>
<body>


<applet code="SumClass" width="300" height=300 name="sumapp" >
</applet>


<applet code="AnsClass" width="300" height="300" name="ansapp" >
</applet>

</body>
</html>

SumClass.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="SumClass" width=300 height=300>
</applet>
*/
/*
<applet code="AnsClass" width=300 height=300>
</applet>
*/

public class SumClass extends Applet implements ActionListener
{
     TextField fNo=new TextField();
     TextField sNo=new TextField();
     Label fNoLable=new Label("Enter First No :-",2);
     Label sNoLable=new Label("Enter Second No :-",2);
     Label error=new Label();
     Button sum =new Button("Sum");
   
    int fNumber,sNumber,ans;
   
    public void init()//Override the applet init method which initilized the componet when the applet start at first....
    {
            setLayout(null);
            add(fNo);//add the first text filed in to applet
            add(sNo);//add the second text filed in to applet
            add(fNoLable);//add the first lable in to applet
            add(sNoLable);//add the second lable in to applet
            add(error);
            add(sum);
            fNoLable.setBounds(50,50,100,50);//set Enter First No :- lable at x,y,width,hight
            sNoLable.setBounds(50,90,120,50);//set Enter First No :- lable at x,y,width,hight
            error.setBounds(190,160,120,70);//set Enter First No :- lable at x,y,width,hight
            fNo.setBounds(190,60,80,25);//set first no textfield :- lable at x,y,width,hight
            sNo.setBounds(190,100,80,25);//set second no textfield :- lable at x,y,width,hight
            sum.setBounds(200,140,50,25);//set Button named Sum x,y,width,hight
            sum.addActionListener(this);//binding the ActionListener with Sum named button
    }
   
    public void actionPerformed(ActionEvent ae)//ovrride the ActionListener's  method which occure when the user click on butten named Sum
    {
        String sum=ae.getActionCommand();
       
       
        String ff,sf;
       

       
        try
        {
                ff=fNo.getText();
                sf=sNo.getText();
                if(ff==null||ff.trim().length()==0)
                {
                    error.setText("Enter First Number");
                }
                else
                {
                    error.setText("");
                    fNumber=Integer.parseInt(fNo.getText());
                   
                    if(sf==null||sf.trim().length()==0)
                    {
                        error.setText("Enter Second Number");
                    }
                    else
                    {
                        error.setText("");
                        sNumber=Integer.parseInt(sNo.getText());
                        ans=fNumber + sNumber;
                        //error.setText(Integer.toString(ans));
                       
                        //AnsClass ac=new AnsClass();
                        AnsClass.ans.setText("");
                        AnsClass.ans.setText(Integer.toString(ans));
                       
                    }
                }
               
        }
        catch(NumberFormatException e)
        {
            error.setText("Enter Proper Number");
        }
        catch(ArithmeticException e)
        {
            System.out.println("Problem During The Arithmetic Operation");
        }
        catch(Exception e)
        {
            System.out.println("Error In Your Input Type");
        }
    }
   
   
}

AnsClas.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/*
<applet code="AnsClass" width=200 height=200>
</applet>
*/

public class AnsClass extends Applet
{
    static TextField ans=new TextField();
    public void init()
    {
        add(ans);
    }
}

Monday, July 04, 2011

Create Dynemic Connnection With Access

import java.sql.*;

public class Conn
{
Connection con;
String database;
Statement stat;

Conn()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Insurance.mdb;";
con=DriverManager.getConnection(database,"","");
stat=con.createStatement();

if(con.isClosed())
{
System.out.println("Closed");
}
else
{
System.out.println("Open");
}
}
catch(Exception ex)
{
System.out.println("Exception : "+ex);
}

}
public static void main(String args[])
{
new Conn();
}
}

Sunday, July 03, 2011

Send E-Mail Using 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 SendMail {

public static final String MAIL_SERVER = "smtp.gmail.com";
public static final String USERNAME = "useremailid"; // Please Add user Email Id
public static final String PASSWORD = "password"; // Please Add user's password

public static void main(String[] args)
{
try
{
String fromAddress = "fromemailId";
String toAddress = "toemdilaid";
String subject = "SMTP";
String message = "Java SMPT SERVER....";

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);
System.out.println("start mail send...");
Transport tr = session.getTransport("smtps");
tr.connect(MAIL_SERVER, USERNAME, PASSWORD);
tr.sendMessage(msg, msg.getAllRecipients());
System.out.println("mail send...");
tr.close();
}
catch (AddressException ex)
{
System.out.println(ex.getMessage());
}
catch (MessagingException ex)
{
System.out.println(ex.getMessage());
}
}
}


Note : This Program Require mail.jar file if u would like to use this program please download mail.jar from www.java2s.com