Friday, 13 March 2015

Java simple mysql program

import java.sql.*;
class MysqlConPro{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
 
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
 
Statement stmt=con.createStatement();
 
ResultSet rs=stmt.executeQuery("select * from EMP");
 
while(rs.next())
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getDouble(3));
 
con.close();
  }
catch(Exception e){ System.out.println(e);}
 
}
}


Count factorial using multiple theading

class CountFact implements Runnable{

public void run(){

    try{

        for(int i=1;i<=10;i++)
        {
            int factValue=1;
            Thread.sleep(500);
   
            for(int n=1;n<=i;n++)
            factValue*=n;
       
        System.out.println("factorial of:"+i+"is"+factValue);
        }
    }


    catch(InterruptedException e)
    {

    System.out.println(e);
    }

}

}

class ThreadDemo1
{

    public static void main(String[] args)
    {


        Thread4 t1=new Thread4();
        Thread t3=new Thread(t1);
        t3.start();
    }

}