Monday, 25 May 2015

A PHP to read a text file and display it as HTML

This PHP program is to read a text file and display in HTML


This is my text file i have saved it in the same folder where my php program is. you can save it at any place but you have to give the complete path for the file





SOURCE CODE :

<!DOCTYPE html>
<html>
<body>

<?php
//vivek.txt is my file name replace it with your file name
$myfile = fopen("vivek.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
   echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

</body>
</html>

OUTPUT :










Saturday, 23 May 2015

A simple java Servlet program

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Demo extends HttpServlet
{
public void doGet(HttpServletResponse res, HttpServletRequest req)throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println(" Welcome ");
}
}

java applet program to create database connection using JDBC


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/*
<applet code="JtextFieldDemo3" width=300 height=200>
</applet>
*/
public class JtextFieldDemo3 extends JApplet
{
JLabel jlb1,jlb2,jlb3,jlb4,jlb5;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5;
JButton jb;
String text1,text2,text3,text4,text5;
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable(){
public void run()
{

                    makeGUI();
                }
        });
    }catch(Exception e)
{ System.out.println(e);
}
   }
   private void makeGUI()
   {
       setLayout(new FlowLayout());
   jlb1=new JLabel("regester_no");
       jtf1=new JTextField(20);
   jlb2=new JLabel("student name");
       jtf2=new JTextField(20);
   jlb3=new JLabel("father name");
       jtf3=new JTextField(20);
   jlb4=new JLabel("mother name");
       jtf4=new JTextField(20);
   jlb5=new JLabel("address");
       jtf5=new JTextField(20);
     

       jb=new JButton("click");
 
  add(jlb1);
  add(jtf1);
  add(jlb2);
  add(jtf2);
  add(jlb3);
  add(jtf3);
  add(jlb4);
  add(jtf4);
  add(jlb5);
  add(jtf5);
  add(jb);
         

  jb.addActionListener(new ActionListener()
  {
    public void actionPerformed(ActionEvent ae)
             {
   text1=jtf1.getText();
text2=jtf2.getText();
   text3=jtf3.getText();
text4=jtf4.getText();
   text5=jtf5.getText();
                showStatus(text1);
                      try
            {
           
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con = DriverManager.getConnection("jdbc:odbc:student_DSN");
                Statement st=con.createStatement();
                st.executeUpdate("insert into student_tb values('" + text1 + "','" + text2 + "','" + text3 + "','" + text4 + "','" + text5 + "')");
               
                con.commit();
                  showStatus("data inserted");
             
            }catch(Exception e)
            {
                System.out.println(e);
            }

           }
  });
   }
}

JAVA database connection to oracle ( Oracle JDBC connection )

import java.io.*;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class OracleJDBC {

public static void main(String[] argv) {

System.out.println("-------- Oracle JDBC Connection Testing ------");

try {

Class.forName("oracle.jdbc.driver.OracleDriver" );

} catch (ClassNotFoundException e) {

System.out.println("Where is your Oracle JDBC Driver?" +e);
e.printStackTrace();
return;

}

System.out.println("Oracle JDBC Driver Registered!");

Connection connection = null;

try {

connection = DriverManager.getConnection("jdbc:oracle:thin:@//127.0.0.1:8080/XE", "system", "gcc");


} catch (SQLException e) {

System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;

}

if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}

}

PHP database connection ,insertion & extraction of data into and from database

There are 5 simple steps we should follow while dealing with PHP database connection
1)create connection to database & test connection
2)perform database query  & test for query errors
3)use returned data(if any)
4)release returned data
5)close database connection

example :
<?php
//1.create connnection to database
$dbhost="localhost";
$dbuser="root";
$dbpass="";
$dbname="mydb";
$connection=mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
//test connnection
if(mysqli_connect_errno())
{
die("database connection failed: " . mysqli_connect_error().
" (".mysqli_connect_errno(). ")"
);
}

?>
<?php
//2.perform database query
$query ="SELECT * ";
$query .="FROM subjects ";
$result=mysqli_query($connection,$query);
//test for query errors
if(!$result)
{
die("database query failed");
}
?>
<html>
<head>
<title>database connection</title>
</head>
<body>
<table border="1">
<tr><th>sub1</th><th>sub2</th></tr>
<?php
//3.use returned data(if any)

while($row=mysqli_fetch_assoc($result))
{
?>
<tr><td><?php echo $row["sub1"] ; ?></td><td><?php echo $row["sub2"] ;?></td></tr>

<?php
}
?>
</table>
<?php
//4. release returned data
mysqli_free_result($result);
?>
</body>
</html>

<?php
//5.close database connection
mysqli_close($connection);
?>

A PHP program to create database connection & check the connection

<?php
$servername = "localhost";
$username = 'root';        // your database system username
$password = 'pas123';  //your database system password

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Thursday, 21 May 2015

A SQL database design to simulate the followings #DBMS1

