All the examples are taken in the context of book table, that we have created today,
like this,
create table book (id INT,topic varchar(30),author varchar(30),price float,publisher varchar(30),primary key(id));
note: this table is created inside the database named as books
============================================================
so,
in java application we can use values as well as variable names inside sql statements
while using jdbc
#### we can pass a value for sql statements inside two single quotes
syntax for insert:
insert into tablename values('value-1','value-2','value-n');
example for insert,
insert into book values('1','c++','kanitkar','450.0','PHI');
syntax for update:
update tablename set column-name='value' where column-name='value';
example for update,
update book set price='400' where author='kanitkar';
syntax for delete:
delete from tablename where column-name='value';
example for delete,
delete from book where publisher='PHI';
syntax for select:
delete from tablename where column-name='value';
example for select,
select * from book where publisher='PHI';
#### we can also pass a variable name inside two single quotes using given syntax
syntax to pass a variable name inside sql statement:
<single-quotes><double-quotes>+VariableName+<double-quotes><single-quotes>
example,
' " +VariableName+ " '
some examples to show how to pass variable names inside sql statement in java application
## lets have some variables
int bookId = 10;
String bookTopic = "PHP";
String bookAuthor =" Rakesh";
float bookPrice = 400.0;
String bookPublisher = "BPB";
## now pass variable names inside insert statement
insert into book values('"+bookId+"','"+bookTopic+"','"+bookAuthor+"','"+bookPrice+"','"+bookPublisher+"');
## now pass variable names inside update statement
update book set price='"+bookPrice+"' where topic='"+bookTopic+"';
## now pass the variable names inside delete statement
delete from book where publisher='"+bookPublisher+"';
## now pass the variable names inside select statement
select * from book where author='"+bookAuthor+"' and publisher='"+bookPublisher+"';
note:
1. the insert or update or delete statement should be used inside executeUpdate() method of Statement interface
fact: the executeUpdate() method returns number of records affected inside the table
2. the select statement should be used inside the executeQuery() method of Statement interface
fact: the executeQuery() method returns ResultSet, which stores the data of a table
Very useful . The best concept in java, jdbc and very well taught by HASAN ARSHI SIR
ReplyDeleteThank you very much sir.
ReplyDeleteVery nice sir..!! :)
ReplyDeleteVery nice sir..!! :)
ReplyDelete