SlideShare a Scribd company logo
Database
What is Database ?


• Database is a collection of data in an
  organized way so that we can update, modify
  data present in database easily.
• Data storage in database is permanent.
• Eg: Oracle, MySQL, MS-Access, DB2, Sybase
How to store data in database?


• Data will stored in “Tables” in database in the
  form of “Records”.
• Table is a collection of relational data in 2D i.e
  in the form of rows and columns.
What is SQL ?


• SQL – Structure Query Language
• It is language used to access and modify the data
  stored in database.
• It uses statements to access the database.
• SQL Statements can be divided into two types:
   DML
   DDL
DDL(Data Definition Language)


• These statements are used for creation or
  deletion of database.
• The commands present in DDL are:
   Create
   Alter
   Drop
Create

• This command is used to create table or
  database.

• create database;//to create database

• create table table_name(column1_name
  datatype,column2_name datatype); // to create
  table
Alter

• This command is used to rename a table or column name of an
  existing table.

• It can also be used to modify, add, drop a column/field present in
  an existing table.

• alter table table_name rename new_table_name; //to rename
  table

• alter table table_name rename column old_column_name to
  new_column_name; //to rename column
Alter

• alter table table_name add column_name datatype;
  //to add a column to table

• alter table table_name modify column_name
  new_datatype; //to modify datatype of column of a
  table

• alter table table_name drop column column_name;
  //to drop a column of a table
Drop


• This command is used to remove an
  object(value).
• We can not get the table or values in it once
  the table is dropped.
• drop table table_name; //to drop a table
DML(Data Manipulation Language)


• These statements are used for managing the
  data in database.
• The commands present in DDL are:
   Insert
   Update
   Delete
   Select
Insert

• This command is used to enter the records
  into the table.

• insert into table_name values(‘value_col_1’,
  ‘value_col_2’); //to insert values into table
Update

• This command is used to update the value of a record
  or column of a table.
• We use condition to know where the updation is to be
  made.

• update table set column_name=“value” where
  some_column_name = “value_of_some_column”;
  //to update the column where it satisfies condition
Delete


• This command is used to delete the records (one or
  many) from a table by specifying some condition
  where to delete the record.

• delete from table_name where column_name =
  “value”;
 //to delete record where it satisfies given condition
select

• Select command is used to retrieve the records from
  the table.

• select column1_name, column2_name,…………. from
  table_name; //to select the specific columns from
  table

• select * from table_name; // to retrieve all records
  present in table
Integrity Constraints


•   Not Null
•   Unique
•   Primary Key
•   Foreign Key
•   Check
Not Null

• It is a constraint that ensures that every row is
  filled with some value for the column which has
  been specified as “not null”.

• create table table_name
  (column1_name datatype NOT NULL,
  column2_name datatype );
Unique

• It is a constraint used for column of a table so
  that rows of that column should be unique.
• It allows null value also.

• create table table_name(
  column1_name datatype UNIQUE,
  column2_name datatype);
Primary Key

• It is a constraint used for a column of a table
  so that we can identify a row in a table
  uniquely.
• create table table_name(
• column1_name datatype PRIMARY KEY,
• column2_name datatype);
Foreign key

• It is a constraint used to establish a relationship between
  two columns in the same or different tables.

• For a column to be defined as a Foreign Key, it should be a
  defined as a Primary Key in the table which it is referring.

• One or more columns can be defined as Foreign key.
• This constraint identifies any column referencing the
  PRIMARY KEY in another table.
SQL clauses


  •   select   • order by
  •   from     • group by
  •   where    • having
  •   update
Order by


• This clause is used the values of a column in
  ascending or descending order.
• select empname from emp1
  order by empname;
Group by


• This clause is used to collect data across
  multiple records and group results by one or
  more columns.
• select department from Student
  where subject1> 20
  group by department;
Having


• This clause is used in combination with sql group
  by clause.
• It is used to filter the records that a sql group by
  returns.
• select max(subject1) from Student
  group by department
  having min(subject1)>20;
Basic Functions

• COUNT() – to count the number of records satisfy
              condition.
• MAX()- to find the maximum value of a column of all
          the records present in table.
• MIN()- to find the minimum value of a column of
         all the records present in table.
• AVG()- to find the average value of a column of
         all the records present in table.
• SUM()- to find the sum of all the values of a column
          in a table.
Basic Functions

• AVG()- to find the average value of a column
         of all the records present in table.
• SUM()- to find the sum of all the values of a
          column in a table.
• Distinct() - to find the distinct record of a table
Joins

• Join is used to retrieve data from two or more
  database tables.
• Joins are of four types:
 Cross join- This join returns the combination of
  each row of the first table with every column of
  second table.
 Inner join-This join will return the rows from both
  the tables when it satisfies the given condition.
Joins

 Left Outer join –suppose if we have two tables. One on
  the left and the other on right side.

      When left outer join is applied on these two tables it
  returns all records of the left side table even if no
  matching rows are found in right side table

 Right Outer join- it is converse of left outer join. It
  returns all the rows of the right side table even if no
  matching rows are found in left side table.
JDBC
JDBC


• JDBC-Java Database Connectivity.
• JDBC is an API that helps us to connect our
  java program to the database and do the
  manipulation of the data through java
  program.
How to write a program in jdbc?


•   Register the Driver
•   Connecting to database
•   Create SQL statement in java
•   Execute SQL statements
•   Retrieve Result
•   Close Connection
JDBC program
public class JDBCProgram{
public static void main(String[] args) {
try {
        Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);//REGISTERING DATABASE
        Connection con = DriverManager.getConnection("jdbc:odbc:dsn", "",""); //CONNECTING DATABASE
        Statement st=con.createStatement();//CREATE STATEMENT
        st.execute("create table Employee(empid integer, empname varchar(50))");//EXECUTING STATEMENT
        st.close();// CLOSING STATEMENT
        con.close();//CLOSING CONNECTION
    }
catch (Exception err) {
      System.out.println( "Error: " + err );
      }
}
}
How to insert values into table using jdbc?

• To insert the values into the table we use prepared statement.

