Archive for the ‘Java’ Category

Open a File in Runtime in Java

Open a File in Runtime in Java:

1.Create a Object For Process

2.exec is a method to open a File in Runtime

3.In the exec Method, we can give all the command which can be give in the Run Window

Process process=Runtime.getRuntime().exec(“notepad d:\demo.txt”);

4.If it cannot Work,Copy Exe File and Paste it in C:\Windows\System32

Popularity: 1% [?]

Conditional Tags in JSP

The if tag allows the conditional execution of its body according to the value of the test attribute

<c:if test=”${not empty details.add}”>
It is Not a Empty ArrayList
</c:if>

<c:if test=”${empty details.add}”>
It is a Empty ArrayList
</c:if>

The choose tag performs conditional block execution by the embedded when subtags. It renders the body of the first when tag whose test condition evaluates to true. If none of the test conditions of nested when tags evaluates to true, then the body of an otherwise tag is evaluated, if present.

<c:choose>
<c:when test=”${bank.account== ‘saving’}”>
Saving Account
</c:when>
<c:when test=”${bank.account == ‘RD’}”>
RD Account
</c:when>
<c:when test=”${bank.account == ‘salary’}”>
Salary Account
</c:when>
<c:otherwise>

</c:otherwise>
</c:choose>

Popularity: 1% [?]

Java with Sql Server

How to Connect Java with Sql Server :

1.import sql package

2.Give all the Supporting class in class.forName

3.Give Ipaddress or Computer Name in dburl or in getConnection

4.Use 1433 port Number as Default Port.

5.Give Username and Password in getConnection

6.Open the Connection using createStatement

7.Execute the query String using executeQuery Statement

8.Fetch the Record one by one Using ResultSet

9.Close the Connection

import java.io.*;
import java.sql.*;
public class sqlserverConnection
{
public static String dburl=”jdbc:mysql://localhost:1433”;
public static String username=”admin”;
public static String pwd=”pass”;
public static Connection con=null;
public static Statement stmt=null;
public static ResultSet rs=null;

public static void main(String[] args)
{
try {

Class.forName( “com.microsoft.jdbc.sqlserver.SQLServerDriver” );
con=DriverManager.getConnection(dburl,username,pwd);
stmt=con.createStatement();
rs=stmt.executeQuery(”SELECT * FROM demo”);
while(rs.next())
{
String name=rs.getString(1);
}
}
catch(SQLException sqlExcp)
{
sqlExcp.printStackTrace();
}
}
}

Popularity: 1% [?]

Java with Mysql

How to Connect Java with Mysql:

1.import sql package

2.Give all the Supporting class in class.forName

3.Give Ipaddress or Computer Name in dburl or in getConnection

4.Give Username and Password in getConnection

5.Open the Connection using createStatement

6.Execute the query String using executeQuery Statement

7.Fetch the Record one by one Using ResultSet

8..mysqldemo is a schema name

9.Close the Connection

import java.io.*;
import java.sql.*;
public class MysqlConnection
{
public static String dburl=”jdbc:mysql://localhost/mysqldemo”;
public static String username=”root”;
public static String pwd=”root”;
public static Connection con=null;
public static Statement stmt=null;
public static ResultSet rs=null;

public static void main(String[] args)
{
try {

Class.forName(”org.gjt.mm.mysql.Driver”);
(or)
Class.forName(”com.mysql.jdbc.Driver”);//use this

con=DriverManager.getConnection(dburl,username,pwd);
stmt=con.createStatement();
rs=stmt.executeQuery(”SELECT * FROM demo”);
while(rs.next())
{
String name=rs.getString(1);
}
}
catch(SQLException sqlExcp)
{
sqlExcp.printStackTrace();
}
}
}

Popularity: 1% [?]

Taglibs in JSP

Different Taglibs and its Usuage in JSP:

1.Core:http://java.sun.com/jsp/jstl/core
This Taglibs is used for Looping,Variable Support,Flow control,URL Management and miscellaneous

<%@taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>

2.XML:http://java.sun.com/jsp/jstl/xml
This Taglibs is Used for Core,Flow Control and Transformation

<%@taglib uri=”http://java.sun.com/jsp/jstl/xml” prefix=”xl”%>

3.Internationalization:http://java.sun.com/jsp/jstl/fmt
This Taglibs is Used For Locale, Message formatting and Number & date formatting

<%@taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt”%>

4.SQL:http://java.sun.com/jsp/jstl/sql
This Taglibs is Used For Sql Query Purpose

<%@ taglib uri=”http://java.sun.com/jsp/jstl/sql” prefix=”sql”%>

