Thursday, 3 June 2010

Write a jdbc program which will insert a record in the Student database?

Write a jdbc program which will insert a record in the Student database?
import java.sql.*;
class InsertRec {
 public static void main(String[] args) {
  try {
   Driver d = new             Sun.jdbc.odbc.JdbcOdbcDriver();
   DriverManager.registerDriver(d);
   System.out.println("DRIVERS LOADED...");
   Connection con =       DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
   System.out.println("CONNECTION ESTABLISHED...");
   Statement st = con.createStatement();
   int i = st.executeUpdate("insert into student values (10,'suman',60.87);");
   System.out.println(i + " ROWS SELECTED...");
   con.close();
  } catch (Exception e) {
   System.out.println("DRIVER CLASS NOT FOUND...");
  }
 }
}

Write a java program to retrieve the data from emp database?

Write a java program to retrieve the data from emp database?
import java.sql.*;
class SelectData {
 public static void main(String[] args) throws Exception {
  DriverManager.registerDriver(new Sun.jdbc.odbc.JdbcOdbcDriver());
  System.out.println("DRIVERS LOADED...");
  Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
  System.out.println("CONNECTION ESTABLISHED...");
  Statement st = con.createStatement();
  ResultSet rs = st.executeQuery("select * from dept");
  while (rs.next()) {
   System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
  }
  con.close();
 }
};

Write a java program to retrieve the records from a specified database by accepting input from keyboard?

