[英]How to call a stored procedure of oracle through jdbc code
I want to call a stored procedure name (mytable1) of oracle 10g through JDBC. 我想通过JDBC调用oracle 10g的存储过程名称(mytable1)。 could you please tell me how to do that ?
你能告诉我怎么做吗? (I am using this code in a struts2 programme)
(我在struts2程序中使用此代码)
import java.sql.*;
public class loginuser
{
public String username;
public String password;
public String execute()
{
Connection con;
Statement stmt;
ResultSet rs;
try
import java.sql.*;
public class loginuser
{
public String username;
public String password;
public String execute()
{
Connection con;
Statement stmt;
ResultSet rs;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","Gaurav","oracle");
stmt=con.createStatement();
rs=stmt.executeQuery(" HOW TO CALL A PROCEDURE ? ");
}
catch(ClassNotFoundException e)
{
}
catch(SQLException ex)
{
}
}
public String getpassword()
{
return password;
}
public void setpassword(String m)
{
this.password = m;
}
public String getusername() {
return username;
}
public void setusername(String username) {
this.username = username;
}
}
--Below is the procedure i want to call------------------ -以下是我要致电的程序------------------
CREATE or replace PROCEDURE mytable1 (tname varchar2)
is
stmt varchar2(1000);
begin
stmt := 'CREATE TABLE '||tname || '(username varchar2(20) ,password varchar2 (10))';
execute immediate stmt;
end;
Crummy way: (You can use your existing stmt and rs variables for this) 愚蠢的方式:(您可以为此使用现有的stmt和rs变量)
String query = "begin mytable1(?, ?); end;";
rs=stmt.executeQuery(query);
Good way: 好办法:
//mytable1 is a stored procedure
String mytable1Sql = "{call mytable1(?)}";
CallableStatement callableStatement = dbConnection.prepareCall(mytable1Sql);
callableStatement.setString(1, "whatever");
// no out parameters, but i'll leave this in so you can see it.
// callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR);
// execute mytable1 stored procedure
callableStatement.executeUpdate();
// no out parameters, but i'll leave this in so you can see it.
// String userName = callableStatement.getString(2);
This being said, I generally don't recommend creating tables with stored procedures. 话虽如此,我通常不建议使用存储过程创建表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.