#PROBLEM STATEMENT
Notown Records has decided to store information about musicians who perform on its albums. Each musician that records at Notown has an SSN, a name, an address, and a phone number. No musician has more than one phone. Each instrument used in songs recorded at Notown has a unique identification number, a name (e.g., guitar, synthesizer, flute) and a musical key (e.g., C, B-flat, E-flat). Each album recorded on the Notown label has a unique identification number, a title, a copyright date and a format (e.g., CD or MC). Each song recorded at Notown has a title and an author. Each musician may play several instruments, and a given instrument may be played by several musicians. Each album has a number of songs on it, but no song may appear on more than one album. Each song is performed by one or more musicians, and a musician may perform a number of songs. Each album has exactly one musician who acts as its producer. A musician may produce several albums. Queries
a) List musician name, title of the song which he has played, the album in which song has occulted.

b) List the details of songs which are performed by more than 3 musicians.
c) List the different instruments played by the musicians and the average number of musicians who play the instrument.
d) Retrieve album title produced by the producer who plays guitar as well as flute and has produced no of songs greater than the average songs produced by all producers.

e) List the details of musicians who can play all the instruments present.

...........................CREATE TABLES.............................
create table projects
(
            pno                  number,
            sponsor                        varchar(15),
            st_date                        number,
            end_date         number,
            budget             number,
            primary key(pno)
);


create table professor
(
            prof_id                        number                        primary key,
            name                varchar(15),
            age                   number(5),
            rank                 varchar(10),
            speciality         varchar(15),
            pno      number            references        projects
);

create table students
(
            usn       varchar(5),
            name    varchar(15),
            program           varchar(15),
            primary key(usn)
);

create table stud_workson_proj
(
            prof_id            number            references        professor,
            pno      number            references        projects,
            usn       varchar(20)      references        students
);

create table prof_workson_proj
(
            prof_id            number            references        professor,
            pno      number            references        projects
);

...............................INSERT INTO TABLE.........................

insert into projects values('101','UGC','2006','2028','70000');
insert into projects values('102','UG','1992','2026','60000');
insert into projects values('103','UG','2003','2024','50000');
insert into projects values('104','COLL','2008','2022','000');
insert into projects values('105','UFC','2010','2020','30000');
insert into projects values('106','ULC','2001','2018','20000');
insert into projects values('107','UML','2000','2016','65000');
insert into projects values('108','FLV','1999','2014','75000');
insert into projects values('109','DOC','2006','2012','78000');
insert into projects values('110','UGC','2004','2010','85000');


insert into professor values('201','Abhishek','20','1200','database mgt','101');
insert into professor values('202','Hrishikesh','21','1201','database','102');
insert into professor values('203','Tejraj','22','1202','D.S','101');
insert into professor values('204','Kokitkar','23','1203','Unix','103');
insert into professor values('205','Kotrappa','24','1204','mysql','104');
insert into professor values('206','Govind','25','1205','Clang','105');
insert into professor values('207','Swaroopa','26','1206','cpp','106');
insert into professor values('208','Venogopal','27','1207','java','102');
insert into professor values('209','Mithari','28','1208','O.S','105');
insert into professor values('210','Manoj','29','1209','system s/w','103');
insert into professor values('211','Poonam','30','1300','visual basic','101');
insert into professor values('212','Amol','31','1301','fco','106');
insert into professor values('213','Raghvendra','32','1302','DMS','108');
insert into professor values('214','Narendra','33','1303','WEB','108');
insert into professor values('215','Sonia','34','1304','computer n/w','105');



insert into students values('s01','Abhijeet','MCA');
insert into students values('s02','Abid','BCA');
insert into students values('s03','Akshata','BE');
insert into students values('s04','Ankita','MCA');
insert into students values('s05','Apurva','MCA');
insert into students values('s06','Anoop','MSC');
insert into students values('s07','Aparna','MS');
insert into students values('s08','Adavesh','MCA');
insert into students values('s10','Rajnikanth','BBA');
insert into students values('s11','Amit','MBA');
insert into students values('s12','Soumya','MCA');
insert into students values('s13','Amita','MCA');
insert into students values('s14','Apeksha','MCA');
insert into students values('s15','Akshay','BE');
insert into students values('s16','Chetana','BE');
insert into students values('s17','Maya','MCA');
insert into students values('s18','dhanraj','ARCH');
insert into students values('s19','Anant','MCA');
insert into students values('s20','kiran','BE');



