Sunday, September 21, 2014

Find power of given value without using any of Math method

private static void findPowerOfValue(int findValue, int powerValue) {
        int temp = 0;
        while (findValue > powerValue) {
            temp = findValue / powerValue;
            if ((temp % powerValue) != 0) {
                System.out.println("Given value : "+findValue+" is not power of "+powerValue);
                break;
            } else {
                System.out.println("Given value : "+findValue+" is power of "+powerValue);
            }
            findValue = findValue / powerValue;
        }
    }

Integer reverse without using String reverse()

private static void reverse(int value) {
        int temp = 0;
        int temp1 = 0;
        while (value != 0) {
            // Take the values length-1
            temp = value / 10;
            // Take the last value
            temp1 = value % 10;
            // assign last value removed integer
            value = temp;
            System.out.print(temp1);
        }
    }

Friday, September 12, 2014

Swap two number without using temp variable

Code:
        int X = 5;
        int Y = 6;
        System.out.println("Before swap X is : "+X+", Y is : "+Y);
        Y = X+Y;
        X = Y-X;
        Y = Y-X;
        System.out.println("After swap X is : "+X+", Y is : "+Y);

Output: 

Before swap X is : 5, Y is : 6
After swap X is : 6, Y is : 5

Wednesday, September 10, 2014

Insert dropdown value into database. Using JSP SERVLET




Create Dynamic project: File > New > Project....> Web > Dynamic Web Project.


























Create Servlet class with name SampleTest.java(it will automatically makes entry in web.xml)
Paste the following code into doPost()
System.out.println("Selected value is : "+(String)request.getParameter("gameName"));
            String gameName = (String)request.getParameter("gameName");
            DBConnection dbConnection = new DBConnection();
            dbConnection.insertQuery(gameName);
            System.out.println("Query insert successfully");


Create DBConnection.java file


     
     


      static Connection l_objConnection;


      public static Connection getConnetion() {


            try {


                  Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
                  // Your url -jdbc:oracle:thin:@localhost:1521:DBName
                  // your username - Test
                  // your password - Test
                  l_objConnection = DriverManager.getConnection(
                              "jdbc:oracle:thin:@localhost:1521:DBName", "username",
                              "password");


            } catch (InstantiationException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (IllegalAccessException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (SQLException e) {
                  // TODO Auto-generated catch block test
                  e.printStackTrace();
            }


            return l_objConnection;
      }


      public void insertQuery(String gameName) {
            try {
                  // Replace your table name instead of TABLENAME
                  // Replace GAMENAME into colounm name.
                  PreparedStatement preStatement = l_objConnection
                              .prepareStatement("INSERT INTO TABLENAME(GAMENAME) VALUES (?)");
                  preStatement.setString(1, gameName);
                  preStatement.execute();
            } catch (SQLException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
      }





Create index.jsp under WebContent folder.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<SCRIPT LANGUAGE="JavaScript">
            <!--
            function button1()
            {
              var value =   document.getElementById("gameName").value;
              alert(value);
              document.SampleTest.submit()
            }   
            
            // -->
        </SCRIPT>
<body>
<FORM NAME="SampleTest" METHOD="POST" action="./SampleTest">
<select id="gameName" name="gameName">
<option>Sports</option>
     <option>Corporate</option>
     <option>Religious</option>
     <option>Music</option>
</select>
<INPUT TYPE="submit" VALUE="Save" ONCLICK="button1()">
</FORM>
</body>
</html>


Web.xml
<servlet>
    <description></description>
    <display-name>SampleTest</display-name>
    <servlet-name>SampleTest</servlet-name>
    <servlet-class>com.test.SampleTest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SampleTest</servlet-name>
    <url-pattern>/SampleTest</url-pattern>
  </servlet-mapping>


Output: