JAVA DATABASE CONNECTIVITY(JDBC)

JAVA DATABASE CONNECTIVITY IS AN APPLICATION  PROGRAMMING INTERFACE (API) WHICH PROVIDE US TO CONNECT AND INTERACT WITH DATABASE. IT PROVIDES METHODS TO QUERY IN THE DATABASE THROUGH STATEMENTS LIKE…(CREATE) STATEMENT IS USED FOR CREATING TABLE IN THE DATABASE ,(UPDATE) STATEMENT IS USED FOR UPDATING THE EXISTING DATA IN THE TABLE, (DELETE) STATEMENT IS USE TO DELETE THE DATA FROM THE EXISTING TABLE , (INSERT) STATEMENT IS USED TO INSERT  DATA IN THE DATABASE

(JDBC) CODE :

package dbmspackage;

import static dbmspackage.SwitchMenu.Search;
import static dbmspackage.SwitchMenu.delete;
import static dbmspackage.SwitchMenu.insert;
import static dbmspackage.SwitchMenu.update;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
import oracle.jdbc.OracleDriver;
import java.sql.Connection;

public class StockDBMS {
public static void insert()
{
System.out.println(“Insert”);
try
{
System.out.println(“Enter Stock”);
Scanner s=new Scanner(System.in);
String itemname=s.nextLine();
String quantity=s.nextLine();
String price=s.nextLine();
String url=”jdbc:oracle:thin:@localhost:1521:xe”;
String username=”system”;
String password=”JNFJNJ”;
DriverManager.registerDriver(new OracleDriver());
Connection connection=DriverManager.getConnection(url, username, password);
System.out.println(connection);
PreparedStatement ps=connection.prepareStatement(“insert into stock values(?,?,?)”);
ps.setString(1,itemname);
ps.setString(2, quantity);
ps.setString(3, price);
int n=ps.executeUpdate();
System.out.println(n);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public static void update()
{
System.out.println(“Update”);
try
{
System.out.println(“update Stock”);
Scanner s=new Scanner(System.in);
String quantity=s.nextLine();
String itemname=s.nextLine();
String url=”jdbc:oracle:thin:@localhost:1521:xe”;
String username=”system”;
String password=”BDJJHUH”;
DriverManager.registerDriver(new OracleDriver());
Connection connection=DriverManager.getConnection(url, username, password);

System.out.println(connection);
PreparedStatement ps=connection.prepareStatement(“update stock set quantity=quantity + ? where itemname=?” );
ps.setString(1,itemname);
ps.setString(2, quantity);
int n=ps.executeUpdate();
System.out.println(n);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public static void delete()
{
System.out.println(“Delete”);
try
{
System.out.println(“Delete Stock”);
Scanner s=new Scanner(System.in);
String itemname=s.nextLine();
String url=”jdbc:oracle:thin:@localhost:1521:xe”;
String username=”system”;
String password=”invalid”;
DriverManager.registerDriver(new OracleDriver());
Connection connection=DriverManager.getConnection(url, username, password);

System.out.println(connection);
PreparedStatement ps=connection.prepareStatement(“delete from stock where itemname=?”);
ps.setString(1,itemname);
int n=ps.executeUpdate();
System.out.println(n);
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public static void Search()
{
System.out.println(“Search”);
try
{
System.out.println(“Search Stock”);
Scanner s=new Scanner(System.in);
String itemname=s.nextLine();
String url=”jdbc:oracle:thin:@localhost:1521:xe”;
String username=”system”;
String password=”invalid”;
DriverManager.registerDriver(new OracleDriver());
Connection connection=DriverManager.getConnection(url, username, password);

System.out.println(connection);
PreparedStatement ps=connection.prepareStatement(“Select * from stock where itemname=?”);
ps.setString(1,itemname);
ResultSet rs=ps.executeQuery();
if(!rs.next())
System.out.println(“Not found”);
else
{
System.out.println(“Found”);
String name=”” + rs.getObject(“itemname”);
System.out.println(itemname);

}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
while(true)
{
System.out.println(“1 for Insert, 2 for update, 3 for delete, 4 for search, 0 to exit”);
int option=s.nextInt();
switch(option)
{
case 0:
return;
case 1:
insert();
break;
case 2:update();
break;
case 3:
delete();
break;
case 4:
Search();
break;
default:
System.out.println(“Invalid”);
}
}
}
}

SCREEN SHOTS FOR THE FOLLOWING PROGRAM:

 

 

Leave a Reply