Tuesday, January 31, 2012

newDatabaseConnection


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javadatabase;

/**
 *
 * @author shahbaz
 */
import java.sql.*;
class Conn {
  public static void main (String[] args) throws SQLException
  {
      try{
   Class.forName ("oracle.jdbc.OracleDriver");//Class.forName is used to load a class from anywhere to the program
      }catch(ClassNotFoundException e){System.out.println("andu");}

   Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "25121987");
                        // @//machineName:port/SID,   userid,  password
   try {
     Statement stmt = conn.createStatement();
     try {
       
       ResultSet rset=stmt.executeQuery("insert into MyTable values('raja',25)");
               rset = stmt.executeQuery("select * from MyTable");
       try {
         while (rset.next()){
           System.out.println (rset.getString(1)+"\t"+rset.getString(2));
         //System.out.println (rset.getString(2));// Print col 1
       } }
       finally {
          try { rset.close(); } catch (Exception ignore) {}
       }
     }
     finally {
       try { stmt.close(); } catch (Exception ignore) {}
     }
   }
   finally {
     try { conn.close(); } catch (Exception ignore) {}
   }
  }
}
/**********nex***************/
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javadatabase;

/**
 *
 * @author shahbaz
 */
import java.sql.*;
public class Conn1 {
    public static void main(String[] args) throws SQLException
    {
       /* Connection con=null;
                Statement s=null;
                        ResultSet rs=null;*/
        try{
            Class.forName("oracle.jdbc.OracleDriver");
        
       Connection  con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "25121987");
        Statement s=con.createStatement();
         s.addBatch("INSERT INTO MyTable values('Rajan',30,'M')");
         
         s.addBatch("INSERT INTO MyTable values('Rajanan',31,'M')");
         //s.addBatch("SELECT * FROM MyTable");//invalid batch command
         s.executeBatch();
       /* while(rs.next())
        {
            System.out.println(rs.getString(1)); 
        }*/
    }
        catch(Exception e)
        {
            System.out.println(e);
        }
       /* finally
        {
            try{
            rs.close();
            con.close();
            s.close();
        }
            catch(Exception e)
            {
                System.out.println(e);
            }
    }*/
    }}

No comments:

Post a Comment

ec2-user@ec2 Permission denied