Java Database Connectivity
JDBC Connectivity Steps…
By
Prof. Sandeep Vishwakarma
What is JDBC
Java Database Connectivity (JDBC) is an
application programming interface (API) for the
programming language Java, which defines how a
client may access a database. It is a Java-based
data access technology used for Java database
connectivity. It is part of the Java Standard Edition
platform, from Oracle Corporation.
Architecture of JDBC
Steps to Connect..
• First do the Driver Registration
•Define the Connection URL
•Established The Connection
•Create Statement Object
•Execute a Query
•Process the Results
•Close the Connection
Steps to Connect..
• Example to register the OracleDriver class
Class.forName("oracle.jdbc.driver.OracleDriver");
• Example to register the MySqlDriver class
Class.forName("com.mysql.jdbc.Driver");
• Example to register the DerbyClient
Class.forName("org.apache.derby.jdbc.ClientDriver ");
 Define the Driver Registration URL:
Class.forName();
Steps to Connect..
 Established the Connection:-
Connection con=DriverManager.getConnection(
“URL",“username",“password");
• Example to Established the Connection using MySql:-
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sandeep","root","root");
Steps to Connect..
 Create a Statement Object:
Statement stmt=con.createStatement();
Steps to Connect..
 Execute the Query:
 For the SELECT Query:
• String sql="Select * from emp";
• stmt.executeQuery(sql);
 For Insert/Update Query:
• String sql="Insert into emp values(101,'sandeep');
• stmt.executeUpdate(sql);
Steps to Connect..
 Process the Result:-
ResultSet rs=stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
Steps to Connect..
 Close the Connection:
release all the resources that the Connection
is holding.
stmt.close();
con.close();
 Step-1 Driver Registration
Class.forName("com.mysql.jdbc.Driver");
 Step-2 Database Connection
Connectioncon=DriverManager.getConnection
("jdbc:mysql://localhost:3306/cbs","root","root");
//here cbs is the database name, root is the username and root is the password
 Step-3 Creating statement
Statement stmt=con.createStatement();
 Step-4 Creating result statement
ResultSet rs=stmt.executeQuery("select * from emp");
 Step-5 Process the result
while(rs.next())
System.out.println(rs.getInt(1)+"t"+rs.getString(2));
 Step-6 Close the Connection
con.close();
Summarizing the
Connection steps
Thank You
Practical-2(A)
Using Request DispatcherInterface create a Servlet which
will validate the password entered by the user, if the user
has entered "Servlet" as password, then he will be
forwarded to Welcome Servlet else the user will stay on
the index.html page and an error message will be
displayed.
By
Prof. Sandeep Vishwakarma
Demonstration of RequestDispatcher
• index.html will have form fields to get user
information.
• Validate.java will validate the data entered
by the user.
• Welcome.java will be the welcome page.
• web.xml , the deployment descriptor.
Jdbc ppt

Jdbc ppt

  • 1.
    Java Database Connectivity JDBCConnectivity Steps… By Prof. Sandeep Vishwakarma
  • 2.
    What is JDBC JavaDatabase Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation.
  • 3.
  • 5.
    Steps to Connect.. •First do the Driver Registration •Define the Connection URL •Established The Connection •Create Statement Object •Execute a Query •Process the Results •Close the Connection
  • 6.
    Steps to Connect.. •Example to register the OracleDriver class Class.forName("oracle.jdbc.driver.OracleDriver"); • Example to register the MySqlDriver class Class.forName("com.mysql.jdbc.Driver"); • Example to register the DerbyClient Class.forName("org.apache.derby.jdbc.ClientDriver ");  Define the Driver Registration URL: Class.forName();
  • 7.
    Steps to Connect.. Established the Connection:- Connection con=DriverManager.getConnection( “URL",“username",“password"); • Example to Established the Connection using MySql:- Connection con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/sandeep","root","root");
  • 8.
    Steps to Connect.. Create a Statement Object: Statement stmt=con.createStatement();
  • 9.
    Steps to Connect.. Execute the Query:  For the SELECT Query: • String sql="Select * from emp"; • stmt.executeQuery(sql);  For Insert/Update Query: • String sql="Insert into emp values(101,'sandeep'); • stmt.executeUpdate(sql);
  • 10.
    Steps to Connect.. Process the Result:- ResultSet rs=stmt.executeQuery(sql); while(rs.next()) { System.out.println(rs.getInt(1)+" "+rs.getString(2)); }
  • 11.
    Steps to Connect.. Close the Connection: release all the resources that the Connection is holding. stmt.close(); con.close();
  • 12.
     Step-1 DriverRegistration Class.forName("com.mysql.jdbc.Driver");  Step-2 Database Connection Connectioncon=DriverManager.getConnection ("jdbc:mysql://localhost:3306/cbs","root","root"); //here cbs is the database name, root is the username and root is the password  Step-3 Creating statement Statement stmt=con.createStatement();  Step-4 Creating result statement ResultSet rs=stmt.executeQuery("select * from emp");  Step-5 Process the result while(rs.next()) System.out.println(rs.getInt(1)+"t"+rs.getString(2));  Step-6 Close the Connection con.close(); Summarizing the Connection steps
  • 13.
  • 14.
    Practical-2(A) Using Request DispatcherInterfacecreate a Servlet which will validate the password entered by the user, if the user has entered "Servlet" as password, then he will be forwarded to Welcome Servlet else the user will stay on the index.html page and an error message will be displayed. By Prof. Sandeep Vishwakarma
  • 15.
    Demonstration of RequestDispatcher •index.html will have form fields to get user information. • Validate.java will validate the data entered by the user. • Welcome.java will be the welcome page. • web.xml , the deployment descriptor.