pradeeshtet's blog

connect JSP with mysql in Linux

Let us make a simple application to connect and access mysql database from jsp code. This covers example jsp program to connect mysql, installing apache tomcat server, configuring mysql connector to establish jdbc driver, setting up the connection. 1st application using jsp.

To run a java server page in localhost, we need to setup a server.

Installing Latest Apache Tomcat server :

Run the following command in the terminal,

                         sudo apt-get install tomcat8            

Find all occurrence of character in the string

Program to find index of particular character occur throughout the string.

Index tell where the character occur.

void Findindex(char c,int n)

{

int n=0;

int result;

String str=”roses”;

while(result>0)

{

result=str.indexOf(c,n);

System.out.println(result);

n=result+1;

}

}

invoking the method

p.Findindex('s',0);

output of the above program:

2

4

-1

Know how the indexOf method is used in java.

indexOf() method in java

Java string indexOf() with example program and explanation.

indexOf(char c)

It return index of first occurrence of a character in the string.

example:


String str="tet";

int result= str.indexOf('e');

Now, the value of result will be 1.

indexOf(char c, int fromindex)

Fromindex set the index from where the particular character is to be searched.

example:

Given a string of odd length, return length three from its middle- replaceALL

java replaceAll() method with example program. This kind of question are asked in codingbat. Improve your programming skills by solving this, its for beginners. Program with explanation.

public String middleThree(String str)

{

String newstr;

int mid;

str=str.replaceAll(“\\s” , “”);

int len=str.length();

if(len==3)

{

return str;

}

mid=(len/2)-1;

newstr=str.substring(mid,mid+3);

return newstr;

}

Below program explain java replace() method. Syntax, example and explanation.

Replace particular character with other in a String JAVA

simple java program to replace particular character in a string with other.

class replaceProgram{
    public static void main(String args[])  {

String st1=”TechieTet”;
String st2=st1.replace(‘e’,’x’);
System.out.println(st2);
}
}

Output of this program will be

  Txchix Txt

 

Syntax of replace method

    replace(char oldchar, char newchar)

   replace(string oldstr, string newstr)

Not only character also a part of string i.e substring can be changed using replace.

Reverse the string Program in java

Simple program to reverse the string in java. This logic, also used to check whether the given string is palindrome or not. By comparing string after reversing it.

class reverseProgram {
    public static void main(String args[])  {

String reverse=” “;
String original=”Techie Tet”;
int len=original.length();
for(int i=len-1;i>=0;i--)
  {
reverse=reverse+original.charAt(i);
 }
System.out.println(reverse);

}
}

 

Program for Fibonacci series

Simple java program to display Fibonacci series using while loop with explanation.Its also called as fibonacci number or sequence. Know what is fibonacci number here.

class fibo {
    public static void main(String args[])  {
       int a=0,b=1,c;
       System.out.println(a);
      while(b<11) 
       {
                System.out.println(b);
  c=a+b;
a=b;
b=c;
}
}
}

Output:

0

1

1

2

3

5

8

Factorial using recursion program

Simple java program for factorial using recursion with explanation. Easy logic, can be used in C#(sharp), python, php, perl... etc.

int fact(int n)
{
int result;
if(n==1)
{
return 1;
}
result=fact(n-1)*n;
return result;
}

Understand the logic behind recursion, so you don’t feel factorial using recursion as difficult.

Input -5, Output – 120.

Explanation:

How to find factorial of number 5 using recursion.

Recursion – Function call itself.

Example 1:

Even or Odd java program

Check whether the given number is even or odd, use scanner to get input from user(Getting integer input from user). To get input from in java, Scanner class is used. Scanner class should be imported to use the funtionality(methods) of Scanner.

 

Pages