Monday, 7 October 2013

Read File Using FileReader,BufferedReader

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

public static void main(String[] args) {

BufferedReader br = null;

try {

String s;
br = new BufferedReader(new FileReader("d:/ram.txt"));

while ((s = br.readLine()) != null) {
System.out.println(s);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}

Read File Using Java 1.7 Files Class


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;

//Files class is available from 1.7
public class FilesClassExample {

public static void main(String args[]){
Path path = Paths.get("d:/ram.txt");
        
        try {
               /*
                //Read file to byte array
byte[] bytes = Files.readAllBytes(path);

String val=new String(bytes);
System.out.println(val);

*/

//Read file to String list
               List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
      
      Iterator<String> i=allLines.iterator();
      while(i.hasNext()) {
          System.out.println(i.next());
      }
      
} catch (IOException e) {
e.printStackTrace();
}
}
}

Read File Contents Using Scanner



import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class UsingScanner {

public static void main(String[] args) throws FileNotFoundException {
Scanner s=new Scanner(new File("d:/ram.txt"));
while (s.hasNextLine()) {
                           String line = s.nextLine();
                           System.out.println(line);
                }
s.close();
}

}

Thursday, 3 October 2013

JQuery+Ajax+Json+Servlet+Maven Example


Explanation:

When we click the button , request will come to JQuery $.ajax method,and then execuete ServletClass ,ServletClass returns Json data as output,this output will display inside div.

Here JSONObject json = new JSONObject(); is used to create JSON data.

Maven pom.xml
<dependencies>
        <dependency>
              <groupId>org.json</groupId>
              <artifactId>json</artifactId>
              <version>20090211</version>
 </dependency>       
</dependencies>


Welcome.html

<html>
<head>
       <script src="jquery.js"></script>
       <script>
              $(document).ready(function() {
                    
                           $("#SubmitButton").click(function() {
                                 
                                   $.ajax({ 
                                  url:'sc'
                                  type:'GET'
                                  dataType: 'json'
                                  success: function(data) { 
                                                              $("#main").html("<p>Name:"+data.name+",Company:"+data.company+"</p>");
                                  },
                                                error: function(e) {
                                                       alert(e.status);
                                                }
                              });
                           });
              });
       </script>
</head>
<body>
<div id="main">
       <button id="SubmitButton">CallServlet</button>
</div>
</body>
</html>

ServletClass.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

public class ServletClass extends HttpServlet {
      
       @Override
       public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
             
              PrintWriter out=response.getWriter();
             
              JSONObject json = new JSONObject();
             
              try {
                     json.put("name","java");
                     json.put("company","oracle");
              } catch (JSONException e) {
                     e.printStackTrace();
              }
             
              out.print(json);
       }

}


Web.xml

<web-app>

<servlet>
       <servlet-name>sc</servlet-name>
       <servlet-class>ServletClass</servlet-class>
       <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
       <servlet-name>sc</servlet-name>
       <url-pattern>/sc</url-pattern>
</servlet-mapping>

</web-app>







Saturday, 28 September 2013

How to Disable Anchor tag after clik the link Using JQuery

<html>
<head>
<style>
.myDisableClass {
pointer-events: none;
cursor: default;
text-decoration: none;
}
</style>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#firstAnchor").click(function(e) {
$(this).addClass("myDisableClass");
});
});
</script>
</head>
<body>
<a id="firstAnchor" href="https://www.google.com" target="#">Click Me</a>
</body>
</html>

Friday, 27 September 2013

JDBC CURD Operations using PreparedStatement


//Insert Record into table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="insert into emp values(?,?)";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,25);
pstmt.setString(2,"jamesGosling");
pstmt.executeUpdate();

System.out.println("+++Record inserted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}


//Update Record/Records in the table using PreparesStatement in JDBC


package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="update emp set name=? where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setString(1,"java");
pstmt.setInt(2,25);

pstmt.executeUpdate();

System.out.println("+++Record updated successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}


//Delete Record/Records in the table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="delete from emp where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,25);

int count=pstmt.executeUpdate();

System.out.println("+++"+count+" Records deleted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}



//Retrieve Record/Records in the table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="select * from emp where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,26);

ResultSet rs=pstmt.executeQuery();

while(rs.next()) {
System.out.print("No:"+rs.getInt("no"));
System.out.println(" Name:"+rs.getString("name"));
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}

JDBC CURD operations using Statement object


//Create table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "create table emp(no int,name varchar(15))";

stmt.executeUpdate(query);
System.out.println("+++Table is created successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Insert Record into table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "insert into emp values(25,'jamesGosling')";

stmt.executeUpdate(query);
System.out.println("+++Record inserted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Update Record/Records in the table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "update emp set name='james' where no=25";

stmt.executeUpdate(query);
System.out.println("+++Record updated successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Delete Record/Records in the table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "delete from emp where no=25";

int count=stmt.executeUpdate(query);
System.out.println("+++"+count+" Records deleted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Retrieve Record/Records in the table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "select * from emp";

ResultSet rs=stmt.executeQuery(query);

while(rs.next()) {
System.out.print("No:"+rs.getInt("no"));
System.out.println(" Name:"+rs.getString("name"));
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}