JAVA DATABASE CONNECTIVITY | DRIVERS

 JAVA DATABASE CONNECTIVITY (JDBC) -




JAVA DATABASE CONNECTIVITY | DRIVERS




            JDBC is a set of java application programming use for executing sql statement. JDBC is much similar ODBC that is open data connectivity interface to sql structure query language. JDBC API is set of interface and classes to be used for communicating with database. the interface and classes are found in java sql package. JDBC allow a developer to write client server project in java. JDBC is an interface that stands between specific database and java application like applet and servlet.


ROLE OF JDBC-


  • establish a connection  with database.
  • send sql statement.
  • process the result.
  • close connection.

JDBC CHARACTERSTIC-


  • it is called level sql interface for java.
  • jdbc mechanism are simple to understand and use.


ADVANTAGES OF JDBC-

  • easy and economical.
  • development time is short.
  • installation and version control in simple.
  • jdbc user is both application and applet.


JDBC DRIVERS-

  • JDBC ODBC bridge driver.
  • java to native API.
  • java to network protocol.
  • java to database protocol.

interface in java.sql  package


import java.sql.*;
  • collable statement
  • statement
  • preparedstatement
  • connection
  • resultset
  • metadata

essential JDBC program


  • import the java.sql package
  • load jdbc driver
  • connect to the database
  • create statement
  • execute the statement
  • retrive the result
  • close the statement and end connection


TYPES OF STATEMENTS


  • statement - simple sql query without parameter.                     
  • prepared statement - executed precompile sql query with or without parameter. prepared statement object are precompile sql statement.                                                            
  • collable statement - execute a call to a database store procedure. collable statement object are sql  store procrdure statement.


Basic jdbc or statements programs-

Q1)1. Write a menu driven program to the following operations on a set of integers as shown in the following figure. The load operation should generate 10 random integers (2 digits) and display the numbers on the screen. The save operation should save the numbers to a file “numbers.txt”. The Sort menu provides various operations and the result is displayed on the screen. 

Operation Sort
Load
Save
Numbers
Exit
Ascending
Descending

--->

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.*;

public class Jdbc {

public static void main(String[] args) {

int choice,choice1;
Connection con=null;
Statement sm=null;
ResultSet rs=null;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
Class.forName("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","1");
sm=con.createStatement();
do{
System.out.println("1.Insert");
System.out.println("2.Modify");
System.out.println("3.Delete");
System.out.println("4.Search");
System.out.println("5.View All");
System.out.println("6.Exit");
System.out.println("Enter Your Choice:\t");
choice=Integer.parseInt(br.readLine());
switch (choice) {
case 1:
System.out.println("Enter Roll No:\t");
int r=Integer.parseInt(br.readLine());
System.out.println("Enter Name:\t");
String name=br.readLine();
System.out.println("Enter Percentage:\t");
float f=Float.parseFloat(br.readLine());
int result=sm.executeUpdate("insert into Student values("+r+",'"+name+"',"+f+");");
if(result>0)System.out.println("Insert Successfully");
else System.out.println("error");
break;
case 2:
System.out.println("Enter Roll No:\t");
r=Integer.parseInt(br.readLine());
System.out.println("1.Name");
System.out.println("2.Percentage");
System.out.println("Enter Your Choice:\t");
choice1=Integer.parseInt(br.readLine());
switch (choice1) {
case 1:
System.out.println("Enter Name:\t");
name=br.readLine();
result=sm.executeUpdate("update Student set sname='"+name+"'where sno="+r);
if(result>0)System.out.println("Update Successfully");
else System.out.println("error");
break;
case 2:
System.out.println("Enter Percentage:\t");
f=Float.parseFloat(br.readLine());
result=sm.executeUpdate("update Student set per="+f+"where sno="+r);
if(result>0)System.out.println("Update Successfully");
else System.out.println("error");
break;
}
break;
case 3:
System.out.println("Enter Roll No:\t");
r=Integer.parseInt(br.readLine());
result=sm.executeUpdate("delete from Student where sno="+r+";");
if(result>0)System.out.println("Deleted Successfully");
else System.out.println("error");
break;
case 4:
System.out.println("Enter Roll No:\t");
r=Integer.parseInt(br.readLine());
rs=sm.executeQuery("select * from Student where sno="+r+";");
ResultSetMetaData rsmd=rs.getMetaData();
int col=rsmd.getColumnCount();

for(int i=1;i<=col;i++)System.out.print(rsmd.getColumnLabel(i)+"\t");
System.out.println();
while(rs.next()){
for(int i=1;i<=col;i++)System.out.print(rs.getString(i)+"\t");
System.out.println();
}
break;
case 5:
rs=sm.executeQuery("select * from Student;");
rsmd=rs.getMetaData();
col=rsmd.getColumnCount();

for(int i=1;i<=col;i++)System.out.print(rsmd.getColumnLabel(i)+"\t");
System.out.println();
while(rs.next()){
for(int i=1;i<=col;i++)System.out.print(rs.getString(i)+"\t");
System.out.println();
}
break;
case 6:System.exit(0);
}
}while(true);

}catch(Exception e){
System.out.println(e);
}

}

}

Post a Comment

0 Comments