Thursday, October 4

Append apostrophe with string or string items

This code would help in appending apostrophe with the string on both ends.
If there are comma separated items in string, each one would be appended with apostrophes.
Use: Appending apostrophe for query IN clause



public static final String appendAppsotophe(String str) {
            if(str!=null) {
                  String returnData = "";
                  String array[] = str.split(",");
                  returnData = "'" + array[0] + "'";
                  for (int i = 1; i < array.length; i++) {
                        returnData += ",'" + array[i].trim() + "'";
                  }
                  return returnData;
            } else {
                  return null;
            }
      }

Wednesday, October 3

Animate web buttons (zoom in zoom out)

Exactly not an animation. Use this javascript code to zoom in zoom out buttons on mouse hover.

--- Below is Javascript code ---



function buttonZoom(id){
      var x = Number(document.getElementById(id).width);
      for(var i=x; i<=Number(x+20); i++){
            document.getElementById(id).width = Number(i);
      }
    }
   
    function buttonZoomOut(id){
      var x = Number(document.getElementById(id).width);
      for(var i=x; i>=Number(x-20); i--){
            document.getElementById(id).width = Number(i);
      }
    }


----- Below is JSP Code ---

<embed id="button" type="image/svg+xml" src="" onclick="as();"
onmouseover="buttonZoom(this.id);" onmouseout="buttonZoomOut(this.id);" width="60"/>

Note: Specifying width in jsp embed tag is necessary

Monday, October 1

Creating / Using session : Java Web Project



// NameSpace imports

import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
/* Use openSymphony .jar for above import


//Declaration of Session
private Map session;

//Create setter getter

session = ActionContext.getContext().getSession();

//Use session.put & session.get
session.put(“Id”,userID );
String userId = (String)session.get(“Id”);

Executing Query from Java



Before this you need to connect to database, refer to this post


Connection conn = null;
try{
         DbConnectionProvider dbConnectionProvider = new DbConnectionProvider();
         conn = dbConnectionProvider.connect();
         Statement stmt=conn.createStatement();
         String query = "SELECT NAME, AGE FROM DETAILS_TABLE";
                ResultSet rs = stmt.executeQuery(query);
                while (rs.next()){
                      System.out.println(rs.getString(1));
}}

Friday, September 28

Connect Java to Oracle Database



package com.action.dbConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConnectionProvider {

      String serverName = "119.45.2.23";
      String sid = "xyz";     // String sid given by Oracle
      String username = "uid";
      String password = "pwd";
      String driverName = "oracle.jdbc.driver.OracleDriver";
      String portNumber = "2008";
      Connection connection = null;
     
      public Connection connect() {
            try {
System.out.println("Inside class DbConnectionProvider, Method connect()");
                  // Load the JDBC driver
                   Class.forName(driverName);
                  // Create a connection to the database
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
connection = DriverManager.getConnection(url, username, password);
            } catch (ClassNotFoundException e) {
                // Could not find the database driver
                  System.out.println("Could not find the database driver");
                  e.printStackTrace();
            } catch (SQLException e) {
                // Could not connect to the database
                  System.out.println("Could not connect to the database");
            } finally {
                 
            }
            return connection;
      }