Write a java program to retrieve the records from a specified database by accepting input from keyboard?
import java.sql.*;
import java.io.*;
class SelectDataRun {
 public static void main(String[] args) {
   try {
    Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
    System.out.println("CONNECTION ESTABLISHED...");
    PreparedStatement ps = con.prepareStatement("select * from dept where deptno");
    DataInputStream dis = new DataInputStream(System.in);
    System.out.println("ENTER DEPARTMENT NUMBER : ");
    String s1 = dis.readLine();
    int dno = Integer.parseInt(s1);
    ps.setInt(1, dno);
    ResultSet rs = ps.executeQuery();
    while (rs.next()) {
     System.out.print(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
    }
    con.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  } // main
}   // SelectDataRun

Write a java program which illustrates the concept of function?

Write a java program which illustrates the concept of function?
create or replace function StuFun(a in number, b in number, n1 out number)
return number as n2 number;
begin
n1:=a*b;
n2:=a+b;
return (n2);
end;

import java.sql.*;
import java.io.*;
class FunConcept {
 public static void main(String[] args) {
   try {
    DriverManager.registerDriver(new
    oracle.jdbc.driver.OracleDriver());
  System.out.println("DRIVERS LOADED...");
    Connection con =
    DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:
     BudDinu " , "scott " , "tiger ");
     System.out.println("CONNECTION OBTAINED...");
     DataInputStream dis = new DataInputStream(System.in);
     System.out.println("ENTER FIRST NUMBER : ");
     String s1 = dis.readLine();
     System.out.println("ENTER SECOND NUMBER : ");
     String s2 = dis.readLine();
     int n1 = Integer.parseInt(s1);
     int n2 = Integer.parseInt(s2);
     CallableStatement cs = con.prepareCall("{?=call ArthFun (?,?,?)}");
     cs.setInt(2, n1);
     cs.setInt(3, n2);
     cs.registerOutParameter(1, Types.INTEGER);
     cs.registerOutParameter(4, Types.INTEGER);
     cs.execute();
     int res = cs.getInt(1);
     int res1 = cs.getInt(4);
     System.out.println("SUM OF THE NUMBERS : " + res);
     System.out.println("MULTIPLICATION OF THE NUMBERS : " + res1);
    } catch (Exception e) {
     e.printStackTrace();
    }
   } // main
  } // FunConcept

Write a java program which illustrates the concept of procedure?

Write a java program which illustrates the concept of procedure?
create or replace procedure StuPro
(no in number, name in varchar2, loc1 out varchar2)
as
begin
select dname, loc into name, loc1 from dept
where deptno=no;
insert int abc values (no, name, loc1);
end;

import java.sql.*;
import java.io.*;
class ProConcept {
 public static void main(String[] args) {
   try {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:
     BudDinu ","scott ","tiger ");
     DataInputStream dis = new DataInputStream(System.in);
     System.out.println("ENTER DEPARTMENT NUMBER : ");
     String s1 = dis.readLine();
     int n1 = Integer.parseInt(s1);
     CallableStatement cs = con.prepareCall("{call StuPro (?,?,?)}");
     cs.setInt(1, n1);
     cs.registerOutParameter(2, Types.VARCHAR);
     cs.registerOutParameter(3, Types.VARCHAR);
     cs.execute();
     String res = cs.getString(2);
     String res1 = cs.getString(3);
     System.out.println("DEPARTMENT NAME : " + res);
     System.out.println("DEPARTMENT LOCATION : " + res1);
    } catch (Exception e) {
     System.out.println(e);
    }
   } // main
  } // ProConcept

Write a java program which illustrates the concept of DatabaseMetaData and ResultSetMetaData?

Write a java program which illustrates the concept of DatabaseMetaData and ResultSetMetaData?
import java.sql.*;
class MetaData {
 public static void main(String[] args) {
   try {
    DriverManager.registerDriver(new Sun.jdbc.odbc.JdbcOdbcDriver());
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc : odbc : oradsn", "scott", "tiger");
    System.out.println("CONNECTION ESTABLISHED...");
    // UNIVERSAL DATABASE DETAILS
    DatabaseMetaData dmd = con.getMetaData();
    System.out.println("DATABASE NAME : " + dmd.getDatabaseProductName());
    System.out.println("DATABASE VERSION : " + dmd.getDatabaseProductVersion());
    System.out.println("NAME OF THE DRIVER : " + dmd.getDriverName());
    System.out.println("VERSION OF THE DRIVER : " + dmd.getDriverVersion());
    System.out.println("MAJOR VERSION OF DRIVER : " + dmd.getDriverMajorVersion());
    System.out.println("MINOR VERSION OF DRIVER : " + dmd.getDriverMinorVersion());
    // USER DATABASE DETAILS
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from dept");
    ResultSetMetaData rsmd = rs.getMetaData();
    System.out.println("NUMBER OF COLUMNS : " + rsmd.getColumnCount());
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
     System.out.println("NAME OF THE COLUMN : " + rsmd.getColumnName(i));
     System.out.println("TYPE OF THE COLUMN : " + rsmd.getColumnType(i));
    }
    con.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  } // main
} // MetaData

Write a jdbc program to retrieve the data from excel?

Write a jdbc program to retrieve the data from excel?
import java.sql.*;
class XSelect {
 public static void main(String[] args) {
   try {
    DriverManager.registerDriver(new
    Sun.jdbc.odbc.JdbcOdbcDriver());
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc:odbc:xldsn");
    System.out.println("CONNECTION ESTABLISHED...");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from [student$]");
    while (rs.next()) {
     System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
    }
    con.close();
   } catch (SQLException sqle) {
    sqle.printStackTrace();
   }
  } // main
} // XSelect

Write a java program which illustrates the concept of resource bundle file or how to develop a flexible jdbc application along with its metadata?

Write a java program which illustrates the concept of resource bundle file or how to develop a flexible jdbc application along with its metadata?
import java.sql.*;
import java.io.*;
import java.util.*;
class RBFConcept {
 public static void main(String[] args) {
   try {
    FileInputStream fis = new FileInputStream("rbfdb.prop");
    Properties p = new Properties();
    p.load(fis);
    String dname = (String) p.get("Dname");
    String url = (String) p.get("URL");
    String username = (String) p.get("Uname");
    String password = (String) p.get("Pwd");
    String tablename = (String) p.get("Tablename");
    // loading drivers and obtaining connection
    Class.forName(dname);
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection(url, username, password);
    System.out.println("CONNECTION CREATED...");
    // executing query
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from" + tablename);
    ResultSetMetaData rsmd = rs.getMetaData();
    // printing column names
    System.out.println("=================================");
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
     System.out.print(rsmd.getColumnName(i) + " ");
    }
    System.out.println("");
    System.out.println("=================================");
    // printing the data
    while (rs.next()) {
     for (int j = 1; j <= rsmd.getColumnCount(); j++) {
      System.out.print(rs.getString(j) + " ");
     }
    }
    con.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
  } // main
}   // RSFConcept

Write a java program which points the data of a table along with its column names?

Write a java program which points the data of a table along with its column names?
import java.sql.*;
class Table {
 public static void main(String[] args) {
   try {
    DriverManager.registerDriver(new Sun.jdbc.odbc.JdbcOdbcDriver());
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
    System.out.println("CONNECTION ESTABLISHED...");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from dept");
    ResultSetMetaData rsmd = rs.getMetaData();
    System.out.println("===========================================");
    // PRINTING COLUMN NAME
    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
     System.out.print(rsmd.getColumnName(i) + " ");
    }
    System.out.println("");
    System.out.println("==========================================");
    // PRINTING THE DATA OF THE TABLE
    while (rs.next()) {
     for (int j = 1; j <= rsmd.getColumnCount(); j++) {
      System.out.print(rs.getString(j) + " ");
     }
     System.out.println("");
    }
    con.close();
   } catch (SQLException sqle) {
    sqle.printStackTrace();
   }
  } // main
}; // Table

Write a java program which illustrates the concept of scrollable ResultSet?

Write a java program which illustrates the concept of scrollable ResultSet?
import java.sql.*;
class ScrollResultSet {
 public static void main(String[] args) {
   try {
    Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
    System.out.println("DRIVERS LOADED...");
    Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
    System.out.println("CONNECTION ESTABLISHED...");
    Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = st.executeQuery("select * from emp");
    System.out.println("RECORDS IN THE TABLE...");
    while (rs.next()) {
     System.out.println(rs.getInt(1) + " " + rs.getString(2));
    }
    rs.first();
    System.out.println("FIRST RECORD...");
    System.out.println(rs.getInt(1) + " " + rs.getString(2));
    rs.absolute(3);
    System.out.println("THIRD RECORD...");
    System.out.println(rs.getInt(1) + " " + rs.getString(2));
    rs.last();
    System.out.println("LAST RECORD...");
    System.out.println(rs.getInt(1) + " " + rs.getString(2));
    rs.previous();
    rs.relative(-1);
    System.out.println("FIRST RECORD...");
    System.out.println(rs.getInt(1) + " " + rs.getString(2));
    con.close();
   } catch (Exception e) {
    System.out.println(e);
   }
  } // main
}; // ScrollResultSet

JDBC Programs