Play Movie From Database

Saturday 15 March 2014

 

What You Need Is

  1. Java Version 6 or Later 
  2. Ms Sql Server 2005 or Later
  3. Netbeans Ide 6.5 or Later
  4. Os Windows 7
  5. Java Media Framework (JMF)
  6. Jdbc Odbc Connectivity
  7. Turn Off Windows 7 User Account Control (UAC)

 

STEP 1.

 

Create a database named as circuit and a Table named as Images with column named as puploads and name

Here I am providing the script

CREATE database circuit

CREATE TABLE Images
(
    [puploads] [image] NULL,
    [name] [varchar](50)

)

 

STEP 2.

 

To communicate with database java has to use Jdbc Odbc Drivers

Now Goto,

Start menu—>Control Panel—>System and Security—>Administrative Tools

—>Data Sources (ODBC)

Here is the link

http://proamrishwas.blogspot.in/2014/03/jdbc-odbc-connectivity.html

 

STEP 3.

 

Due to windows 7 doesn't give permission to read write file we have to turn off User Account Control(UAC)

Just type UAC into the start menu or Control Panel search box.

and scroll to Never Notify

 

STEP 4.

 

Now The Coding Part

Uploding Video File to database

try
       {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       }
       catch(ClassNotFoundException e)
       {
           System.out.println(e);
       }
      
       try
       {
       String name1=jTextField2.getText().trim();
       namex=name1;
       String pic="INSERT INTO Images([Puploads],[name]) VALUES(?,?)";
       String ure="jdbc:odbc:xer";
       Connection con=null;
       con = DriverManager.getConnection(ure,"sa","sa");
       String url=jTextField1.getText().trim();
       File f=new File(url);
       FileInputStream fis=new FileInputStream(f);
       PreparedStatement pst=con.prepareStatement(pic);
       pst.setBinaryStream(1, fis, fis.available());
       pst.setString(2,name1);
       int rxb=pst.executeUpdate();
       pst.close();
       con.close();
      
       }
       catch(Exception sd)
       {
           System.out.println(sd);
       }

 

STEP 5.

 

Now Retriving the video or movie file

try
{

String namex=jTextField2.getText().trim();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
    System.out.println(e);
}
String ure="jdbc:odbc:xer";
Connection con=null;
  
con = DriverManager.getConnection(ure,"sa","sa");
String query = "SELECT (Puploads) from Images where name ='"+namex+"'";
//out.println(query);
ResultSet rs = con.createStatement().executeQuery(query);
if(rs.next())
{

byte[] bytearray = new byte[4096];
int size=0;
InputStream image;
image = rs.getBinaryStream(1);
File f=new File(System.getProperty("java.io.tmpdir")+"/temp.mpg");
FileOutputStream fos=new FileOutputStream(f);
while((size=image.read(bytearray))!= -1 )
{
    fos.write(bytearray);

//it will write the file in temp folder C:\Users\xxx\AppData\Local\Temp
}

image.close();
rs.close();
fos.close();
con.close();
}
}
catch (Exception exc)
{ System.out.println(exc);
}
        
 

try
{
String mdurl="file:///"+System.getProperty("java.io.tmpdir")+"/temp.mpg";
URL url=new URL(mdurl);
Player player=Manager.createRealizedPlayer(url);
Component cmpt=player.getVisualComponent();
       
Component ctrls=player.getControlPanelComponent();
cmpt.resize(600, 400);
this.add(cmpt,BorderLayout.CENTER);
ctrls.resize(600, 400);
this.add(ctrls,BorderLayout.SOUTH);
player.start();
this.resize(600, 470);
}
catch(Exception sdd)
{
            System.out.println(sdd);
  }
       
       

Basically this program first read video or movie file from database writes that file to temproray directory of windows and than it plays.

Note:-Java Media Framework not supports all media formats I have used .avi and .mpg file in this project

If you have any Query and Comments please Write down in comment Box

Thanks

0 comments:

Paste Long Url