insert into stud_workson_proj values('201','101','s01');
insert into stud_workson_proj values('201','102','s02');
insert into stud_workson_proj values('202','103','s03');
insert into stud_workson_proj values('203','101','s01');
insert into stud_workson_proj values('204','104','s01');
insert into stud_workson_proj values('205','103','s03');
insert into stud_workson_proj values('205','105','s04');
insert into stud_workson_proj values('206','104','s05');
insert into stud_workson_proj values('207','101','s06');
insert into stud_workson_proj values('209','102','s04');
insert into stud_workson_proj values('201','105','s03');
insert into stud_workson_proj values('201','106','s05');
insert into stud_workson_proj values('202','107','s07');
insert into stud_workson_proj values('203','103','s05');
insert into stud_workson_proj values('204','103','s05');
insert into stud_workson_proj values('206','105','s04');
insert into stud_workson_proj values('205','105','s06');
insert into stud_workson_proj values('207','101','s08');
insert into stud_workson_proj values('208','107','s14');
insert into stud_workson_proj values('208','103','s11');
insert into stud_workson_proj values('201','107','s11');
insert into stud_workson_proj values('201','103','s11');
insert into stud_workson_proj values('201','104','s11');
insert into stud_workson_proj values('201','108','s11');
insert into stud_workson_proj values('201','110','s11');




insert into prof_workson_proj values('201','101');
insert into prof_workson_proj values('202','101');
insert into prof_workson_proj values('203','101');
insert into prof_workson_proj values('204','102');
insert into prof_workson_proj values('205','103');
insert into prof_workson_proj values('206','103');
insert into prof_workson_proj values('207','102');
insert into prof_workson_proj values('208','108');
insert into prof_workson_proj values('209','108');
insert into prof_workson_proj values('210','102');
insert into prof_workson_proj values('211','104');
insert into prof_workson_proj values('212','101');
insert into prof_workson_proj values('215','106');
insert into prof_workson_proj values('214','106');
insert into prof_workson_proj values('215','108');
insert into prof_workson_proj values('204','108');
insert into prof_workson_proj values('205','104');
insert into prof_workson_proj values('206','108');
insert into prof_workson_proj values('203','109');
insert into prof_workson_proj values('206','110');
insert into prof_workson_proj values('207','101');
insert into prof_workson_proj values('201','109');
insert into prof_workson_proj values('201','102');
insert into prof_workson_proj values('201','103');
insert into prof_workson_proj values('201','104');
insert into prof_workson_proj values('201','105');
insert into prof_workson_proj values('201','106');
insert into prof_workson_proj values('201','107');
insert into prof_workson_proj values('201','108');
insert into prof_workson_proj values('201','110');


insert into prof_workson_proj values('202','102');
insert into prof_workson_proj values('202','103');
insert into prof_workson_proj values('202','104');
insert into prof_workson_proj values('202','105');
insert into prof_workson_proj values('202','106');
insert into prof_workson_proj values('202','107');
insert into prof_workson_proj values('202','108');
insert into prof_workson_proj values('202','109');
insert into prof_workson_proj values('202','110');




QUERIES:
1.
select p.name
from professor p, projects pr
where p.pno=pr.pno AND budget<100000;

NAME
--------------------
Abhishek
Hrishikesh
Tejraj
Kokitkar
Kotrappa
Govind
Swaroopa
Venogopal
Mithare
Manoj
Poonam

NAME
--------------------
Amol
Raghvendra
Narendra
Sonia

15 rows selected.


2.
 select s.name,p.name,a.sponsor
from students s, professor p, projects a, stud_workson_proj w
where s.usn=w.usn AND p.prof_id=w.prof_id AND a.pno=w.pno;

NAME                 NAME                 SPONSOR
-------------------- -------------------- --------------------
Akhilesh             Abhishek             UGC
Abid                 Abhishek             UG
Akshata              Hrishikesh           UG
Akhilesh             Tejraj               UGC
Akhilesh             Kokitkar             COLL
Akshata              Kotrappa             UG
Ankita               Kotrappa             UFC
Apurva               Govind               COLL
Anoop                Swaroopa             UGC
Ankita               Mithare              UG
Akshata              Abhishek             UFC
Apurva               Abhishek             ULC
Aparna               Hrishikesh           UML
Apurva               Tejraj               UG
Apurva               Kokitkar             UG
Ankita               Govind               UFC
Anoop                Kotrappa             UFC
Adavesh              Swaroopa             UGC
Amit                 Venogopal            UG
Apeksha              Venogopal            UML

20 rows selected.


3.
 select p.name, sum(budget)
from professor p, projects a
where p.pno=a.pno AND a.st_date>2000 AND a.end_date=2018
group by p.name;


NAME                 SUM(BUDGET)
-------------------- -----------
Amol                       20000
Swaroopa                   20000


4.
select p.name
from professor p, projects a
where p.pno=a.pno AND
BUDGET>(select avg(budget)
from projects);

NAME
--------------------
Abhishek
Hrishikesh
Tejraj
Venogopal
Poonam
Raghvendra
Narendra

7 rows selected.select *

5.
select p.name
from professor p
where p.prof_id in(select prof_id
from prof_workson_proj
group by prof_id
having count(*)=(select count(*)
from projects));


NAME
--------------------
Hrishikesh