• PreparedStatement ps = con.prepareStatement("insert into
  Table_name(col1,col2)values(?,?)");//creating preparedstatement

  ps.setInt(1,value_col1);//inserting the value in column1
  ps.setString(2, “value_col2”); //inserting the value in column2

  ps.executeUpdate();//to execute the prepared statement
How to update values in table using jdbc?

• To update the values into the table also we use prepared statement.

• PreparedStatement ps = con.prepareStatement(“update Table_name set
  col1=?,col2=? where col1=?);//creating preparedstatement

  ps.setInt(1,new_value_col1);//updating new value in column1
  ps.setString(2, “value_col2”); //updating the value in column2
  ps.setInt(1,old_value_col1);//getting old value of column1
  ps.executeUpdate();//to execute the prepared statement
How to delete record in table using jdbc?

• To delete the record in the table also we use prepared statement.

• PreparedStatement ps = con.prepareStatement(“delete from Table_name
  where col1=?); //creating preparedstatement

  ps.setInt(1,old_value_col1);//getting value of column1
  ps.executeUpdate();//to execute the prepared statement
How to retrieve record from table using jdbc?

• To store the retrieved records we should use ResultSet.

• ResultSet rs = st.executeQuery("select * from Employee"); //using
  result set to store the result
  while(rs.next())
  {
  System.out.println(rs.getInt(1)); //to retrieve value of col1
  System.out.println(rs.getString(2)); //to retrieve value of
                                          col2
  }
•Q& A..?
Thanks..!

More Related Content

What's hot (19)

PPTX
SQL(DDL & DML)
Sharad Dubey
 
PPT
Sql basics and DDL statements
Mohd Tousif
 
PPTX
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
PPTX
Database COMPLETE
Abrar ali
 
PPTX
Lab2 ddl commands
Balqees Al.Mubarak
 
PPTX
8. sql
khoahuy82
 
PPT
Sql DML
Vikas Gupta
 
PPTX
Getting Started with MySQL II
Sankhya_Analytics
 
PPTX
Creating database using sql commands
Belle Wx
 
PPTX
MySQL Essential Training
HudaRaghibKadhim
 
ODP
Sql commands
Balakumaran Arunachalam
 
PDF
STRUCTURED QUERY LANGUAGE
SarithaDhanapal
 
PPTX
Database Systems - SQL - DCL Statements (Chapter 3/4)
Vidyasagar Mundroy
 
PDF
Sql smart reference_by_prasad
paddu123
 
PPTX
Commands of DML in SQL
Ashish Gaurkhede
 
SQL(DDL & DML)
Sharad Dubey
 
Sql basics and DDL statements
Mohd Tousif
 
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
Database COMPLETE
Abrar ali
 
Lab2 ddl commands
Balqees Al.Mubarak
 
8. sql
khoahuy82
 
Sql DML
Vikas Gupta
 
Getting Started with MySQL II
Sankhya_Analytics
 
Creating database using sql commands
Belle Wx
 
MySQL Essential Training
HudaRaghibKadhim
 
STRUCTURED QUERY LANGUAGE
SarithaDhanapal
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Vidyasagar Mundroy
 
Sql smart reference_by_prasad
paddu123
 
Commands of DML in SQL
Ashish Gaurkhede
 

Viewers also liked (8)

PPTX
Java class 1
Edureka!
 
PPTX
Java class 7
Edureka!
 
PPTX
Java class 4
Edureka!
 
PDF
Java
Edureka!
 
PPTX
Java class 5
Edureka!
 
PPTX
Java class 3
Edureka!
 
PPTX
Java class 6
Edureka!
 
PPTX
What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...
Edureka!
 
Java class 1
Edureka!
 
Java class 7
Edureka!
 
Java class 4
Edureka!
 
Java
Edureka!
 
Java class 5
Edureka!
 
Java class 3
Edureka!
 
Java class 6
Edureka!
 
What Is Salesforce CRM? | Salesforce CRM Tutorial For Beginners | Salesforce ...
Edureka!
 
Ad

Similar to Java class 8 (20)

PPT
Db1 lecture4
Sherif Gad
 
PPTX
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
PPTX
OracleSQLraining.pptx
Rajendra Jain
 
PPT
Mysql 120831075600-phpapp01
sagaroceanic11
 
PPTX
Lab
neelam_rawat
 
PPT
MY SQL
sundar
 
PPTX
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
PDF
Structured Query Language - All commands Notes
sarithaapgcr
 
DOC
Oracle
Rajeev Uppala
 
PPTX
Dbms sql-final
NV Chandra Sekhar Nittala
 
PPT
dbs class 7.ppt
MARasheed3
 
PPTX
Sql slid
pacatarpit
 
PPTX
Its about a sql topic for basic structured query language
IMsKanchanaI
 
PPTX
Avinash database
avibmas
 
PPTX
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
nimsarabuwaa2002
 
PDF
Dbms interview questions
ambika93
 
PDF
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
zmulani8
 
PDF
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
rohithlingineni1
 
PDF
23. SQL and Database.pdf
AmaanRizvi6
 
Db1 lecture4
Sherif Gad
 
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
OracleSQLraining.pptx
Rajendra Jain
 
Mysql 120831075600-phpapp01
sagaroceanic11
 
MY SQL
sundar
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
Structured Query Language - All commands Notes
sarithaapgcr
 
dbs class 7.ppt
MARasheed3
 
Sql slid
pacatarpit
 
Its about a sql topic for basic structured query language
IMsKanchanaI
 
Avinash database
avibmas
 
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
nimsarabuwaa2002
 
Dbms interview questions
ambika93
 
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
zmulani8
 
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
rohithlingineni1
 
23. SQL and Database.pdf
AmaanRizvi6
 
Ad

More from Edureka! (20)

PDF
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
PDF
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
PDF
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
PDF
Tableau Tutorial for Data Science | Edureka
Edureka!
 
PDF
Python Programming Tutorial | Edureka
Edureka!
 
PDF
Top 5 PMP Certifications | Edureka
Edureka!
 
PDF
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
PDF
Linux Mint Tutorial | Edureka
Edureka!
 
PDF
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
PDF
Importance of Digital Marketing | Edureka
Edureka!
 
PDF
RPA in 2020 | Edureka
Edureka!
 
PDF
Email Notifications in Jenkins | Edureka
Edureka!
 
PDF
EA Algorithm in Machine Learning | Edureka
Edureka!
 
PDF
Cognitive AI Tutorial | Edureka
Edureka!
 
PDF
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
PDF
Blue Prism Top Interview Questions | Edureka
Edureka!
 
PDF
Big Data on AWS Tutorial | Edureka
Edureka!
 
PDF
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
PDF
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
PDF
Introduction to DevOps | Edureka
Edureka!
 
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Edureka!
 

Recently uploaded (20)

PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Horarios de distribución de agua en julio
pegazohn1978
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 

Java class 8

  • 2. What is Database ? • Database is a collection of data in an organized way so that we can update, modify data present in database easily. • Data storage in database is permanent. • Eg: Oracle, MySQL, MS-Access, DB2, Sybase
  • 3. How to store data in database? • Data will stored in “Tables” in database in the form of “Records”. • Table is a collection of relational data in 2D i.e in the form of rows and columns.
  • 4. What is SQL ? • SQL – Structure Query Language • It is language used to access and modify the data stored in database. • It uses statements to access the database. • SQL Statements can be divided into two types: DML DDL
  • 5. DDL(Data Definition Language) • These statements are used for creation or deletion of database. • The commands present in DDL are: Create Alter Drop
  • 6. Create • This command is used to create table or database. • create database;//to create database • create table table_name(column1_name datatype,column2_name datatype); // to create table
  • 7. Alter • This command is used to rename a table or column name of an existing table. • It can also be used to modify, add, drop a column/field present in an existing table. • alter table table_name rename new_table_name; //to rename table • alter table table_name rename column old_column_name to new_column_name; //to rename column
  • 8. Alter • alter table table_name add column_name datatype; //to add a column to table • alter table table_name modify column_name new_datatype; //to modify datatype of column of a table • alter table table_name drop column column_name; //to drop a column of a table
  • 9. Drop • This command is used to remove an object(value). • We can not get the table or values in it once the table is dropped. • drop table table_name; //to drop a table
  • 10. DML(Data Manipulation Language) • These statements are used for managing the data in database. • The commands present in DDL are: Insert Update Delete Select
  • 11. Insert • This command is used to enter the records into the table. • insert into table_name values(‘value_col_1’, ‘value_col_2’); //to insert values into table
  • 12. Update • This command is used to update the value of a record or column of a table. • We use condition to know where the updation is to be made. • update table set column_name=“value” where some_column_name = “value_of_some_column”; //to update the column where it satisfies condition
  • 13. Delete • This command is used to delete the records (one or many) from a table by specifying some condition where to delete the record. • delete from table_name where column_name = “value”; //to delete record where it satisfies given condition
  • 14. select • Select command is used to retrieve the records from the table. • select column1_name, column2_name,…………. from table_name; //to select the specific columns from table • select * from table_name; // to retrieve all records present in table
  • 15. Integrity Constraints • Not Null • Unique • Primary Key • Foreign Key • Check
  • 16. Not Null • It is a constraint that ensures that every row is filled with some value for the column which has been specified as “not null”. • create table table_name (column1_name datatype NOT NULL, column2_name datatype );
  • 17. Unique • It is a constraint used for column of a table so that rows of that column should be unique. • It allows null value also. • create table table_name( column1_name datatype UNIQUE, column2_name datatype);
  • 18. Primary Key • It is a constraint used for a column of a table so that we can identify a row in a table uniquely. • create table table_name( • column1_name datatype PRIMARY KEY, • column2_name datatype);
  • 19. Foreign key • It is a constraint used to establish a relationship between two columns in the same or different tables. • For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring. • One or more columns can be defined as Foreign key. • This constraint identifies any column referencing the PRIMARY KEY in another table.
  • 20. SQL clauses • select • order by • from • group by • where • having • update
  • 21. Order by • This clause is used the values of a column in ascending or descending order. • select empname from emp1 order by empname;
  • 22. Group by • This clause is used to collect data across multiple records and group results by one or more columns. • select department from Student where subject1> 20 group by department;
  • 23. Having • This clause is used in combination with sql group by clause. • It is used to filter the records that a sql group by returns. • select max(subject1) from Student group by department having min(subject1)>20;
  • 24. Basic Functions • COUNT() – to count the number of records satisfy condition. • MAX()- to find the maximum value of a column of all the records present in table. • MIN()- to find the minimum value of a column of all the records present in table. • AVG()- to find the average value of a column of all the records present in table. • SUM()- to find the sum of all the values of a column in a table.
  • 25. Basic Functions • AVG()- to find the average value of a column of all the records present in table. • SUM()- to find the sum of all the values of a column in a table. • Distinct() - to find the distinct record of a table
  • 26. Joins • Join is used to retrieve data from two or more database tables. • Joins are of four types:  Cross join- This join returns the combination of each row of the first table with every column of second table.  Inner join-This join will return the rows from both the tables when it satisfies the given condition.
  • 27. Joins  Left Outer join –suppose if we have two tables. One on the left and the other on right side. When left outer join is applied on these two tables it returns all records of the left side table even if no matching rows are found in right side table  Right Outer join- it is converse of left outer join. It returns all the rows of the right side table even if no matching rows are found in left side table.
  • 28. JDBC
  • 29. JDBC • JDBC-Java Database Connectivity. • JDBC is an API that helps us to connect our java program to the database and do the manipulation of the data through java program.
  • 30. How to write a program in jdbc? • Register the Driver • Connecting to database • Create SQL statement in java • Execute SQL statements • Retrieve Result • Close Connection
  • 31. JDBC program public class JDBCProgram{ public static void main(String[] args) { try { Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);//REGISTERING DATABASE Connection con = DriverManager.getConnection("jdbc:odbc:dsn", "",""); //CONNECTING DATABASE Statement st=con.createStatement();//CREATE STATEMENT st.execute("create table Employee(empid integer, empname varchar(50))");//EXECUTING STATEMENT st.close();// CLOSING STATEMENT con.close();//CLOSING CONNECTION } catch (Exception err) { System.out.println( "Error: " + err ); } } }
  • 32. How to insert values into table using jdbc? • To insert the values into the table we use prepared statement. • PreparedStatement ps = con.prepareStatement("insert into Table_name(col1,col2)values(?,?)");//creating preparedstatement ps.setInt(1,value_col1);//inserting the value in column1 ps.setString(2, “value_col2”); //inserting the value in column2 ps.executeUpdate();//to execute the prepared statement
  • 33. How to update values in table using jdbc? • To update the values into the table also we use prepared statement. • PreparedStatement ps = con.prepareStatement(“update Table_name set col1=?,col2=? where col1=?);//creating preparedstatement ps.setInt(1,new_value_col1);//updating new value in column1 ps.setString(2, “value_col2”); //updating the value in column2 ps.setInt(1,old_value_col1);//getting old value of column1 ps.executeUpdate();//to execute the prepared statement
  • 34. How to delete record in table using jdbc? • To delete the record in the table also we use prepared statement. • PreparedStatement ps = con.prepareStatement(“delete from Table_name where col1=?); //creating preparedstatement ps.setInt(1,old_value_col1);//getting value of column1 ps.executeUpdate();//to execute the prepared statement
  • 35. How to retrieve record from table using jdbc? • To store the retrieved records we should use ResultSet. • ResultSet rs = st.executeQuery("select * from Employee"); //using result set to store the result while(rs.next()) { System.out.println(rs.getInt(1)); //to retrieve value of col1 System.out.println(rs.getString(2)); //to retrieve value of col2 }