Showing posts with label Media. Show all posts
Showing posts with label Media. Show all posts

Tuesday, April 15, 2014

CLOBs, NCLOBs and BLOBs

CLOB, NCLOB and BLOB are database data types with data size up to 128 terabytes. The data size of regular VARCHAR and VARBINARY is 32 kilobytes.

CLOB stands for character large object, which is a Unicode character string. It is appropriate for storing text-orientated information.

NCLOB stands for national character large object, which is a Unicode character string. This data type like the CLOB data type, is appropriate for storing text-orientated information.

BLOB stands for binary large object, which is a binary string. This data type is appropriate for storing images, sound, and videos.

Following is a sample code of using these data types.

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.NClob;
import java.sql.PreparedStatement;

public class Test {
      public static void main(String[] args) {
            Connection conn = null;
            try {
                  conn = <your method of getting the database connection>;
         

               //Save data to database

                  //Create the data type objects
                  Clob theClob = conn.createClob();
                  NClob theNclob = conn.createNClob();
                  Blob theBlob = conn.createBlob();
         
                  //Add data to the data type objects
                  BufferedReader charReader = new BufferedReader(
                           new FileReader("descritpion.txt"));
                  StringBuilder file = new StringBuilder();
                  String line = "";
                  while ((line = charReader.readLine()) != null){
                         file.append(line);
                  }
         
                  theClob.setString(1, file.toString());
                  theNclob.setString(1, file.toString());
                  charReader.close();
         
                 BufferedInputStream input = new BufferedInputStream(
                          new FileInputStream("happyBird.gif"));
         
                 OutputStream byteWriter = theBlob.setBinaryStream(1);
                  int theChar = -1;
                  while ((theChar = input.read()) != -1) {
                        byteWriter.write(theChar);
                  }
                  byteWriter.flush();
                  input.close();
                  byteWriter.close();
         
                  //Insert the data into database
                  String sql = "INSERT into picture values(?, ?, ?)";
                  PreparedStatement pstmt = conn.prepareStatement(sql);
                  pstmt.setClob(1, theClob);
                  pstmt.setNClob(2, theNclob);
                  pstmt.setBlob(3, theBlob);
                  pstmt.executeUpdate();
                  pstmt.close();

              //Retrieve data from database

                  String sql2 = "SELECT title, description, image from picture";
                  pstmt = conn.prepareStatement(sql2);
                  ResultSet rs = pstmt.executeQuery();
                  while (rs.next()) {
                         Clob myClob = rs.getClob(1);
                         String title = myClob.getSubstring(1, myClob.length());

                         NClob myNclob = rs.getNClob(2);
                         String description = myNclob.getSubstring(1, myNclob.length());

                         Blob myBlob = rs.getBlob(3);
                         byte[] imageArray = myBlob.getBytes(1, myBlob.length());
                  }
                   rs.close();
                   pstmt.close();
                  conn.close();
            }catch (Exception e){
                  e.printStackTrace();
            }
      }
}

References:

1.Using Large Objects
2. 26 Oracle Data Types

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Monday, March 31, 2014

JavaFX - Add video to the screen

To add a Video to your JavaFX screen, first create a Media using the video file, then create a MediaPlayer using the Media, and create a MeidaView using the created MediaPlayer or set the MediaPlayer of your MediaView to the created MediaPlayer. Finally add the MediaView to your scene.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;

public class VedioTest extends Application {
 
    @Override
    public void start(Stage primaryStage) {
        //Creating the Media
        //Either use absolute path of the audio clip or it should be in the same place
        //where the Class used to load it is located
        //Here the Kalimba.mp3 should be in the same directory as the VedioTest
        Media media = new Media(VedioTest.class.getResource("TestVideo.mp4").toString());
     
        //Creating the MediaPlayer
        final MediaPlayer mediaPlayer = new MediaPlayer(media);
//        Make the video automatically play forever
//        mediaPlayer.setAutoPlay(true);
//        mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
     
        //Creating the MediaView
        MediaView mediaView = new MediaView(mediaPlayer);
     
        Button btn = new Button();
        btn.setText("Play Video");
        btn.setOnAction(new EventHandler<ActionEvent>() {
         
            @Override
            public void handle(ActionEvent event) {
                //Call one of the methods of MediaPlayer such as
                //play(), pause(), and stop()
                mediaPlayer.play();
            }
        });
     
        //Add the MediaView to the scene
        Pane root = new Pane();
        btn.relocate(50, 200);
        root.getChildren().addAll(mediaView, btn);
//        root.getChildren().add(btn);
     
        Scene scene = new Scene(root, 300, 250);
     
        primaryStage.setTitle("Video!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 
----------------------------------------------------------------------------------------------------------  

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

Friday, March 14, 2014

JavaFX - AudioClip

To add an AudioClip to your JavaFX screen, create an AudioClip and call its method play().

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;

public class AudioClipTest extends Application {
 
    @Override
    public void start(Stage primaryStage) {
        //Creating the AudioClip
        //Either use absolute path of the audio clip or it should be in the same place
        //where the Class used to load it is located
        //Here the Kalimba.mp3 should be in the same directory as the AudioClipTest
        final AudioClip audioClip = new AudioClip(AudioClipTest.class.getResource("Kalimba.mp3").toString());
     
        Button btn = new Button();
        btn.setText("Song: Kalimba");
        btn.setOnAction(new EventHandler<ActionEvent>() {
         
            @Override
            public void handle(ActionEvent event) {
                //calling the AudioClip's play method
                audioClip.play();
            }
        });
     
        StackPane root = new StackPane();
        root.getChildren().add(btn);
     
        Scene scene = new Scene(root, 300, 250);
     
        primaryStage.setTitle("My favorate songs!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
 
--------------------------------------------------------------------------------------------------------------  

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.