JDBC Ratan
JDBC Ratan
JDBC Ratan
Core Java
Adv java
as per the sun micro systems standreds the java divided into three parts
b. J2EE/JEE(java 2 enterprise edition) : servlets , jsp , jstl , EJB , xml, webservices , JPA , JMS
2. adv java syllabus : some part of j2se & some part of j2ee
JDBC
Servlets
Technologies :
j2se
j2ee
j2me
frame works :
spring
hibernate
structs
JSF
e. frame work will give more predefined support : So we can write less lines of code
when compare to f/w the Tech we will have less predeinfed support: everything we have to write : more lines of
code
level-1
corejava
adv java
level-2
spring
hibernate
level-3
spring boot,security
https://www.protectedtext.com/advjava9am
password : java
a. it is a free of cost.
b. source code open: we can download the source code & we can do the modifications.
Day-2
e. it contians main method execution starts from main method & executed by JVM.
Web apps :
5. web applications no main method : it contians life cycle method : init service destory these are executed by servers.
Note:
JDBC code we can write the main method because it is belongs to J2SE
servlets,jsp code not possible to declare the main method it contains only life cycle methods.
client:
datase :
The technologies which are used to write the programming at client side are called client side technologies.
ex: jsp,html,css,…etc
The technologies which are used to write the programming at server side are called server side technologies.
1. presentation layer
html , css , js
2. business layer
servlets , jsp
3. persistance/database layer
JDBC
JDBC :
persitance logics means the logics which are connecting with the database.
servlets :
JSP :
Day-3
oracle 9i
oracle 10g
oracle 11g
oracle 12c
oracle 18c
oracle 19c
express edition : xe
https://www.oracle.com/database/technologies/xe-prior-releases.html
//oracle queries
desc emp;
//oracle statments
grant revoke
commit rollback
//eclipse IDE :
h --- helious
i --- indigo
j ---- juno
k ---- kepler
l --- luna
m --- mars
n --- neon
o ---- oxygen
p --- photon
https://www.eclipse.org/downloads/packages/release/oxygen/r/eclipse-ide-java-ee-developers
1. interfaces
2. Exception handling
uchecked exception
3. Scanner class
4. try-with resources.
Day - 4
DRIVER
driver : it is trransaltor who taranslate java syntax queries into database syntax.
oracle.jdbc.driver.OracleDriver
class-name : OracleDriver
The vendor given driver class-name in the form of jar file.The jar file location is,
C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib
oracle 10g:
ojdbc14.jar
oracle 11g:
oracle12c :
E:\>javap java.lang.Class
The forname() method throws ClassNotFoundException & it is a checked exception so handle the checked exception in two
ways,
Class.forName("oracle.jdbc.driver.OracleDriver")
To see the OracleDriver information just extract the jar file(ojdbc6.jar) check the location of class-name & open the driver class
using java decompiler.
http://www.javadecompilers.com
the OracleDriver class contians the static block. This static block will register the driver into DriverManager service.
static {
The IDE will take the jar file data from buildpath
When we run the applciation using command prompt in this case the command prompt will locate the jar file in class-path.
once we set the jar in classpath this will work when we are running the appication using ccommand prompt.
package com.tcs;
Class.forName("oracle.jdbc.driver.OracleDriver");
Day - 5
DRIVER
driver : it is trransaltor who taranslate java syntax queries into database syntax.
oracle.jdbc.driver.OracleDriver
class-name : OracleDriver
The vendor given driver class-name in the form of jar file.The jar file location is,
C:\oraclexe\app\oracle\product\11.2.0\server\jdbc\lib
oracle 10g:
ojdbc14.jar
oracle 11g:
oracle12c :
E:\>javap java.lang.Class
The forname() method throws ClassNotFoundException & it is a checked exception so handle the checked exception in two
ways,
Class.forName("oracle.jdbc.driver.OracleDriver")
To see the OracleDriver information just extract the jar file(ojdbc6.jar) check the location of class-name & open the driver class
using java decompiler.
http://www.javadecompilers.com
the OracleDriver class contians the static block. This static block will register the driver into DriverManager service.
static {
The IDE will take the jar file data from buildpath
When we run the applciation using command prompt in this case the command prompt will locate the jar file in class-path.
once we set the jar in classpath this will work when we are running the appication using ccommand prompt.
package com.tcs;
Class.forName("oracle.jdbc.driver.OracleDriver");
java.sql
javax.sql
this method throws SQLException & it is a checked exception so handle the checked exception using
a. try-catch blocks
b. throws
Statement st = connection.createStatement();
syntax:
this method throws SQLException & it is a checked exception so handle the checked exception using
a. try-catch blocks
b. throws
Statement:
0 1 row rowcount 0
connection.close()
syntax :
this method throws SQLException & it is a checked exception so handle the checked exception using
a. try-catch blocks
b. throws
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
int x = statement.executeUpdate(q1);
connection.close();
To overcome above limitation to close the database both normal & abnormal cases use finally block.
The finally block code is executed both normal & abnormal terminations.
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
statement = connection.createStatement();
int x = statement.executeUpdate(q1);
int y = statement.executeUpdate(q2);
int z = statement.executeUpdate(q3);
catch(SQLException|ClassNotFoundException e)
{ e.printStackTrace();
finally{
try {
if(statement!=null)statement.close();
if(connection!=null)connection.close();
} catch (SQLException e) {
e.printStackTrace();
ex-1: try-with-resources.
when we create the ResultSet the cursor is pointing to before first record.
-->rs
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
try(Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
while(set.next())
catch(SQLException e)
{ e.printStackTrace();
}
}
stop the execution 30sec : go and check the dabase the data is updated or not
Statement :
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
System.out.println("Enter query.....");
if(status)
while(set.next())
else
{ int x = statement.getUpdateCount();
System.out.println("updated records..."+x);
scanner.close();
statement.close();
connection.close();
ex-4: Assignment
Take the input eid,ename,esal from end user using scanner class
after insertion give the message do you want one more record(yes/no)
Assignments
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//Open Connection
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
stmt = con.createStatement();
int x = stmt.executeUpdate(q1);
int y = stmt.executeUpdate(q2);
System.out.println("Insert query executed with value "+y);
int z = stmt.executeUpdate(q3);
int a = stmt.executeUpdate(q4);
int b = stmt.executeUpdate(q5);
while(set.next()) {
int c = stmt.executeUpdate(q7);
Thread.currentThread();
Thread.sleep(30000);
}catch(ClassNotFoundException|SQLException|InterruptedException e)
e.printStackTrace();
finally {
try {
if(con!=null&&stmt!=null) {
stmt.close();
con.close();
System.out.println("Connection closed successfully.");
}catch (SQLException f) {
f.printStackTrace();
Assignment 2 - Accept details from user and execute insert queries - Sriharsha
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
try(Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
int i;
//loop to accept details from user and insert them into database
do{
int x = stmt.executeUpdate(query);
i = in.nextInt();
} while(i==1);
in.close();
==============================================================
Raj
/*
stop the execution 30sec : go and check the dabase the data is updated or not
*/
package ExerciseAdvJava;
import java.sql.*;
import java.util.*;
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("connection created");
Statement st = con.createStatement();
String queryUpdate = "update student set smarks = smarks + 2 where smarks <= 35";
while (rs.next()) {
Thread.sleep(10000);
catch(Exception e) {
finally{
catch(Exception e){e.printStackTrace();}
System.out.println("Connection Closed");
-----------
/*
* ex-4: Assignment
Take the input eid,ename,esal from end user using scanner class
after insertion give the message do you want one more record(yes/no)
package ExerciseAdvJava;
import java.sql.*;
import java.util.*;
System.out.println("connection created");
Statement st = con.createStatement();
while (true) {
System.out.println(res);
System.out.println("record inserted - Do you want to insert another record type yes/no ");
if (choice.equalsIgnoreCase("no"))
break;
ResultSet rs = st.executeQuery(querySelect);
while (rs.next()) {
catch(Exception e) {
e.printStackTrace();
Day - 7
package ExerciseAdvJava;
import java.sql.*;
import java.util.*;
System.out.println("connection created");
Statement st = con.createStatement();
while (true) {
System.out.println(res);
System.out.println("record inserted - Do you want to insert another record type yes/no ");
if (choice.equalsIgnoreCase("no"))
break;
catch(Exception e) {
e.printStackTrace();
1000 ---times
DBE : dtabase engine will execute the query.
a. query tokeniztion
b. query parsing.
c. query optimization
d. query execution.
1000*4 = 4k steps
when we declare the query using PS the query compilation is done only once.
when we declare the query using PS the query compilation is done only once. So steps are reduced.
ex-1:
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
while(true)
{
System.out.println("Enter emp id:");
preparedStatement.setInt(1, eid);
preparedStatement.setString(2, ename);
preparedStatement.setDouble(3, esal);
preparedStatement.executeUpdate();
if(option.equalsIgnoreCase("no"))
break;
scanner.close();
preparedStatement.close();
connection.close();
ex-2:
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
preparedStatement.setInt(1, 500);
preparedStatement.setInt(2, 5000);
System.out.println("updated records....."+row_count);
preparedStatement.close();
connection.close();
ex-3:
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
preparedStatement.setInt(1, 10000);
while(resultSet.next())
resultSet.close();
connection.close();
ex-2: Assignent : Read the data from emp table in previous direction.
=========================================================================
Assignment
ex-2: Assignent : Read the data from emp table in previous direction.
package ExerciseAdvJava;
import java.sql.*;
import java.util.*;
Class.forName("oracle.jdbc.OracleDriver");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
con.close();
ResultSet:
--->rs
ResultSet type :
forward direction
ResultSet.TYPE_FORWARD_ONLY
scrollable
ResultSet.TYPE_SCROLL_SENSITIVE
The ResultSet reflects changes made by others to the underlying data source while it remains opened.
ResultSet.TYPE_SCROLL_INSENSITIVE
The ResultSet does NOT reflect change made by others to the underlying data source while it is opened,
hence, insensitive to changes.
ResultSet concurency :
readonly
ResultSet.CONCUR_READ_ONLY
updatable
ResultSet.CONCUR_UPDATABLE
rs : 3-records
ex-1:
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
Statement statement =
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
set.afterLast();
while(set.previous())
set.first();
System.out.println(set.getInt(1)+" "+set.getString(2));
set.last();
System.out.println(set.getInt(1)+" "+set.getString(2));
set.absolute(3);
System.out.println(set.getInt(1)+" "+set.getString(2));
set.close();
connection.close();
ex:
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
Statement statement =
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
set.absolute(3);
set.updateInt(1, 3);
set.updateString(2, "srav");
set.updateFloat(3, 30000);
set.updateRow();
set.close();
connection.close();
ex: Assignment : Using ResultSet print the data in forward & backword direction.
set.moveToInsertRow();
set.updateInt(1, 55);
set.updateString(2, "ratan");
set.updateFloat(3,2000);
set.insertRow();
Standalone apps
Step 1:- prepare the component and add the components to the frame
Step 3:-conversion of static component into the dynamic component.(by adding listeners)
Example 1:
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
{ Label l;
TextField tf;
Button b;
Connection con;
Statement st;
ResultSet rs;
SearchFrame()
{ try
{ this.setVisible(true);
this.setSize(500,400);
this.setBackground(Color.pink);
this.setLayout(new FlowLayout());
l=new Label("productId");
tf=new TextField(15);
b=new Button("Search");
b.addActionListener(this);
this.add(l);
this.add(tf);
this.add(b);
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","manager");
st=con.createStatement();
catch (Exception e)
{ e.printStackTrace();
{ try
repaint();
catch (Exception e)
{ e.printStackTrace();
}
}
{ try
g.setFont(f);
boolean b=rs.next();
if(b==true)
{ g.drawString("emp id....."+rs.getInt(1),50,100);
g.drawString("emp Name....."+rs.getString(2),50,150);
g.drawString("emp sal....."+rs.getInt(3),50,200);
else
catch (Exception e)
{ e.printStackTrace();
class Test
{ new SearchFrame();
Example:-
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
{ Button b1,b2;
Connection con;
ResultSet rs;
Statement st;
String label;
MyFrame()
{ try{
this.setVisible(true);
this.setSize(500,500);
this.setBackground(Color.red);
b1=new Button("NEXT");
b2=new Button("PREVIOUS");
this.setLayout(new FlowLayout());
this.add(b1);
this.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:ratan","system","manager");
st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
catch(Exception e)
{ System.out.println(e);
{ label=e.getActionCommand();
repaint();
{ try{
if (label.equals("NEXT"))
{ boolean b=rs.next();
if (b==true)
{ g.drawString("emp number"+rs.getInt(1),50,100);
g.drawString("emp name"+rs.getString(2),50,200);
g.drawString("emp sal"+rs.getInt(3),50,300);
else
{ g.drawString("no record",50,500);
if (label.equals("PREVIOUS"))
{ boolean b=rs.previous();
if (b==true)
{ g.drawString("emp number"+rs.getInt(1),50,100);
g.drawString("emp name"+rs.getString(2),50,200);
g.drawString("emp sal"+rs.getInt(3),50,300);
else
{ g.drawString("no record",50,500);
catch(Exception e)
{ System.out.println(e); }
};
class Test
oracle.properties
comman file
read the data from properties file create the connection return the connection
f1 f2 f3 f4
size size
number varchar
CallableStatement
Transaction Management
Connection Pooling
Blob CLOB
MYsql Database
RowSet examples
as
BEGIN
END getSal;
//oracle.properties
username=system
password=ratan
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
//TestConnection
package com.tcs;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
try{
properties.load(inputStream);
Class.forName(properties.getProperty("driver"));
connection = DriverManager.getConnection(properties.getProperty("url"),properties.getProperty("username"),
properties.getProperty("password"));
e.printStackTrace();
return connection;
//DBMDex.java
package com.tcs;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
System.out.println(metaData.getDatabaseProductName());
System.out.println(metaData.getDatabaseProductVersion());
System.out.println(metaData.getUserName());
System.out.println(metaData.getURL());
System.out.println(metaData.getMaxUserNameLength());
System.out.println(metaData.getDriverName());
connection.close();
//RSMDEx
package com.tcs;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
System.out.println(metaData.getColumnCount());
System.out.println(metaData.getColumnClassName(1));
System.out.println(metaData.getColumnClassName(2));
System.out.println(metaData.getColumnName(1));
System.out.println(metaData.getColumnName(2));
System.out.println(metaData.getColumnDisplaySize(1));
System.out.println(metaData.getColumnDisplaySize(2));
System.out.println(metaData.getColumnTypeName(1));
System.out.println(metaData.getColumnTypeName(2));
connection.close();
PL/SQl :
both are to take the inputs & process the data & gives the response.
procedures :
no return value.
IN OUT
functions :
as
BEGIN
END getSal;
package com.tcs;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
callableStatement.setInt(1, 111);
callableStatement.registerOutParameter(2,Types.FLOAT);
callableStatement.executeUpdate();
System.out.println("Result...."+sal);
callableStatement.close();
connection.close();
as
BEGIN
END ;
package com.tcs;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
callableStatement.setInt(1, 11);
callableStatement.setString(2, "raj");
callableStatement.setFloat(3, 50000);
callableStatement.executeUpdate();
callableStatement.close();
connection.close();
System.out.println("Connection closed suceccsully.....");
create or replace procedure getDetails(id IN number, o1 OUT number,o2 OUT varchar,o3 OUT number)
as
BEGIN
END getDetails;
package com.tcs;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
callableStatement.setInt(1, 11);
callableStatement.registerOutParameter(2, Types.INTEGER);
callableStatement.registerOutParameter(3, Types.VARCHAR);
callableStatement.registerOutParameter(4, Types.FLOAT);
callableStatement.execute();
System.out.println("Emp id="+callableStatement.getInt(2));
System.out.println("Emp name="+callableStatement.getString(3));
System.out.println("Emp sal="+callableStatement.getFloat(4));
callableStatement.close();
connection.close();
ex-4:
PL/SQL : write a procedure to take sal input gives the multiple records as output. sal>10000
IN : 1 : number
OUT : 1 : CURSOR
JDBC :
call the procedure give the input & store the all records & print it.
ex-5 : this function is taking two eid as input : gives avg salary.
as
sal1 number;
sal2 number;
BEGIN
return (sal1+sal2)/2;
END getAvg;
package com.tcs;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection
=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
callableStatement.setInt(2, 111);
callableStatement.setInt(3, 222);
callableStatement.registerOutParameter(1,Types.FLOAT);
callableStatement.execute();
System.out.println("Result....."+callableStatement.getFloat(1));
callableStatement.close();
connection.close();
ex-6:
PL/SQL : write a function to take sal input gives the multiple records as output. sal>10000
IN : 1 : number
JDBC :
call the function give the input & store the all records & print it.
main prinicpal
a. do everyting : commit()
b. do nathing : rollback()
Local Transaction: A local transaction means, all operation in a transaction are executed against one database.
For example; If transfer money from first account to second account belongs to same bank then transaction is local transaction.
Global Transaction: A global transaction means, all operations in a transaction are executed against multiple database.
For Example; If transfer money from first account to second account belongs to different banks then the transaction is a global
transaction.
Note: Jdbc technology performs only local transactions. For global transaction in java we need either hibernate or spring
framework.
Note: In transaction management DDL(create ,alter drop) commands are not allowed.
once if we set autocommit is false then we can use commit() & rollback()
ex-1:
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
try
{ Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
connection.setAutoCommit(false);
statement = connection.createStatement();
connection.commit();
System.out.println("Transaction Successfull....");
catch (Exception e) {
try {
connection.rollback();
System.out.println("Transaction is fail.....");
e1.printStackTrace();
finally
{ try{
if(statement!=null)statement.close();
if(connection!=null)connection.close();
catch(SQLException e)
{ e.printStackTrace();
}
ex: savepoint
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
try
{ Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
connection.setAutoCommit(false);
statement = connection.createStatement();
savepoint = connection.setSavepoint();
System.out.println(".....");
System.out.println(".....");
connection.commit();
System.out.println("Transaction Successfull....");
catch (Exception e) {
try {
connection.rollback(savepoint);
System.out.println("Transaction is fail.....");
e1.printStackTrace();
finally
{ try{
if(statement!=null)statement.close();
if(connection!=null)connection.close();
catch(SQLException e)
{ e.printStackTrace();
MYSQL :
https://dev.mysql.com/downloads/installer/
+--------------------+
| Database |
+--------------------+
| information_schema |
| ahmad |
| mysql |
| naren |
| raj |
| ratan |
| test |
+--------------------+
Database changed
+------+-------+-------------+
+------+-------+-------------+
+------+-------+-------------+
ex:
https://dev.mysql.com/downloads/file/?id=496255
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("com.mysql.jdbc.Driver");
connection.close();
ex-2:
stop the execution 20sec : in thos time check the database is updated or not
MySQL tool
sqlyog
https://en.softonic.com/download/sqlyog/windows/post-download
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("com.mysql.jdbc.Driver");
while(set.next())
connection.close();
Assignments
2 as
3 BEGIN
6 END;
7 /
Procedure created.
package com.tcs;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleTypes;
System.out.println("connection created");
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.execute();
ResultSet rs = (ResultSet)cs.getObject(1);
while (rs.next()) {
con.close();
System.out.println("connection close");
}
}
2 as
3 emps SYS_REFCURSOR;
4 BEGIN
7 return emps;
8 END;
9 /
Function created.
package com.tcs;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleTypes;
System.out.println("connection created");
CallableStatement cs = con.prepareCall("{? = call getAllEmpFunction(?)}");
cs.setInt(2, 666);
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.execute();
ResultSet rs = (ResultSet)cs.getObject(1);
while (rs.next()) {
con.close();
System.out.println("connection close");
=============================================================
FileInputStream
FileOutputStream
FileReader
FileWriter
FileInputStream
package com.tcs;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
preparedStatement.setString(1, "Desert");
preparedStatement.setBinaryStream(2,inputStream,f.length());
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
package com.tcs;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
preparedStatement.setString(1, "quote");
set.next();
//write the image to HD : read the image from inputstream write to HD(writer)
int c;
while((c=inputStream.read())!=-1)
{ writer.write(c);
set.close();
writer.close();
preparedStatement.close();
connection.close();
FileReader
package com.tcs;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
preparedStatement.setInt(1, 111);
preparedStatement.setCharacterStream(2,reader,f.length());
preparedStatement.executeUpdate();
connection.close();
package com.tcs;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
preparedStatement.setInt(1, 111);
set.next();
//write the file to HD : read the file from reader write to HD(Filewriter)
while((c=reader.read())!=-1)
{ writer.write(c);
set.close();
writer.close();
preparedStatement.close();
connection.close();
java.sql
RowSet:
javax.sql
package com.tcs;
import java.sql.SQLException;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;
rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
rowSet.setUsername("system");
rowSet.setPassword("ratan");
rowSet.execute();
rowSet.afterLast();
while(rowSet.previous())
rowSet.first();
System.out.println(rowSet.getInt(1));
rowSet.last();
System.out.println(rowSet.getInt(1));
rowSet.absolute(3);
System.out.println(rowSet.getInt(1));
rowSet.close();
Batch operations:
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
statement.addBatch(q1);
statement.addBatch(q2);
statement.addBatch(q3);
statement.addBatch(q4);
statement.addBatch(q5);
{ System.out.println(res);
package com.tcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
statement.addBatch(q1);
statement.addBatch(q2);
statement.addBatch(q3);
statement.addBatch(q4);
statement.addBatch(q5);
{ System.out.println(res);
ex:
package com.tcs;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
{ Label l;
TextField tf;
Button b;
Connection con;
Statement st;
ResultSet rs;
SearchFrame()
{ try
{ this.setVisible(true);
this.setSize(500,400);
this.setBackground(Color.pink);
this.addWindowListener(new WindowAdapter() {
@Override
System.exit(0);
}
});
this.setLayout(new FlowLayout());
tf=new TextField(15);
b=new Button("Search");
this.add(l);
this.add(tf);
this.add(b);
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","ratan");
st=con.createStatement();
b.addActionListener(new ActionListener() {
@Override
try
System.out.println(q);
{ e1.printStackTrace();
});
catch (Exception e)
{ e.printStackTrace();
}
}
{ try
g.setFont(f);
boolean b = rs.next();
if(b==true)
{ g.drawString("emp id....."+rs.getInt(1),50,100);
g.drawString("emp Name....."+rs.getString(2),50,150);
g.drawString("emp sal....."+rs.getInt(3),50,200);
else
catch (Exception e)
{ e.printStackTrace();
{ new SearchFrame();
ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE
Assignment-1
package com.Advjava.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Statement statement =
connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
while (set.next())
Thread.sleep(25000);
set.beforeFirst();
while (set.next())
set.close();
connection.close();
statement.close();
}
ASSIGNMENT-2
ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE
package com.Advjava.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Statement statement =
connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
set.absolute(5);
set.updateInt(1,100604);
set.updateString(2,"Bhav");
set.updateInt(3,98);
set.updateRow();
int a = statement.executeUpdate(q2);
Thread.sleep(30000);
"where student_id=1006047";
int b = statement.executeUpdate(q3);
set.close();
connection.close();
statement.close();