5.Functions:http://java.sun.com/jsp/jstl/functions
This Taglibs is Used For Collection length and String manipulation

<%@ taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn”%>

Popularity: 2% [?]

Accessing Private Variable

Reflection in java:
Reflection is the mechanism by which Java exposes the features of a class during runtime, allowing Java programs to enumerate and access a class’ methods, fields, and constructors as objects. In other words, there are object based mirrors which reflect the Java object model, and you can use these objects to access an object’s features using runtime API constructs instead of compile time language constructs.

Each object instance has a getClass() method, inherited from java.lang.Object, which returns an object with the runtime representation of that object’s class; this object is an instance of the java.lang.Class.This object in turn has methods which return the fields, methods, constructors, superclass, and other properties of that class.

You can use these reflection objects to access fields, invoke methods, or instantiate instances, all without having compile time dependencies on those features. The Java runtime provides the corresponding classes for reflection. Most of the Java classes which support reflection are in the java.lang.reflect package.

Accessing Private Features with Reflection:
All features of a class can be obtained via Reflection, including Access to private methods & variables.Setting the Accessible flag in a reflected object of java.lang.reflect.AccessibleObject class allows us to read/modify a private variable that we normally wouldn’t have permission to. However, the access check can only be supressed if approved by installed SecurityManager.
The Classes java.lang.reflect.Field, java.lang.reflect.Method and java.lang.reflect.Constructor can inherit this behavior from java.lang.reflect.AccessibleObject class. The following example demonstrates read/modify access to a private variable which is normally inaccessible.

SimpleKeyPair.java

package com;

public class SimpleKeyPair {

private String privateKey = “Welcome SimpleKeyPair “; // private field

}

PrivateMemberAccessTest.java

package com;

import java.lang.reflect.Field;

public class PrivateMemberAccessTest {

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

SimpleKeyPair keyPair = new SimpleKeyPair();

Class c = keyPair.getClass();

// get the reflected object

Field field = c.getDeclaredField(”privateKey”);

// set accessible true

field.setAccessible(true);

System.out.println(”Value of privateKey: ” + field.get(keyPair));

/* prints “Welcome SimpleKeyPair */

// modify the member varaible

field.set(keyPair, “Welcome PrivateMemberAccessTest”);

System.out.println(”Value of privateKey: ” + field.get(keyPair));

/* prints “PrivateMemberAccessTest” */

}

}

Output

=====

Value of privateKey: Welcome SimpleKeyPair

Value of privateKey: PrivateMemberAccessTest

Popularity: 11% [?]

Compare two ArrayList in JSP

Compare two ArrayList:

1.ArrayList are ListOfData and SecondList

2.Include this tag in the JSP Page

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

3.Find the Length of the ArrayList Using fn:length tag

4.Compare the ArrayList as

<c:if test=”${not empty ListOfData}” >
<c:forEach var=”j” begin=”0″ end=”${fn:length(SecondList)-1}”>
<c:forEach var=”i” begin=”0″ end=”${fn:length(ListOfData)-1}”>

<tr>
<c:if test=”${SecondList[j].empid==ListOfData[i].empid}”>
<td>${ListOfData[i].empid} </td>
<td>${ListOfData[i].empname} </td>
<td>${ListOfData[i].empdest} </td>
</c:if>
</tr>
</c:forEach>
</c:forEach>
</c:if>

Popularity: 2% [?]

what is Spring Framework?

What is Spring Framework?

Spring is grate framework for development of Enterprise grade application. Spring is a light-weight framework for the development of enterprise-ready applications. Spring can be used to configure declarative transaction management, remote access to your logic using RMI or web services, mailing facilities and various options in persisting your data to a database.Spring framework can be used in modular fashion, it allows to use in parts and leave the other components which is not required by the application.

Feature of Spring Framework

Popularity: 1% [?]

Spring Framework

  • Transaction Management: Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. Spring’s transaction support is not tied to J2EE environments and it can be also used in container less environments.
  • JDBC Exception Handling: The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling strategy.
  • Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS.
  • AOP Framework: Spring is best AOP framework
  • MVC Framework: Spring comes with MVC web application framework, built on core Spring functionality. This framework is highly configurable via strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI. But other frameworks can be easily used instead of Spring MVC Framework.

what is Spring Framework?

Popularity: 1% [?]

how to find a string within a string

Find a String within a String:
Search a String “a Nice” is Exist or Not within a String “Have a Nice Day” in JAVA

Coding:

String s = “Have a Nice Day”;
if(s.indexOf(“a Nice”)!=-1)
System.out.println(“string exist==>”);
else
System.out.println(“string Not exist==>”);

Popularity: 1% [?]

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars