Вы находитесь на странице: 1из 10

Q.

1 CREATE A STORED PROCEDURE (IN PARAMETER) IN MYSQL FOR BANK SCHEMA TO PRINT THE
DETAILS OF A PARTICULAR CUSTOMER. INVOKE THAT PROCEDURE USING JDBC METHOD CALL.

ANSWER:

STEP 1: OPEN MYSQL PROMPT

STEP 2: USE BANK;

STEP 3: //CREATION OF PROCEDURE

Sql> delimiter $

CREATE OR REPLACE PROCEDURE GETCUSTDETAILS (IN NAME VARCHAR(10))

BEGIN

SELECT * FROM CUSTOMER WHERE CUST_NAME=NAME;

END;

STEP 4 : OPEN ECLIPSE AND CREATE A JDBC CLASS “useMysqlProcedure” IN THE EXISTING PACKAGE AS
FOLLOWS:

import java.sql.*;

public class useMysqlProcedure {

Connection con;

Statement st;

ResultSet rs;

CallableStatement stmt;

public useMysqlProcedure() throws Exception {

Class.forName(“com.mysql.jdbc.Driver”);

System.out.println(“Driver Loaded”);

con= DriverManager.getConnection(“jdbc:mysql://localhost:3306/”,”root”,”root”);

System.out.println(“Connection Established”);
st=con.createStatement();

st.executeUpdate(“use Bank”)’

System.out.println(“Database opened to use”);

public void invokeProcedure() throws Exception {

stmt= con.prepareCall(“ call GETCUSTDETAILS(?)”);

Scanner sc=new Scanner(System.in);

String nm;

System.out.println(“Enter a name”);

nm=sc.next();

stmt.setString(1,nm);

rs=stmt.executeQuery();

System.out.println(“The Details are”);

while(rs.next()) {

System.out.println(rs.getString(1)+”\t”+rs.getString(2)+”\t”+rs.getString(3));

public void closeConnection() throws Exception {

rs.close();

st.close();

stmt.close();

con.close();

System.out.println(“Connection Closed”);

}
STEP 5 : CREATE JDBC CLASS “TEST” IN THE EXISTING PACKAGE AS FOLLOWS:

import jdbcApp. useMysqlProcedure;

public class TEST {

public static void main(String args[]) throws Exception {

useMysqlProcedure ob=new useMysqlProcedure();

ob.invokeProcedure();

ob.closeConnection();

}
}

Q.2 CREATE A STORED PROCEDURE (IN, OUT PARAMETER) IN MYSQL FOR BANK SCHEMA TO PRINT
THE ACCOUNT BALANCE OF A PARTICULAR CUSTOMER. INVOKE THAT PROCEDURE USING JDBC
METHOD CALL.

ANSWER:

STEP 1: OPEN MYSQL PROMPT

STEP 2: USE BANK;

STEP 3: //CREATION OF PROCEDURE

Sql> delimiter $

CREATE OR REPLACE PROCEDURE PRINTBALANCE (IN NAME VARCHAR(10),OUT BLNC REAL)

BEGIN

SELECT A.BALANCE INTO BLNC FROM ACCOUNT A,CUSTOMER C,DEPOSITOR D WHERE


C.CUST_NAME=D.CUST_NAME AND D.ACCNO=A.ACCNO AND C.CUST_NAME=NAME;

END;

STEP 4 : OPEN ECLIPSE AND CREATE A JDBC CLASS “useMysqlProcedure2” IN THE EXISTING PACKAGE
AS FOLLOWS:
import java.sql.*;

public class useMysqlProcedure2 {

Connection con;

Statement st;

CallableStatement stmt;

public useMysqlProcedure2() throws Exception {

Class.forName(“com.mysql.jdbc.Driver”);

System.out.println(“Driver Loaded”);

con= DriverManager.getConnection(“jdbc:mysql://localhost:3306/”,”root”,”root”);

System.out.println(“Connection Established”);

st=con.createStatement();

st.executeUpdate(“use Bank”)’

System.out.println(“Database opened to use”);

public void invokeProcedure() throws Exception {

stmt= con.prepareCall(“ call PRINTBALANCE(?,?)”);

Scanner sc=new Scanner(System.in);

String nm;

double d=0.0;

System.out.println(“Enter a name”);

nm=sc.next();

stmt.setString(1,nm);

stmt.registerOutParameter(2,java.sql.Types.DOUBLE);

stmt.executeQuery();

d=stmt.getDouble(2);
System.out.println(“The balance of ”+nm+” is: ”+d);

System.out.println( )

public void closeConnection() throws Exception {

st.close();

con.close();

System.out.println(“Connection Closed”);

STEP 5 : CREATE JDBC CLASS “TEST” IN THE EXISTING PACKAGE AS FOLLOWS:

import jdbcApp. useMysqlProcedure2;

public class TEST {

public static void main(String args[]) throws Exception {

useMysqlProcedure2 ob=new useMysqlProcedure2();

ob.invokeProcedure();

ob.closeConnection();

}
}
Q.3) Scrollable ResultSet Methods

Table : employee

Eid ename
1 Chandra sekhar
2 murad
3 Chandra sekhar
4 rajesh
5 john

public class ScrollRSTest {

public static void main(String args[]) throws Exception {

Class.forName(“com.mysql.jdbc.Driver”);

Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”root”);

Statement
st=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

ResultSet rs=st.executeQuery(“select * from Employee”);

while(rs.next()) {

System.out.println(rs.getInt(1) +”\t”+rs.getString(2));

rs.first();

System.out.println(rs.getInt(1) + " \t " + rs.getString(2));

rs.absolute(3);

System.out.println(rs.getInt(1) + " \t " + rs.getString(2)); Output:

rs.last(); 1 chandra sekhar


System.out.println(rs.getInt(1) + " \t " + rs.getString(2)); 2 Murad

rs.previous(); 3 chandra sekhar

System.out.println(rs.getInt(1) + " \t " + rs.getString(2)); 4 rajesh

rs.relative(-1); 5 john

System.out.println(rs.getInt(1) + " \t " + rs.getString(2)); 1 chandra sekhar

con.close(); 3 chandra sekhar

}} 5 john

4 rajesh

3 chandrasekhar

Q.4)Testing DatabaseMetaData and ResultSetMetaData

Print all the tables present in “company schema” (use of DatabaseMetaData)

Print all the column name for Employee (use of ResultSetMetaData)

import java.sql.*;

public class JDBCMetadataApp {


Connection con;
Statement st;
ResultSet rs;
DatabaseMetaData dm;
ResultSetMetaData rm;
JDBCMetadataApp() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","roo
t");
System.out.println("Connection Established");
st=con.createStatement();
st.executeUpdate("use Bank");
System.out.println("Database opened to use");

}
void printTableName() throws Exception {
dm=con.getMetaData();
rs=dm.getCatalogs();
System.out.println("Tables present in company schema are: ");
while(rs.next()) {
System.out.println(rs.getString(1));
}

}
void printColumnName() throws Exception {
rs=st.executeQuery("select * from employee");
rm=rs.getMetaData();
int n=rm.getColumnCount();
System.out.println("No. of column in employee table : "+n);
System.out.println("\n\nColumn names present in employee table are:
");
String cname;
for(int i=1;i<=n;i++) {
cname=rm.getColumnName(i);
System.out.println(cname);
}
}
void closeConnection() throws Exception {
st.close();
con.close();
System.out.println("Connection Closed");

public static void main(String args[]) throws Exception {


JDBCMetadataApp ob=new JDBCMetadataApp();
ob.printTableName();
ob.printColumnName();
ob.closeConnection();
}
}

Q.5 ) Inserting Image to table

Create a table EmployeeImage (Eid int , Ephoto blob, foreign key(Eid) references
Employee(Eid) on delete cascade);
import java.sql.*;

import java.io.*;

public class ImageInsertion {

Connection con;

Statement st;

PreparedStatement ps;

ImageInsertion() throws Exception {

Class.forName(“com.mysql.jdbc.Driver”);

System.out.println(“Driver Loaded”);

con=
DriverManager.getConnection(“jdbc:mysql://localhost:3306/”,”root”,”root”);

System.out.println(“Connection Established”);

st=con.createStatement();

st.executeUpdate(“use Company”);

System.out.println(“Database opened to use”);

void insertImage() throws Exception {

String s=” Create a table EmployeeImage (Eid int , Ephoto blob, foreign key(Eid)
references Employee(Eid) on delete cascade)”;

st.executeUpdate(s);

FileInputStream f=new FileInputStream(“src\\images\phot.jpg”);

String s2=”insert into EmployeeImage values(?,?)”;


ps=con.prepareStatement();

ps.setInt(1,101);

ps.setBinaryStream(2,f,f.available());

ps.executeUpdate();

System.out.println(“Image Inserted “);

public void closeConnection() throws Exception {

st.close();

con.close();

System.out.println(“Connection Closed”);

public static void main(String args[]) throws Exception {

ImageInsertion ob=new ImageInsertion();

Ob.insertImage();

ob.closeConnection();

Вам также может понравиться