SQL - 09 (JDBC)
SQL - 09 (JDBC)
======
--in the above things the most important thing is persistant business data or
stored data.
--we store the business data for a typical business application inside a RDBMS.
A/- DBMS are excellent in data storage in secure and easily retrivable manner,
but it is very poor in processing the data and presenting the data in user-
understandable
format.
--Java is excellent in processing the data and presenting the data, but it is very
poor in storing the data.
A/- Java app can do any task only through method calls and objects. these method
calls and objects are not
directly understandable to the DB.
and DB can understand only sql statements, Java compiler does not accept the sql
syntax directly.
translator
Chinese <----------------> Russian
--this "Jdbc Driver s/w" is the implementaion of the JDBC specificaiton. which will
act as a translator to communicate our java appl with the DB s/w.
--JDBC Specification will be implemeted by the DB vendor or any 3rd party vendor
and develop jdbc driver s/w
MYSQL
ORCALE
POSTGRES
SQLSERVER
DB2
RATANDB------ driver s/w of Ratandb ------------Java application developer
--inorder to comunnicate java application and DB server, Java developer need the
jdbc driver s/w. and also jdbc api to perform the DB operation.
1.java.sql package
2.javax.sql package
--using this jdbc api we can work with jdbc driver s/w and can communicate our java
appl with the DB s/w.
.java----.class---jar file
--java developer need to get/download the jdbc driver related jar file from net
and in order to work with that jar file , we need to set that file inside the
classpath of
our java application.
mysql : mysql-connector.jar
oracle: Ojdbc6.jar
Postgres: Postrgres.jar
Jdbc Client:
==========
JDBC Driver:
==========
Demo.java:
---------------
package com.masai;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Demo {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("problem with loding the driver main class");
}
String cs = "jdbc:mysql://localhost:3306/sb101db";
try {
Connection conn= DriverManager.getConnection(cs,"root","root");
if(conn != null)
System.out.println("connected");
else
System.out.println("not connected..");
}catch(SQLException ex) {
ex.printStackTrace();
}
Note: from Java 1.6 onwards loading the driver related main class is optional.
ex:
try {
DriverManager.registerDriver(d);
}
catch(SQLException e) {
}
--the above statements is written inside the static block of the Driver class.
and whenever any class will be loaded into the memory their static blocks
will be executed.
package com.masai;
public class A {
{
System.out.println("inside non-static block of A");
}
static{
System.out.println("inside non-static block of A");
}
A(){
System.out.println("inside the constructor of A");
}
A a1 = new A();
a1.funA();
output:
inside static block of A
inside main of A
inside non-static block of A
inside the constructor of A
inside funA of A
---after loading all the static members and before starting the application from
main
jvm will execute all the static block one by one.
ex2:
----
public class A {
static{
System.out.println("inside static block of A");
A a1 = new A();
a1.funA();
}
package com.masai;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.cj.jdbc.Driver;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("problem with loding the driver main class");
}
String cs = "jdbc:mysql://localhost:3306/sb101db";
try {
Connection conn= DriverManager.getConnection(cs,"root","root");
if(x >0 )
System.out.println("inserted..");
else
System.out.println("not inserted");
}catch(SQLException ex) {
ex.printStackTrace();
}