Wednesday, 11 December 2013

jdbc program for inserting the values into the table

import java.sql.*;
public class Ex3
{
 public static void main(String arg[]) throws SQLException, ClassNotFoundException
 {
  String driverClassName="com.mysql.jdbc.Driver";
  String url="jdbc:mysql://localhost:3306/test";
  String userName="root";
  String password="admin";

 //load driver class,
  Class.forName(driverClassName);
 //get the connection
 Connection con=DriverManager.getConnection(url,userName,password);
 //get the statement
 Statement st=con.createStatement();
 //execute the query


 int count=st.executeUpdate("insert into students values(103,'ram','sec')");
 System.out.println("no of rows "+count);
  //close the connection
 con.close();
 }//main
}//class
/*
/*
D:\jdbc>set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.1.18-bin.jar;.;
D:\jdbc>javac Ex3.java
D:\jdbc>java Ex3
*/

*/

inserting the values connecting to the db using user id and password in Mysql

//connecting to the db using user id and password

import java.sql.*;
public class Ex2
{
 public static void main(String arg[]) throws SQLException, ClassNotFoundException
 {
  String driverClassName="com.mysql.jdbc.Driver";
  String url="jdbc:mysql://localhost:3306/test";
  String userName="root";
  String password="admin";
//  String q="insert into students values(101,\'kumar\')";//or
 // String q="insert into students values(102,'raj')";
 //load driver class,
  Class.forName(driverClassName);
 //get the connection
 Connection con=DriverManager.getConnection(url,userName,password);
 //get the statement
 Statement st=con.createStatement();
 //execute the query
// int count=st.executeUpdate(q);
//or
 int count=st.executeUpdate("insert into students values(103,'ram','sec')");
 System.out.println("no of rows "+count);
  //close the connection
 con.close();
 }//main
}//class
/*
D:\jdbc>set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.1.18-bin.jar;.;
D:\jdbc>javac Ex2.java
D:\jdbc>java Ex2
*/

inserting into the table using url

//connecting to the db using url
import java.sql.*;//jdbc api
class jdbcdemo
{

public static void main(String[] args) throws Exception
{

//Class.forName("oracle.jdbc.driver.OracleDriver");
//Connection conn = DriverManager.getConnection("jdbc:odbc:oracleXE","sai","sai");
//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","sai","sai" );
//loading the jdbc driver
Class.forName("com.mysql.jdbc.Driver");

//establish the connection with db sw
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "admin");

Statement st= con.createStatement();

st.executeUpdate("insert into emp values(20,'sai',200)");
System.out.println("Rows inserteded");
st.close();
con.close();

}



}
/*
D:\jdbc>set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.1.18-bin.jar;.;
D:\jdbc>javac jdbcdemo2.java
D:\jdbc>java jdbcdemo2
*/

creating table in MYSQL

