Monday, November 3, 2014

Java queries Pro



What is single member annotation and how to process it

package com.java2novice.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
Retention(RetentionPolicy.Runtime)
interface MyAnnotn
{
    String value();
}
public class MySingleMembAnnotation
{
   MyAnnotn("java2novice.com")
    public void myAnnotationTestMethod()
   {
    try
    {
            Class MySingleMembAnnotation> cls = this.getClass();
            Method mth = cls.getMethod("myAnnotationTestMethod");
            MyAnnotn myAnno = mth.getAnnotation(MyAnnotn.class);
            System.out.println("value: "+myAnno.value());
        }
         catch (SecurityException e)
         {
            e.printStackTrace();
         }
         catch (NoSuchMethodException e)
         {
                     e.printStackTrace();
        }
    }
     
    public static void main(String a[])
    {
        MySingleMembAnnotation mat = new MySingleMembAnnotation();
        mat.myAnnotationTestMethod();
    }
}

How to create JDBC Connection

package com.java2novice.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcConnection
{
public static void main(String a[])
{
       try
      {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.
                getConnection("jdbc:oracle:thin:::"
                    ,"user","password");
            Statement stmt = con.createStatement();
            System.out.println("Created DB Connection....");
        }
         catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
         catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}

How to execute and read select queries using JDBC

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 MyResultSetEx
{
public static void main(String a[])
{
    try
   {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager
           .getConnection("jdbc:oracle:thin:::","user","password");
            Statement stmt = con.createStatement();
            System.out.println("Created DB Connection....");
            ResultSet rs = stmt.executeQuery("select name, salary from emp");
            while(rs.next())
            {
                System.out.println(rs.getString("name"));
                System.out.println(rs.getInt("salary"));
            }
            rs.close();
            con.close();
        }
         catch (ClassNotFoundException e)
         {
          e.printStackTrace();
          }
          catch (SQLException e)
          {
                      e.printStackTrace();
           }
    }
}

How to update a record in the database using JDBC

package com.java2novice.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class MyQueryUpdate
{
 public static void main(String[] args)
{
        Connection con = null;
        Statement stmt = null;
        try
      {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.
        getConnection("jdbc:oracle:thin:::"
         ,"user","password");
            stmt = con.createStatement();
            String query = "update table emp set salary=2000 where empid=200";
             int count = stmt.executeUpdate(query);
            System.out.println("Updated queries: "+count);
        }
         catch (ClassNotFoundException e)
         {
          e.printStackTrace();
         }
         catch (SQLException e)
         {
                     e.printStackTrace();
        }  
         finally
         {
            try
             {
                if(stmt != null) stmt.close();
                if(con != null) con.close();
            }
            catch(Exception ex){}
        }
    }
}

How to execute any type of query in JDBC

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 MyExecuteMethod
{
public static void main(String a[])
{
        Connection con = null;
        try
      {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.
                    getConnection("jdbc:oracle:thin:@::"
                        ,"user","password");
            Statement stmt = con.createStatement();
            String query = "select * from emp";
            boolean status = stmt.execute(query);
            if(status)
            {
                ResultSet rs = stmt.getResultSet();
                while(rs.next())
               {
                    System.out.println(rs.getString(1));
                }
                rs.close();
            }
            else
         {
                int count = stmt.getUpdateCount();
                System.out.println("Total records updated: "+count);
            }
        }
         catch (ClassNotFoundException e)
         {
          e.printStackTrace();
          }
          catch (SQLException e)
          {
            e.printStackTrace();
        }
         finally
         {
            try
            {
                if(con != null) con.close();
            }
            catch (Exception ex){}
        }
    }
}

No comments:

Post a Comment