Hi all, i have learnt how to use stored procedure in mysql using java. I would like that with you.
create a database and then create a table.
package testproject;
/**
*
* @author Raghuram
*/
import java.sql.*;
public class StoredProcedure {
public static void createProcedure() {
Connection con = null;
Statement stat = null;
Statement stat1 = null;
Statement stat2 = null;
ResultSet rs = null;
try {
/*create procedure empproc(in name char(50),in ph
char(50))
begin
insert into emp values(name,ph);
end;*/
/*create procedure selectempproc()
begin
select * from emp;
end;*/
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/raghu", "root", "root");
stat = con.createStatement();
//stored procedure using insert query
String pr = "create procedure empproc(in name char(50),in ph "
+ "char(50)) begin insert into emp values(name,ph); end;";
//stored procedure using select query.
// String pr = "create procedure selectempproc() begin select * from emp; end;";
stat.executeUpdate(pr);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// rs.close();
// stat.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void callProcedure() {
Connection con = null;
Statement stat = null;
Statement stat1 = null;
Statement stat2 = null;
ResultSet rs = null;
long now = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/raghu", "root", "root");
now = System.currentTimeMillis();
stat = con.createStatement();
CallableStatement calstat = con.prepareCall("{call empproc(?,?)}");
calstat.setString(1, "Sriram");
calstat.setString(2, "98765432");
rs = calstat.executeQuery();
con.close();
calstat.close();
System.out.println("Your data has been inserted into table.");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// rs.close();
// stat.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void callSelectProcedure() {
Connection con = null;
Statement stat = null;
Statement stat1 = null;
Statement stat2 = null;
ResultSet rs = null;
long now = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/raghu", "root", "root");
now = System.currentTimeMillis();
stat = con.createStatement();
CallableStatement calstat = con.prepareCall("{call selectempproc()}");
// calstat.execute();
rs = calstat.executeQuery();
if (rs == null) {
System.out.println("No resultSet!");
} else {
ResultSetMetaData rsmd = rs.getMetaData();
int nbCol = rsmd.getColumnCount();
while (rs.next()) {
for (int j = 1; j <= nbCol; j++) {
System.out.println(j+"--" + rs.getString(j));
}
}
}
con.close();
calstat.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// rs.close();
// stat.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String a[]) {
createProcedure();
callProcedure();
callSelectProcedure();
}
}
8/18/10
Subscribe to:
Post Comments (Atom)
Popular Posts
-
Hi folks, I got a chance to work with JSF, it was an interesting requirement. Its about a custom component which would be of more use to ...
-
Hi Folks, I would like to share my another POC task in JSF with you all. "Primefaces Dropdown with Pagination & Filter "...
-
We shall create temp table in mysql using the sql script CREATE TEMPORARY TABLE testraghu(name VARCHAR(50) , phone VARCHAR(50)) Let us se...
-
Hi all, i would like to share a simple example of storing haspmap in mysql db. In order to store object into your db, the field type must ...
No comments:
Post a Comment