Monday, November 3, 2014

Java database pro



How to read an image from database table? or Write an example for reading BLOB from table

package com.java2novice.jdbc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyBlobRead
{
public static void main(String a[])
{
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        InputStream is = null;
        OutputStream os = null;
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.
         getConnection("jdbc:oracle:thin: ::
            ,"user","password");
            st = con.createStatement();
            rs = st.executeQuery("select student_img from student_profile where id=101");
            if(rs.next())
            {
                is = rs.getBinaryStream(1);
             }
            is = new FileInputStream(new File("Student_img.jpg"));
            os = new FileOutputStream("std_img.jpg");
            byte[] content = new byte[1024];
            int size = 0;
            while((size = is.read(content)) != -1)
            {
                os.write(content, 0, size);
            }
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SQLException e)
          {
            e.printStackTrace();
        }
         catch (FileNotFoundException e)
         {
              e.printStackTrace();
        }
         catch (IOException e)
        {
            e.printStackTrace();
        }
         finally
         {
            try
             {
                if(is != null) is.close();
                if(os != null) os.close();
                if(st != null) st.close();
                if(con != null) con.close();
            }
             catch(Exception ex){}
        }
    }
}

What is Database Meta Data write an example code

package com.java2novice.jdbc;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MyDatabaseMetadata
{
 public static void main(String a[])
{
      Connection con = null;
 try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.
                getConnection("jdbc:oracle:thin: ::","user","password");
            DatabaseMetaData dm = con.getMetaData();
            System.out.println(dm.getDriverVersion());
            System.out.println(dm.getDriverName());
            System.out.println(dm.getDatabaseProductName());
            System.out.println(dm.getDatabaseProductVersion());
        }
 catch (SQLException e)
{
                e.printStackTrace();
 }
finally
{
            if(con != null){}
                try
            {
                    con.close();
                }
 catch (SQLException e)
 {
                     e.printStackTrace();
   }
            }
        }
    }
}

How to get JDBC Connection object using properties file

package com.java2novice.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class MyConnWithProperties
{
public static void main(String a[])
{
        System.setProperty("Jdbc.drivers", "oracle.jdbc.driver.OracleDriver");
        Properties prop = new Properties();
        prop.put("user", "user-name");
        prop.put("password", "password");
        try
       {
            Connection con = DriverManager
                .getConnection("jdbc:oracle:thin: ::", prop);
            System.out.println("Got Connection Object");
        }
        catch (SQLException e)
       {
                        e.printStackTrace();
        }
    }
}

No comments:

Post a Comment