2013-07-12 64 views
0

我想通過JDBC調用oracle 10g的存儲過程名稱(mytable1)。 你能告訴我該怎麼做嗎? (我用的Struts2的程序的代碼)如何通過jdbc代碼調用oracle的存儲過程

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是我想打電話給------------------

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; 
程序
+0

可能重複[如何在struts2中調用oracle的存儲過程](http://stackoverflow.com/questions/17599487/how-to-call-a-stored-procedure-of-oracle-in-struts2 ) –

+1

考慮編輯/更新原件,而不是發佈您之前問題的幾乎相同的副本。 –

回答

0

照出的方式:(您可以使用這個現有的語句和RS變量)

String query = "begin mytable1(?, ?); end;"; 
rs=stmt.executeQuery(query); 

好方法:

//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); 

這就是說,我通常不建議使用存儲過程創建表。