五、查詢數據的存儲過程(模糊查詢)
-----------------存儲過程---------------------
create procedure FindCusts
@cust varchar(10)
as
select customerid from orders where customerid
like '%'+@cust+'%'
---------------執行---------------------------
execute FindCusts 'alfki'
-------------在JAVA中調用--------------------------
import java.sql.*;
public class ProcedureTest
{
public static void main(String args[]) throws Exception
{
//加載驅動
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
//獲得連接
Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
//創建存儲過程的對象
CallableStatement c=conn.divpareCall("{call FindCusts(?)}");
c.setString(1,"Tom");
ResultSet rs=c.executeQuery();
while(rs.next())
{
String cust=rs.getString("customerid");
System.out.println (cust);
}
c.close();
}
}