Write an example for scrollable result set with read only mode
package com.java2novice.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyScrollableResultSetEx
{
public static void main(String a[])
{
Connection
con = null;
Statement
st = null;
ResultSet
rs = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con
= DriverManager.
getConnection("jdbc:oracle:thin:
::","user","password");
st =
con.createStatement(ResultSet.type_scroll_sensitive,
ResultSet.concur_read_only);
rs
= st.executeQuery("select accno, bal from bank");
System.out.println("ResultSet
Curson is at before first: "+rs.isBeforeFirst());
while(rs.next())
{
System.out.println(rs.getInt(1)+"
"+rs.getDouble(2));
}
System.out.println("Is
After Last: "+rs.isAfterLast());
while(rs.previous())
{
System.out.println(rs.getInt(1)+"
"+rs.getDouble(2));
}
}
catch (ClassNotFoundException
e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs
!= null) rs.close();
if(st
!= null) st.close();
if(con
!= null) con.close();
}
catch(Exception
ex){}
}
}
}
Write an example for updatable result set
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyUpdatableResultSet
{
public static void main(String a[])
{
Connection
con = null;
Statement
st = null;
ResultSet
rs = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con
= DriverManager.
getConnection("jdbc:oracle:thin:
::" ,"user","password");
st
= con.createStatement(ResultSet.type_scroll_sensitive,
resultset.concur_updatable);
rs
= st.executeQuery("select accno, bal from bank");
while(rs.next())
{
if(rs.getInt(1)
== 100)
{
rs.updateDouble(2,
2000);
rs.updateRow();
System.out.println("Record
updated!!!");
}
}
}
catch (ClassNotFoundException
e)
{
e.printStackTrace();
}
catch (SQLException e)
{
try
{
e.printStackTrace();
}
finally
{
if(rs != null) rs.close();
if(st
!= null) st.close();
if(con
!= null) con.close();
}
}
catch(Exception ex){}
}
}
How to insert an image into database table? or Write an example for inserting BLOB into table
package com.java2novice.jdbc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MyBlobInsert
{
public static void main(String a[])
{
Connection
con = null;
PreparedStatement
ps = null;
InputStream
is = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con
= DriverManager.
getConnection("jdbc:oracle:thin:
::"
,"user","password");
ps
= con.prepareCall("insert into student_profile values (?,?)");
ps.setInt(1,
101);
is
= new FileInputStream(new File("Student_img.jpg"));
ps.setBinaryStream(2,
is);
int count =
ps.executeUpdate();
System.out.println("Count:
"+count);
}
catch (ClassNotFoundException
e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
catch (FileNotFoundException
e)
{
e.printStackTrace();
}
finally
{
try
{
if(is
!= null) is.close();
if(ps
!= null) ps.close();
if(con
!= null) con.close();
}
catch(Exception
ex){}
}
}
}
No comments:
Post a Comment