import java.io.BufferedInputStream;
import java.io.FileInputStream;
class GetOccurrences{
public static void main(String args[])
{
try{
FileInputStream fis=new FileInputStream("C:\\Users\\SAN-MCA-ME-MTECH-MS\\Documents\\SAN\\Temp.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
int i;
int occurs=0;
while((i=bis.read())!=-1)
{
char a=(char)i;
if(a==args[0].charAt(0))
{
occurs++;
}
}
System.out.println("Letter e is "+occurs+" of times repeated in the file");
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
Java programs for beginners, list of programs in java on various topics, interesting programs in java. Java programs on various topics
Saturday, March 1, 2014
Write an example that counts the number of times a particular character, such as e, appears in a file. The character can be specified at the command line.
Saturday, February 23, 2013
Java Connect to Oracle XE Database
In order to connect to oracle database you need to add ojdbc14.jar file in your eclipse project as a external jar file.... Download from web.
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class OracleConnect {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","saravanan","saravanan");
System.out.println("Connected Successfully To Oracle");
Statement stmt;
ResultSet rs;
stmt=con.createStatement();
rs=stmt.executeQuery("select * from saran");
while(rs.next())
{
System.out.println(rs.getString("empname")+"--"+rs.getInt("salary"));
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Simple java program for connecting to DATABASE(mysql)
In order to connect to database you need to add mysql-connector-java-5.1.22.jar file in your eclipse project as a external jar file....
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlSample {
/**
* @param args
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws SQLException
*/
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
Connection con;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/saran","root","root");
Statement stmt;
ResultSet rs;
stmt=con.createStatement();
rs=stmt.executeQuery("select * from employee;");
while(rs.next())
{
System.out.println(rs.getString("empname")+rs.getInt("salary"));
}
}
}
Servlet Program for Retrieving data from Table
In order to connect to database you need to add mysql-connector-java-5.1.22.jar file in your eclipse project as a external jar file....
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet
*/
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public
class HelloServlet extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html>");
out.println("<head><title>Students List</title></head>");
out.println("<body>");
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception E) {
out.println("Unable to load driver.");
E.printStackTrace();
}
out.println("<br><center><p style=color:purple;font-size:30px;>STUDENTS DATABASE</p></center><hr>");
try {
Connection Conn =
DriverManager.getConnection("jdbc:mysql://localhost/saran","root","root");
// Do something with the Connection
Statement Stmt = Conn.createStatement();
ResultSet RS = Stmt.executeQuery("SELECT * from student");
out.println("<center><table border='1' style=color:purple;font-size:20px;><th>Name</th><th>Area</th><th>Course</th>");
while (RS.next()) {
out.println("<tr><td>"+RS.getString("name")+"<td>"+RS.getString("area")+"<td>"+RS.getString("course")+"</td></tr>");
}
out.println("</table></center>");
out.println("<center><form action='index.htm'><input type='submit' value='Home'></form></center>");
// Clean up after ourselves
RS.close();
Stmt.close();
Conn.close();
}
catch (SQLException E) {
}
out.println("</body></html>");
}
}
Friday, February 22, 2013
Compare Two Integer Arrays
This program will compare two integer arrays and return a result number of matching numbers in those arrays :
public void arrayMatcher(int a[], int b[])
{
int count=0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if((a[i])==(b[j]))
{
count++;
}
}
}
System.out.println(count+" numbers matches in two arrays");
}
CONTENT WARNING:This is my own program.. It is for Educational purposes only. Please do not republish or reproduce with another name in online.
public void arrayMatcher(int a[], int b[])
{
int count=0;
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if((a[i])==(b[j]))
{
count++;
}
}
}
System.out.println(count+" numbers matches in two arrays");
}
CONTENT WARNING:This is my own program.. It is for Educational purposes only. Please do not republish or reproduce with another name in online.
Sort Letters in a String Object
public void doSorting(String str)
{
char array[]=str.toCharArray();
char temp;
System.out.print("Given name :");
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]);
}
System.out.println();
for(int i=0;i<array.length;i++)
{
for(int j=0;j<i;j++)
{
if(array[i]<array[j])
{
temp=array[j];
array[j]=array[i];
array[i]=temp;
}
}
}
CONTENT WARNING:This is my own program.. It is for Educational purposes only. Please do not republish or reproduce with another name in online.
{
char array[]=str.toCharArray();
char temp;
System.out.print("Given name :");
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]);
}
System.out.println();
for(int i=0;i<array.length;i++)
{
for(int j=0;j<i;j++)
{
if(array[i]<array[j])
{
temp=array[j];
array[j]=array[i];
array[i]=temp;
}
}
}
CONTENT WARNING:This is my own program.. It is for Educational purposes only. Please do not republish or reproduce with another name in online.
Reverse String in JAVA
This is the method which will reverse the given STRING :
public void myOwnstringReversing(String str)
{
for(int i=str.length()-1;i>=0;i--)
{
System.out.print(str.charAt(i));
}
}
CONTENT WARNING:
This is my own program.. It is for Educational purposes only. Anyone do not republish in your website will be punished offensively.
Subscribe to:
Posts (Atom)