How to get column properties from Result Set using Result Set Meta Data
package com.java2novice.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class MyResultSetMetadata
{
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();
rs
= st.executeQuery("select * from emp");
ResultSetMetaData
rsmd = rs.getMetaData();
int columnCount =
rsmd.getColumnCount();
for(int i=0;i<=columnCount;i++)
{
System.out.println(rsmd.getColumnName(i));
System.out.println(rsmd.getColumnType(i));
}
}
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 batch update using Statement
package com.java2novice.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MyStatementBatchUpdate
{
public static void main(String a[])
{
Connection
con = null;
Statement
st = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con
= DriverManager.
getConnection("jdbc:oracle:thin:::","user","password");
con.setAutoCommit(false);
st
= con.createStatement();
st.addBatch("update
emp set sal=3000 where empid=200");
st.addBatch("insert
into emp values (100,4000)");
st.addBatch("update
emp set emp name='Ram' where empid=345");
int count[] =
st.executeBatch();
for(int i=1;i<=count.length;i++)
{
System.out.println("Query
"+i+" has effected "+count[i]+" times");
}
con.commit();
}
catch (ClassNotFoundException
e)
{
try
{
con.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
e.printStackTrace();
}
catch (SQLException e)
{
try
{
con.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try
{
if(st
!= null) st.close();
if(con
!= null) con.close();
}
catch(Exception
ex){}
}
}
}
Write an example for batch update using Prepared Statement
package
com.java2novice.jdbc;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
public
class MyBatchPreparedStmt
{
public
static void main(String a[])
{
Connection
con = null;
PreparedStatement
pst = null;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con
= DriverManager.
getConnection("jdbc:oracle:thin: ::" ,"user","password");
con.setAutoCommit(false);
pst
= con.prepareStatement("update emp set sal=? where empid=?");
pst.setInt(1,
3000);
pst.setInt(2,
200);
pst.addBatch();
pst.setInt(1,
4000);
pst.setInt(2,
230);
pst.addBatch();
int
count[] = pst.executeBatch();
for(int
i=1;i<=count.length;i++)
{
System.out.println("Query
"+i+" has effected "+count[i]+" times");
}
con.commit();
}
catch (ClassNotFoundException e)
{
try
{
con.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
e.printStackTrace();
}
catch (SQLException e)
{
try
{
con.rollback();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
e.printStackTrace();
}
finally
{
try
{
if(pst
!= null) pst.close();
if(con
!= null) con.close();
}
catch(Exception ex){}
}
}
}
No comments:
Post a Comment