Monday, January 9, 2012

JDBC Programming

Simple JDBC program


package com.miani.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class GetDeptData {
public static void main(String[] args) {

try{


         Class.forName("oracle.jdbc.driver.OracleDriver");
         Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jsltech","jsl");
         Statement st=con.createStatement();
         ResultSet rs=st.executeQuery("select * from dept");
         while(rs.next()){
                     System.out.print(rs.getInt("deptno"));
                     System.out.print(rs.getString("dname"));
                     System.out.print(rs.getString("loc"));
                     System.out.println("\n_______________\n");
     }
        st.close();
        con.close();
    }catch (Exception e) {
          System.out.println(e);
       }
    }
}


The program to store the image into the database

package com.miani.jdbc.psmt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;


public class BookInfo {


/*create table book_details(book_id number(6),book_image blob)*/


public static void main(String[] args) {
         
                   Connection con=null;
                   PreparedStatement ps=null;
                   ResultSet rs=null;
       try{
              
                   Class.forName("oracle.jdbc.driver.OracleDriver");
                   con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jsltech","jsl");
                   ps=con.prepareStatement("insert into book_details(book_id,book_image) values(?,?)");
                   File file=new File("java.jpg");
                   FileInputStream fis=new FileInputStream(file);
                   ps.setInt(1, 10001);
                   ps.setBinaryStream(2, fis,(int)file.length());
                   ps.execute(); 
           }catch (Exception e) {
                   e.printStackTrace();
     }
  }
}


The program to retrive the image from the database

package com.miani.jdbc.psmt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class BookInfo {


/*create table book_details(book_id number(6),book_image blob)*/


public static void main(String[] args) {


           Connection con=null;
           PreparedStatement ps=null;
           ResultSet rs=null;
try
{
                     Class.forName("oracle.jdbc.driver.OracleDriver");
                     con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jsltech","jsl");
                     ps=con.prepareStatement("select book_id,book_image from book_details where book_id=?");
                     ps.setInt(1, 10001);
                     rs=ps.executeQuery();
          
             while(rs.next()){
                        System.out.println(rs.getInt(1));
                        File f=new File(rs.getInt(1)+".jpg");
                        FileOutputStream fos=new FileOutputStream(f);
                        InputStream r=rs.getBinaryStream(2);
                        int i=r.read();
              
              while(i!=-1){
                          fos.write(i);
                          i=r.read();
                     }
            } 
}catch (Exception e) {
                   e.printStackTrace();
              }
         }
}

No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...