//using Mysql and database name is test, so create one database in mysql(ex: in this prog
//i have already created one database in 'test'. If u wont create u get error
import java.sql.*;//jdbc api
public class CreateTable
{
public static void main(String args[]) throws SQLException, ClassNotFoundException
{
//get connection
Connection con=prepareConnection();
//obtain a statement
Statement st=con.createStatement();
String q= "CREATE TABLE `test`.`mytable`( `id` INT(3) NOT NULL AUTO_INCREMENT, `name` VARCHAR(20), `add` VARCHAR(40), PRIMARY KEY (`id`) )";


//execute the query
st.executeUpdate(q);
System.out.println("Table created successfully");
con.close();


}
public static Connection prepareConnection() throws SQLException, ClassNotFoundException
{
String d="com.mysql.jdbc.Driver";//driver class name
String url="jdbc:mysql://localhost:3306/test";
String uname="root";
String pwd="admin";
//load driver class
Class.forName(d);
//obtain the connection
Connection con=DriverManager.getConnection(url,uname,pwd);
return(con);

}
}
/* TO RUN THIS PROGRAM

1) Install MySql , i gave my username is "root", password is "admin"
2) 2) download jar files of MySql: "mysql-connector-java-5.1.18-bin.jar" and save in any folder i saved this jar file in "D:\softwares\MySQL\new"
3) set the class path:
example

D:\jdbc>set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.1.18-bin.jar
;.;
4) now compile the program

D:\jdbc>javac CreateTable.java
5) execute the program
D:\jdbc>java CreateTable




Thursday, 5 December 2013

RowSets

•       We can send only those java objects over the network that are Serializable.
•       In order to make object as Serializable object, the class of the object must implement java.io.Serializable interface.
•       In order to store data of the object in a file, the object must be serializable object.
•       ResultSet object is not Serializable object, so we can not send this object over the network directly.
•       To overcome this problem, there are two solutions
•       1. Use RowSets in the place of ResultSets.
•       2. copy data of ResultSet object to RowSet
•       Rowsets are Serializable objects and given as enhancement of ResultSet
•       All collection framework dataStructures are Serializable objects by default.
•       RowSet object means it is the object of a class that implements javax.sql.RowSet interface
•       RowSets are introduced to JDBC from JDBC 2.0
TYPES OF ROWSETS
•       There are 3 types of RowSets
•       1. Cached RowSet
•       Disconnected Rowset, like insensitive ResultSet.
•       2.JDBC Rowset: like sensitive ResultSet
•       3.Web Rowset: connected RowSet->like Sensitive ResultSet, userful to store xml data collected from the table
•       Cached RowSet, JDBC RowSet can not deal with xml data as column values. But web RowSet can do this work.
•       The oracle thin driver, OCI driver is giving support for two types of RowSets. (catched RowSet, JDBC RowSet).
•       The oracle corporation supplies two classes having the functionality of cached, JDBC RowSet.
•       They are :
•       1. oracle.jdbc.RowSet.OracleCachedRowSet.
•       2.
•       These two classes are there oracle-home\ora92\jdbc\lib\ocrs12.jar file
•       So we need to add it in classpath.
•       If one jar file classes are using the classes of other jar files then other jar files are called dependent jar files to main jar file.
•       When java application is using classes of main jar file and these classes are having dependent classes in other jar files, then we need to execute both main and dependent jar files in the classpath.
•       Ojdbc14.jar file is not having dependent jar files. But this ojdbc14.jar file is the dependent jar file for ocrs12.jar file.
•       In order to work with Oracle corporation supplied RowSets, we need to add ocrs12.jar file, ojdbc14.jar files in claspath environment variable.
Advantages of RowSets
•       1. RowSet  object are Serializable objects. So we can send them directly over the network.
•       2. we can perform select operations on the table by simply working with setXXX(), getXXX() methods and there is no need of working with the regular JDBC objects like Connection, ResultSet etc.
Limitations of RowSets
•       1. RowSets are given only for select operations. So non-select operations are not possible.
RowSet example program
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.rowset.JdbcRowSet;

import com.sun.rowset.JdbcRowSetImpl;

public class JavaJDBCRowSetExample {

    public static void main(String[] args) throws SQLException {

        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:swing");
            String sql = "select * from emp";
            Statement ps = con.createStatement(1004, 1008);
            ResultSet rs = ps.executeQuery(sql);           
            JdbcRowSet jdbcRs = new JdbcRowSetImpl(rs);
            jdbcRs.afterLast();
            System.out.println("Records before deleting of row");
            System.out.println("id \t fName \t lName \t address");
            while (jdbcRs.previous())
            {
                int id = jdbcRs.getInt("id");
                String fName = jdbcRs.getString("fName");
                String lName = jdbcRs.getString("lName");
                String address = jdbcRs.getString("address");              
                System.out.println(id+" \t "+fName+" \t "+lName+" \t "+address);           
            }
            boolean bol = jdbcRs.absolute(3);              
            int i = jdbcRs.getRow();
            System.out.println("Deleting row no. "+i+".....");
            jdbcRs.deleteRow();
            jdbcRs.refreshRow();
            System.out.println("row no. "+i+" is deleted");
            jdbcRs.afterLast();
            System.out.println("Records after deleting of row");
            System.out.println("id \t fName \t lName \t address");
            while (jdbcRs.previous())
            {
                int id = jdbcRs.getInt("id");
                String fName = jdbcRs.getString("fName");
                String lName = jdbcRs.getString("lName");
                String address = jdbcRs.getString("address");              
                System.out.println(id+" \t "+fName+" \t "+lName+" \t "+address);           
            }
            rs.close();
            ps.close();
        }
        catch(Exception ex)
        {
            System.out.println(ex);
        }
    }
}

Wednesday, 7 August 2013

How to start Oracle10g

How to start Oracle10g?
steps:

start->programs->Oracle Database 10g Express Edition -> Run SQL command


  • It opens a window at DOS prompt where SQL> prompt appears. 
  • type connect and, it asks to enter user-name and password.



What is Database?

What is Database?
A Database is a repository of data. We can store data permanently in a database and retrieve it later whenever needed by using query commands.

List of Databases:
  1. Oracle
  2. Sybase
  3. MySQL
  4. SQL Server, and so..on


  • Data is stored in the form of tables in the database.
  • to get the data from the tables and handover to the users, we have to write programs. This programs are known as 'database client' program.
Database servers