Академический Документы
Профессиональный Документы
Культура Документы
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:
Sql> delimiter $
BEGIN
END;
STEP 4 : OPEN ECLIPSE AND CREATE A JDBC CLASS “useMysqlProcedure” IN THE EXISTING PACKAGE AS
FOLLOWS:
import java.sql.*;
Connection con;
Statement st;
ResultSet rs;
CallableStatement stmt;
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”)’
String nm;
System.out.println(“Enter a name”);
nm=sc.next();
stmt.setString(1,nm);
rs=stmt.executeQuery();
while(rs.next()) {
System.out.println(rs.getString(1)+”\t”+rs.getString(2)+”\t”+rs.getString(3));
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:
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:
Sql> delimiter $
BEGIN
END;
STEP 4 : OPEN ECLIPSE AND CREATE A JDBC CLASS “useMysqlProcedure2” IN THE EXISTING PACKAGE
AS FOLLOWS:
import java.sql.*;
Connection con;
Statement st;
CallableStatement stmt;
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”)’
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( )
st.close();
con.close();
System.out.println(“Connection Closed”);
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
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);
while(rs.next()) {
System.out.println(rs.getInt(1) +”\t”+rs.getString(2));
rs.first();
rs.absolute(3);
rs.relative(-1); 5 john
}} 5 john
4 rajesh
3 chandrasekhar
import java.sql.*;
}
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");
Create a table EmployeeImage (Eid int , Ephoto blob, foreign key(Eid) references
Employee(Eid) on delete cascade);
import java.sql.*;
import java.io.*;
Connection con;
Statement st;
PreparedStatement ps;
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”);
String s=” Create a table EmployeeImage (Eid int , Ephoto blob, foreign key(Eid)
references Employee(Eid) on delete cascade)”;
st.executeUpdate(s);
ps.setInt(1,101);
ps.setBinaryStream(2,f,f.available());
ps.executeUpdate();
st.close();
con.close();
System.out.println(“Connection Closed”);
Ob.insertImage();
ob.closeConnection();