DDM Lab
DDM Lab
No:1
Data Definition Commands, Data Manipulation Commands for inserting, deleting, updating
and retrieving Tables and Transaction Control statements
AIM:
To create the database using Data Definition Language(DDL) Commands and to perform
Insert, Delete, Modify, Alter, Update and View the records using Data Manipulation
Language (DML) Commands and to control the tables using Transaction Control Statements.
CREATE
CREATE command is used to create tables in database.
Syntax
CREATE TABLE <table name> (column1 datatype1,…, column datatypen);
RENAME
RENAME command is used to rename the tables.
Syntax
Rename <old table name> to <new table name>;
ALTER
The structure of a table can be changed using this command. Using this command we can do the
following.
i Add a new column.
ii Change the width of a data type
iii Change the data type of a
column
While altering the columns the values of the particular column should be empty.
Syntax
ALTER TABLE <table name> MODIFY (column
datatype,…); ALTER TABLE <table name> ADD
(column datatype,…);
TRUNCATE
It is used to remove all the records in the table including the space allocated for the
table.
But the structure of the table is
retained. TRUNCATE
TABLE
<table name>;
DROP
This command is used to remove a table from the database.
Syntax
DROP TABLE <table name>;
Output
CREATE
SQL> create table employee (Employee_namevarchar(10), employee_no number(8), dept_name
varchar(10),dept_no number (5),date_of_join date);
Table created.
Table created.
DESC
SQL>desc employee1;
Name Null? Type
EMPLOYEE_NAME
VARCHAR2(10) EMPLOYEE_NO NOT NULL
NUMBER(8) DEPT_NAME
VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
RENAME
SQL> Rename employee to
employee2; Table renamed.
ALTER
SQL> alter table employee1 add ( salary number);
SQL> alter table employee1 modify ( salaryvarchar(10));
Table altered.
DROPPING PRIMARY KEY USING ALTER COMMAND
SQL> alter table employee1 drop primary
TRUNCATE
SQL> truncate table
employee1; Table
truncated.
DROP
SQL> drop table employee1;
Table dropped.
The DML commands in the SQL are INSERT, UPDATE, DELETE, SELECT.
INSERT
It is used to insert the values into the table.
Syntax
INSERT INTO <table name> VALUES (value1, value2…);
SQL> INSERT INTO employee1 VALUES(‘&Employee_name’,
’&employee_no’, ‘&dept_name’, ‘&dept_no’,’&date_of_join’);
Using this we can insert ‘N’ no. of rows.
UPDATE
The Update command is used to update (changing values in one or two columns of a row) rows
in a table. Specific rows can also be updated based on some condition.
If the WHERE clause is omitted, then the changes take place in all rows of the table.
Syntax
UPDATE <table_name> SET column1=expression,column2=expression…
WHERE
<search_condition>;
DELETE
The delete command is used to delete rows from a table.
Syntax
DELETE FROM<
Deletion of all rows
SQL>DELETE FROM <table_name>;
This causes the deletion of all rows in the table.
SELECT
The Select command is used to retrieve the stored data from a table.
Syntax
SELECT * FROM <table_name>;
OUTPUT:
SQL> create table employee (Employee_namevarchar(10), employee_no number(8), dept_name
varchar(10),dept_no number (5),date_of_join date);
Table created.
INSERT
SQL>insert into employee1 values
('Vijay',345,'CSE',21,'21-jun-2006'); 1 row
created.
UPDATE
SQL> update employee1 set employee_no=300 where
dept_no=67; 1 row updated.
DELETE
SQL> delete from employee1 where
employee_no>344; SQL> delete from employee1
where employee_no =124;
SELECT
SQL> select * from employee1;
It selects all the columns from the table.
SAVE POINT
This command is used to identify a point in a transaction in which it can be restored using
Roll back command.
SQL> savepoint <save point Name>;
Savepoint created.
ROLLBACK
It is used to restore database to original since last commit.
SQL> rollback;
Rollback
complete.
SQL> roll back <savepoint Name>
Rollback complete.
SQL> commit;
Commit complete.
SQL> rollback;
Rollback complete.
RESULT
Thus the Database creation using Data Definition Language(DDL) commands, the Insert,
Delete, Modify, Alter, Update and View the records using Data Manipulation Language (DML)
Commands and to control tables using Transaction Control statement was executed
successfully.
Ex. No 2: DatabaseQuerying – Simplequeries, Nested queries, Sub queries
and Joins
AIM:
To create a table and execute Simple queries, Nested queries, Sub queries and Joins.
DESCRIPTION:
JOIN OPERATIONS
CREATION OF TABLE
SQL>create table stud (sname varchar2(30), sid varchar2(10), sage number(10), sarea varchar2(20),
sdept varchar2(20));
Table created.
ARITHMETIC OPERATION
ashwin 201
bhavesh 202
pruthvik 203
charith 204
ashwin 19
bhavesh 18
BETWEEN OPERATOR
SQL> select sname,sarea, sid from studs where sid between 102 and 104;
SNAME SAREA SID
ashwin annanagar
bhavesh nungambakkam
pruthvik Annanagar
LOGICAL AND OPERATOR
SQL> select sname ,sid from studs where sid>102
pruthvik 103
LOGICAL OR OPERATOR
SQL> select sname ,sid from studs where sid>102 or sarea='annanagar';
SNAME SID
ashwin 101
pruthvik 103
charith 104
SNAME
SPOCKET
ashwin
750
bhavesh
500
pruthvik
250
100
AGGREGATE FUNCTIONS
studs; 400
studs; 100
4
SQL> select max(spocket) result from
studs; 750
SQL> select sum(spocket) result from
studs; 1600
SQL> select *
from dept;
DNO DNAME
LOC
10 inventory hyd
20 finance bglr
30 HR mumbai
Creating emp2 table:
values(333,'jagan','manager',111,10); 1 row
values(444,'madhu','engineer',222,40); 1 row
1 Equijoin:
A join which contains an equal to ‘=’ operator in this joins condition
Using Clause:
SQL> select eno,ename,job,dname,loc from emp2 e join dept d using(dno);
On Clause:
SQL> select eno,ename,job,dname,loc from emp2 e join dept
d on(e.dno=d.dno); ENO ENAMEJOB DNAMELOC
3 Self Join:
Joining the table itself is called self join.
SQL> select e1.eno,e2.ename,e1.job,e2.dno from emp2 e1,emp2 e2 where
e1.eno=e2.mgr;
ENO ENAME JOB DNO
4 Natural Join:
It compares all the common columns.
SQL> select eno,ename,job,dname,loc from emp2 natural
join dept; ENO ENAMEJOB
DNAMELOC
5 Cross Join:
This will give the cross product.
SQL> select eno,ename,job,dname,loc from emp2 cross
join dept; ENO ENAME JOB DNAME
LOC
SQL> select eno,ename,job,dname,loc from emp2 e right outer join dept d on(e.dno =d.dno);
(OR)
SQL> select eno,ename,job,dname,loc from emp2 e,dept d where e.dno(+)=d.dno;
RESULT
Thus the relationship between databases has been implemented using join operation.
Ex. No 3: Creation of Views, Sequences and Synonyms
AIM:
To create views, sequences and synonyms using DDL, and DML statements
DESCRIPTION:
Views
Sequences
• Oracle provides the capability to generate sequences of unique numbers, and
they are called sequences.
• Just like tables, views and synonyms, a sequence is a type of database object.
• Sequences are used to generate unique, sequential integer values that are used
as primary key values in database tables.
• The sequence of numbers can be generated in either ascending or descending order.
Synonyms
• A synonym is an alias, that is, a form of shorthand used to simplify
the task of referencing a database object.
• There are two categories of synonyms, public and private.
• A public synonym can be accessed by any system user.
• Private synonyms, on the other hand, belong to the system user that creates
them and reside in that user's schema.
• A system user can grant the privilege to use private synonyms that they own
to other system users.
• In order to create synonyms, we will need to have the CREATE SYNONYM privilege.
• This privilege will be granted to us by the DBA.
• We must have the CREATE PUBLIC SYNONYM privilege in order to
create public synonyms.
• If we own a synonym, we have the right to drop (delete) the synonym.
The DROP SYNONYM command is quite simple.
• DROP SYNONYM synonym_name;
• In order to drop a public synonym we must include the PUBLIC keyword in
the DROP SYNONYM command.
• In order to drop a public synonym, we must have the DROP
PUBLIC SYNONYM privilege.
• DROP PUBLIC SYNONYM synonym_name;
OUTPUT:
TO CREATE THE TABLE ‘FVIEWS’
SQL> create table fviews( name varchar2(20),no number(5), sal number(5), dno
number(5)); Table created.
Xxx 1 19000 11
Aaa 2 19000 12
Yyy 3 40000 13
SQL> create view ssview( cusname,id) as select name, no from fviews where
ssview;
CUSNAME
ID
aaa 2
ssview; View
dropped.
Sequences
Creation of table:
SQL> create table class(name varchar(10),id number(10));
Table created.
SQL> /
Enter value for name:
brindha Enter value for
id: 02
old 1: insert into class
values('&name',&id) new 1: insert into
class values('brindha',02) 1 row created.
SQL> /
Enter value for name:
chinthiya Enter value for id:
03
old 1: insert into class
values('&name',&id) new 1: insert into
class values('chinthiya',03) 1 row
created. SQL> select * from class;
NAME ID
anu 1
brindha 2
chinthiya 3
Create Sequence:
SQL> create sequence s_1
2 start with 4
3 increment by 1
4 maxvalue
100 5cycle;
Sequence
created.
anu 1
brindha 2
chinthiya 3
divya 4
Drop Sequence:
SQL> drop sequence s_1;
Sequence dropped.
Synonym:
SQL> select * from
class; NAMEID
Anu 1
Brindha 2
Chinthiya 3
Divya 4
ezhil 5
fairoz 7
hema 9
7 rows selected.
Create synonym:
SQL> create synonym c1 for class;
Synonym created.
SQL> insert into c1
values('kalai',20); 1 row created.
SQL> select * from class;
NAME ID
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hem 9
a 20
kalai
8 rows selected.
SQL> select *
from c1; NAMEID
anu 1
brindha 2
chinthiya 3
divya 4
Ezhil 5
fairoz 7
hema 9
kalai 20
8 rows selected.
c1; NAME ID
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
Manu 21
9 rows selected.
Drop Synonym:
SQL> drop synonym c1;
Synonym dropped.
RESULT:
Thus the creation of Views, Sequences and Synonyms has been executed successfully.
EX.NO:4 IMPLEMENTATION OF CURSORS
Aim:
To Create Cursor for Electricity Bill Calculation.
Procedure :
Implicit Cursor
Whenever any DML operations occur in the database, an implicit cursor iscreated that holds the
rows affected, in that particular operation. These cursorscannot be named and, hence they
cannot be controlled or referred from another placeof the code. We can refer only to the most
recent cursor through the cursor attributes.
Explicit Cursor
Programmers are allowed to create named context area to execute their DMLoperations to get
more control over it. The explicit cursor should be defined in thedeclaration section of the
PL/SQL block, and it is created for the 'SELECT' statementthat needs to be used in the code.
END;
SQL>createtablebill(namevarchar2(10),addressvarchar2(20),cityvarchar2(20),unitnumber(10))
; Tablecreated.
SQL>insertintobillvalues('&name','&addess','&city','&unit');Entervalueforname:yuva
Entervalueforaddess:sriviEntervalueforcity:srivilliputurEntervalueforunit:100
old
1:insertintobillvalues('&name','&addess','&city','&unit')new
1:insertintobillvalues('yuva','srivi','srivilliputur','100')
1rowcreated.
SQL>/
Entervalueforname:nithya
Entervalueforaddess:LakshminagarEntervalueforcity:sivaka
si Entervalueforunit:200
old 1:insertintobillvalues('&name','&addess','&city','&unit')
new 1:insertintobillvalues('nithya','Lakshminagar','sivakasi','200')
1rowcreated.
SQL>/
Entervalueforname:maya
Entervalueforaddess:housingboardEntervalueforcity:sivakas
i Entervalueforunit:300
old 1:insertintobillvalues('&name','&addess','&city','&unit')
new
1:insertintobillvalues('maya','housingboard','sivakasi','300')
1rowcreated.
SQL>/
Entervalueforname:jeeva
Entervalueforaddess:RRRnagarEntervalueforcity:sivaganagaiEntervalueforunit
:400 old 1:insertintobillvalues('&name','&addess','&city','&unit')
20
new
1:insertintobillvalues('jeeva','RRRnagar','sivaganagai','400')
1rowcreated.
SQL>select*frombill;
SQL>declare
2 cursorcisselect*frombill;
3 bbill%ROWTYPE;
4 begin
5 openc;
Amount'); 7 Loop
8 fetchcintob;
9 if(c%notfound)then
10 exit;
11 Else
12 if(b.unit<=100)then
13 dbms_output.put_line(b.name||''||b.address||' '||b.city||' '||b.unit||' '||b.unit*1);
14 elsif(b.unit>100andb.unit<=200)then
15 dbms_output.put_line(b.name||' '||b.address||' '||b.city||'
'||b.unit||' '||b.unit*2);
16 elsif(b.unit>200andb.unit<=300)then
17 dbms_output.put_line(b.name||' '||b.address||' '||b.city||'
'||b.unit||' '||b.unit*3);
18 elsif(b.unit>300andb.unit<=400)then
19 dbms_output.put_line(b.name||''||b.address||''||b.city||' '||b.unit||' '||b.unit*4);
20 else
21 dbms_output.put_line(b.name||''||b.address||''||b.city||' '||b.unit||' '||b.unit*5);
21
22 endif;
23 endif;
24 endloop;
25 closec;
26 end;
27 /
NameAddress cityUnit
Amount yuva srivi
srivilliputur 100 100
NithyaLakshminagar sivakasi 200 400
Maya housingboard sivakasi 300 900
jeeva RRRnagar sivaganagai 400 1600
PL/SQLproceduresuccessfullycompleted.
RESULT:
AIM
DESCRIPTION
PL/SQL PROGRAMMING
• As we want output of PL/SQL Program on screen, before Starting writing anything type (Only)
• To write program, use Notepad through Oracle using
ED command. SQL> ED ProName
Type the program Save & Exit.
• To Run
the program SQL>
@ProName
LOOPING STATEMENTS:-
For executing the set of statements repeatedly we can use loops. The oracle supports
number of looping statements like GOTO, FOR, WHILE & LOOP.
Here is the syntax of these all the types of looping statements.
GOTO STATEMENTS
<<LABEL>>
SET OF STATEMENTS
GOTO LABEL;
FOR LOOP
FOR <VAR> IN [REVERSE]
<INI_VALUE>..<END_VALUE> SET OF
STATEMENTS END LOOP;
WHILE LOOP
WHILE
(CONDITION) LOOP
SET OF
STATEMENTS
END LOOP;
LOOP STATEMENT
LOOP
SET OF
STATEMENTS
IF (CONDITION)
THEN EXIT
SET OF
STATEMENTS
END LOOP;
While using LOOP statement, we have take care of EXIT condition, otherwise it may
go into infinite loop.
SQL> set
serveroutput
on; SQL>
declare
2 a varchar2(20);
3 begin
4 a:='Hello';
5 dbms_output.put_line(a);
6 end
;7/
27.2 Insert the record into Sailors table by reading the values from
the Keyboard. SQL> create table sailors(sid number(10),sname
varchar(10),rating number(10),age number(10));
Table created.
SQL> /
2 lavanya 1 25
3 vani 2 25 RESULT
DEFINITION
A procedure or function is a logically grouped set of SQL and PL/SQL statements that perform a
specific task. They are essentially sub-programs. Procedures and functions are made up of,
• Declarative part
• Executable part
IN: Specifies that a value for the argument must be specified when calling the procedure ie.
used to pass values to a sub-program. This is the default parameter.
OUT: Specifies that the procedure passes a value for this argument back to it’s calling environment
after execution ie. used to return values to a caller of the sub-program.
INOUT: Specifies that a value for the argument must be specified when calling the procedure and
that procedure passes a value for this argument back to it’s calling environment after execution.
RETURN: It is the datatype of the function’s return value because every function must return a
value, this clause is required.
PROCEDURES – SYNTAX
variable declaration;
constant
declaration;
begin
PL/SQL subprogram
body; exception
SQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4), prodid
1 row created.
1 row created.
1 row created.
Procedure created.
2 begin
6 dbms_output.put_line('price is null');
7 end if;
8 end
;9/
Procedure
created.
SQL> exec
yyy(103);
Actual price
is 4000
Procedure created.
SQL> declare
2 a number;
3 b number;
4 begin
5 zzz(101,b);
7 end
;8/
2 begin
3 a:=a+1;
4 end;
5/
Procedure created.
SQL> declare
2 a number:=7;
3 begin
4 itit(a);
5 dbms_output.put_line(‘The updated value is ‘||a);
6 end
;7/
RESULT
The PL/SQL programs were executed and their respective outputs were verified.
Ex. No: 5C Creation of database functions
AIM
FUNCTIONS – SYNTAX
create or replace function <function name> (argument in datatype,……) return datatype {is,as}
variable declaration;
constant
declaration;
begin
PL/SQL subprogram
body; exception
exception
PL/SQL block;
end;
SQL>select *
from ittrain;
TNO TFARE
1001 550
1002 600
SALARY
Xxx 11 10000
30
Yyy 12 10500
Zzz 13 15500
SQ L>
create function aa a (train number number) return number is 2
train function it train
.tf are
%
type;
3 begin
4 select tfare into trainfunction from ittrain where tno=trainnumber;
5 return(trainfunction);
6 end; 7 /
2 total number;
3 begin
4 total:=aaa (1001);
5 dbms_output.put_line('Train fare is Rs. '||total);
6 end; 7 /
2 a number:=7;
3 f number(10);
4 begin
5 f:=itfact(a);
6 dbms_output.put_line(‘The factorial of the given number is’||f);
7 end;
8/
RESULT
Thus the functions were created, executed and their respective outputs were verified.
Ex. No: 6 Creation of database triggers
AIM
DEFINITION
• A trigger is a statement that is executed automatically by the system as a
side effect of a modification to the database. The parts of a trigger are,
• Trigger statement: Specifies the DML statements and fires the trigger body. It
also specifies the table to which the trigger is associated.
• Trigger body or trigger action: It is a PL/SQL block that is
executed when the triggering statement is used.
• Trigger restriction: Restrictions on the trigger can
be achieved The different uses of triggers are as follows,
• To generate data automatically
• To enforce complex integrity constraints
• To customize complex securing authorizations
• To maintain the replicate table
• To audit data modifications
TYPES OF TRIGGERS
• For each row: It specifies that the trigger fires once per row.
• For each statement: This is the default trigger that is invoked. It specifies that
the trigger fires once per statement.
• :new
• :old
These two variables retain the new and old values of the column updated in the database.
The values in these variables can be used in the database triggers for data manipulation
TRIGGERS - SYNTAX
create or replace trigger triggername [before/after] {DML
exceptio
n end;
TO CREATE A SIMPLE TRIGGER THAT DOES NOT ALLOW INSERT
UPDATE AND DELETE OPERATIONS ON THE TABLE
SQL> create trigger ittrigg before insert or update or delete on itempls for each row
2 begin
4 end
;5
6/
Trigger created.
values('aaa',14,34000)
* ERROR
at line 1:
ename='xxx';
* ERROR
at line 1:
ORA-20010: You cannot do
manipulation ORA-06512: at
"STUDENT.ITTRIGG",
line 2
ORA-04088: error during execution of trigger
'STUDENT.ITTRIGG' SQL> update itempls set eid=15 where
ename='yyy';
update itempls set eid=15 where ename='yyy'
*
ERROR at line 1:
ORA-20010: You cannot do
manipulation ORA-06512: at
"STUDENT.ITTRIGG",
line 2
ORA-04088: error during execution of trigger 'STUDENT.ITTRIGG'
SQL> create trigger ittriggs before insert or update of salary on itempls for each row
2 declare
3 triggsalitempls.salary%type;
4 begin
5 select salary into triggsal from itempls where eid=12;
6 if(:new.salary>triggsal or :new.salary<triggsal) then
7 raise_application_error(-20100,'Salary has not been changed');
8 end if;
9 end
; 10 /
Trigger created.
RESULT
Thus the Triggers were created, executed and their respective outputs were verified.
Ex. No: 7 PL/SQL block that handles all types of exceptions.
AIM:
DESCRIPTION:
PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL Block
known as exception Handling. Using Exception Handling we can test the code and avoid it
from exiting abruptly.
SQL> DECLARE
17.4 BEGIN
5 L_NUM1 := 10;
6 L_NUM2 := 0;
7 DBMS_OUTPUT.PUT_LINE('RESULT:'||L_NUM1/L_NUM2);
8 EXCEPTION
9 WHEN ZERO_DIVIDE THEN
10 DBMS_OUTPUT.PUT_LINE(SQLCODE);
11 DBMS_OUTPUT.PUT_LINE(SQLERRM);
12 END
;
13 /
-1476
Thus the PL/SQL program that handles exception has been implemented and output was
verified.
Ex. No: 8 Database Design using ER modeling, normalization and
DEFINITION
Normalization is the analysis of functional dependencies between attributes/data
items of user views. It reduces a complex user view to a set of small and stable subgroups of the
fields and relations. This process helps to design a logical data model known as conceptual data
model.
1NF states that the domain of an attribute must include only atomic
(simple, indivisible) values and that value of any attribute in a tuple must be a single value from
the domain of that attribute.
Attributes must be atomic:
– they can be chars, ints, strings
– they can’t be
–.1 _ tuples
–.2 _ sets
–.3 _ relations
–.4 _ composite
–.5 _ multivalued
E-R DIAGRAM
ALGORITHM:
1 row created.
4 Emp table is not in the first normal form since it has a composite attribute.So it
has been normalized to first normal form.
BEFORE NORMALIZATION :
en11 en12
Trivial functional dependency means that the right-hand side is a subset ( not necessarily
a proper subset) of the left- hand side.
Before Normalization
4.a create the table en21 with eno,ename from the table
4.e Create table en23 with eno,pno and hours from the table epnorm2.
Table created.
4.f Alter table en23 with a foreign key on eno with references on eno from
en21 SQL> alter table en23 add constraint en231 foreign key(eno)
4.g Alter table en23 with foreign key on pno with references on pno from
en22 SQL> alter table en23 add constraint en232 foreign key(pno)
After Normalization
en21 En
en22
en23
en Pn Hour
o o s
Third Normal Form(3NF)
Epnorm3
Table created.
Example record
1 row created.
3 The relation does not satisfy the 3rd normal form since dno is not a
primary key. So normalization is done for the third normal form.
Before Normalization
Empdept
3.a Create table en31 with eno,ename,sal,dno from the table emp_dept.
3.b Create table en32 with dno,dname from the table emp_dept.
3.c Alter the table en31 with the constraint primary key on eno.
3.e Alter table en31 with the constraint foreign key on dno with reference
from dno in en32
SQL> alter table en31 add constraint en311 foreign key(dno)
references en32(dno)
Table altered.
After Normalization
en31
En enam Sa dn
o e l o
en32 dname
Dno
RESULT:
Thus the study of Database Design Using ER Modeling and Normalization was
successfully implemented
Ex.No :9 RAILWAY RESERVATION SYSTEM
(Database Connectivity with Front End Tools)
AIM:
To create a Database Connectivity with Front End Tools.
DESCRIPTION:
Railway Reservation System is used to book the train tickets. It
has one form
1. Reservation form
Reservation form:
It allows to book the train ticket by selecting the train id and specifying the travel date
and no. of seats to be booked. The information entered is stored in the database.
DATABASE TABLES
TRAIN TABLE
create table train (
trainid INT PRIMARY KEY,
travels VARCHAR2(50),
departure VARCHAR2(20),
arrival VARCHAR2(20),
duration INT,
seats int,
fare int
);
CODING :
MAIN FORM
package e.ticketing;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
//TODO add your handling code here: this.setVisible(false); new
Reservation().setVisible(true)
}
RESERVATION FORM
package e.ticketing;
import java.sql.Connection; import
java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
public class Reservation extends javax.swing.JFrame { int ttrainid,fare,avail; public
Reservation() {
initComponents();
private void jrathimeenaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
Class.forName("oracle.jdbc.OracleDriver");
Connection
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","heme
sh","123");
String sql="select * from train where trainid=2";
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
if(rs.next())
{
ttrainid =rs.getInt("trainid");
fare =rs.getInt("fare");
}
String sql1="select * from trainbooking where trainid=2";
PreparedStatement pst1=conn.prepareStatement(sql1);
ResultSet rs1=pst1.executeQuery();
if(rs1.next())
{
avail =rs1.getInt("AVAILABLESEATS");
availseat.setText(String.valueOf(avail));
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
{ Class.forName("oracle.jdbc.OracleDriver");
Connectionconn=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "hemesh",
"123");
String sql="update trainbooking set availableseats=? where trainid=?";
PreparedStatement pst=conn.prepareStatement(sql); //create a statement
pst.setInt(1,avail-noseats); pst.setInt(2,ttrainid);
pst.executeUpdate(); JOptionPane.showMessageDialog(null,"Booking
Confirmed");
}
else JOptionPane.showMessageDialog(null,"Sorr y
{ Seats not Available");
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
RESULT:
Thus the project was successfully created, executed and verified.
Ex. No. 10 INVENTORY CONTROL SYSTEM
AIM:
To create a mini project named Inventory Control System.
DESCRIPTION:
Inventory Control System is a project which allows to maintain the stocks and sell the
products and update the stock.
It has three forms
• Main Menu form
• Stock Form.
• Sales Form
Main Menu Form :
It allows to choose the option whether stock entry or sales entry.
Stock Form:
It allows to enter the product id, product name, quantity, unit price and reorder value.
Sales Form:
It allows to sell the product by choosing the product id and specifying the sales
quantity. It checks whether the sales quantity is less than or equal to available quantity
and also checks whether the remaining quantity after sales is lesser than reorder level.
If so, it disallows sales.
The information entered is stored in the database.
DATABASE TABLES:
STOCK TABLE
CREATE TABLE stock
(
prodid INT PRIMARY KEY,
prodname VARCHAR2(50),
quantity INT,
unitprice INT,
reorder int
);
SALES TABLE
CREATE TABLE sale
(
prodid INT REFERENCES stock(prodid),
prodname VARCHAR2(50),
unitprice INT,
salesqty INT,
datetime VARCHAR2(50)
);
SAMPLE CODING:
STOCK ENTRY:
package conn;
import java.sql.Connection; import
java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;
public class stockentry extends javax.swing.JFrame {
Connection conn=null;
OraclePreparedStatement pst=null;
OracleResultSet rs=null;
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
STOCK SALES FORM
pst1.setInt(3,Integer.parseInt( txt_unitprice.getText
())); pst1.s
pst1.setString(5,date);
pst1.execute();
JOptionPane.showMessageDialog(null, "Sucessfully Inserted");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
RESULT
Thus the project was successfully created, executed and verified.