Skip to main content

Posts

Showing posts with the label JAVA TUTORIAL

HOW TO INTERFACE WITH FILES IN JAVA

Simple example to use string in different ways. Syntax : String variable_name = "Hello World!" Example : import java.io.*;   public class CopyFile {    public static void main(String args[]) throws IOException    {       FileInputStream in = null;       FileOutputStream out = null;         try {          in = new FileInputStream("input.txt");          out = new FileOutputStream("output.txt");                   int c;          while ((c = in.read()) != -1) {             out.write(c);          }       }finally {          if (in != null) {             in.close();          }          if (o...

HOW TO CONCATENATE (join) TWO STRINGS

Simple example to use string in different ways. Syntax : string1 .concat( string2 ) Example : public class StringConcatDemo { public static void main(String args[]) {    String s = "Strings are immutable";    s = s.concat(" all the time");    System.out.println(s);    } } Description : Step 1: Starting of class Step 2: Start of main function. Step 3: Define a string "s" Step 4: Concatenating data of string 's' with new data "all the time". Step 5: Display content of variable "s" to console. Output: Strings are immutable all the time

HOW TO FIND LENGTH OF A STRING IN JAVA

Simple example to use string in different ways. Syntax : Some_string_variable_name .length() Example : public class StringDemo {      public static void main(String args[]) {       String palindrome = "Dot saw I was Tod";       int len = palindrome.length();       System.out.println( "String Length is : " + len );    } }   Description : Step 1: Starting of class Step 2: Start of main function. Step 3: Define a string "palindrome" Step 4: Defining and initializing a variable "len". Here the length(number of characters) of the string "palindrome" is copied to variable "len" which is of integer type. Step 5: Display content of variable "len" to console. Output: String Length is : 17

HOW TO CREATE A STRING IN JAVA

Simple example to use string in different ways. Syntax : String variable_name = "Hello World!" Example : public class String_Demo{      public static void main(String args[]){       char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};       String helloString = new String(helloArray);        System.out.println( helloString );    } } Description : Step 1: Starting of class Step 2: Start of main function. Step 3: Define a character array Step 4: Defining and initializing "helloString". Here value of the variable "Hello array" is copied to the String variable "helloString". Step 5: Display content of variable "helloString" to console.

HOW TO SEND EMAIL WITH ATTACHMENT IN JAVA APPLICATION

Simple application to send an email using java application Example : import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.util.Date; import java.util.Properties;   public class EmailAttachmentDemo {        public static void main(String[] args) {          EmailAttachmentDemo demo = new EmailAttachmentDemo();          demo.sendEmail();      }        public void sendEmail() {          String from = "email@example.com";          String to = "email@example.com";          String subject = "Important Message";          String bodyText = "T...

HOW TO SEND AND EMAIL USING JAVA APPLICATION

Simple example to send an email (without attachment ) using java application Syntax : Example : import javax.mail.Session; import javax.mail.Message; import javax.mail.Transport; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.InternetAddress; import java.util.Properties;   public class Main {   public static void main(String[] args) {     String from = "user@some-domain.com";     String to = "user@some-domain.com";     String subject = "Hi There...";     String text = "How are you?";       Properties properties = new Properties();     properties.put("mail.smtp.host", "smtp.some-domain.com");     properties.put("mail.smtp.port", "25");     Session session = Session.getDefaultInstance(properties, null);       Message message = new MimeMessage(s...

JAVA BASICS - HOW TO USE A STRING IN SWITCH STATEMENTS

Simple example to switch statements using string. Syntax : switch(String variable) { case "" : case "" : case "" : : : . } Example : public class StringSwitchDemo { public static void main(String[] args) {                StringSwitchDemo demo = new StringSwitchDemo();                String day = "Sunday";                switch (day)                {                               case "Sunday":                               demo.doSomething();                               break;                         ...

JDBC - JAVA DATABASE CONNECTIVITY

Java Database connectivity: JDBC is not that difficult as it seem to. Following steps will guide you to set up a java database connection. In the end a simple example will get you the clear idea of how this thing actually works. Step 1: Firstly open "Data Sources".  Start -> Control panel ->Administrative tools -> Data Sources Now you have to add data source that JDBC - ODBC bridge driver can open. To do so click add and the following dialog will appear. Make sure you select Microsoft Access Driver driver as shown above and click finish. In the next screen enter the name of data source name. This is the name you will be using in your java code to open your database, also enter description. Use select button to browse your MS access file that you have created. Click ok .Your database will now be in the list of data source.  Following is the sample java code that will help you to interface your database file. import java.sql.*; ...

HOW TO ADD BACKGROUND IMAGE IN JAVA SWING

The simple example to insert an image to background of frame in java Syntax : ImageIcon("Path-to-imagefile//image_name.its-extension"))); Example :  import javax.swing.*; import java.awt.*; import java.awt.event.*; class BackgroundImageJFrame extends JFrame { JButton b1; JLabel l1;                public BackgroundImageJFrame()                {                setTitle("Background Color for JFrame");                 setLocationRelativeTo(null);                setDefaultCloseOperation(EXIT_ON_CLOSE);                setVisible(true);                  setContentPane(new JLabel(new                             ...