JAVA调用数据库存储过程

JAVA调用数据库存储过程

1.首相给数据库创建一个简单的存储过程:(在这里我是创建的Oracle数据库的存储过程)

create or replace procedure stu_upd(nname varchar2,npassword varchar2) is
begin
update student set name=nname where password=npassword;
end pl_pro;

create or replace procedure stu_del(nid number)is
begin
delete from student where ID=nid;
end stu_del;


create or replace procedure stu_ins(nid number,nname varchar2,npassword varchar2,nage varchar2,nemail varchar2)is
begin
insert into student(id,name,password,age,email) values(nid,nname,npassword,nage,nemail);
end stu_ins;

2.通过JAVA程序与数据库建立连接:

package jdbc;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

public class BaseDAO {

    private static BasicDataSource ds=null;
    //private static Connection conn=null;
    private static String url;
    private static String user;
    private static String password;
    private static String driverName;
    static{        
        try {
            //1.读取配置文件conf.properties,采用java.util.Properties来读取
            Properties pp=new Properties();
            //2.通过文件流读取并解析配置文件内容,本地数据库用的mysql,所以把配置文件mysql的配置放开,其他数据库配置注释
            pp.load(new FileInputStream("src/jdbc.properties"));
            driverName=pp.getProperty("jdbc.driverClassName");//获取驱动名称                        
            url=pp.getProperty("jdbc.url");//获取数据库的url                                
            user=pp.getProperty("jdbc.username");//用户名                                          
            password=pp.getProperty("jdbc.password");//密码                                    
            int maxActive=Integer.parseInt(pp.getProperty("jdbc.maxActive"));//获取最大连接数         
            int maxWait=Integer.parseInt(pp.getProperty("jdbc.maxWait"));//获取最大等待时间                          
            //3.创建一个连接池                                                                  
            ds=new BasicDataSource();                                                           
            ds.setDriverClassName(driverName);//设置驱动名称                                    
            ds.setUrl(url);//设置数据库地址                                                     
            ds.setUsername(user);//设置用户名                                                   
            ds.setPassword(password);//设置密码                                                 
            ds.setMaxActive(maxActive);//设置最大连接数                                         
            ds.setMaxWait(maxWait);//设置最大等待时间    
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    //第一种加载BasicDataSource进行数据库的连接
    public static Connection getConnection(){        
        try {
            System.out.println("数据库连接成功");
            return ds.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("连接数据库失败");
            return null;
        }        
    }
    //第二种通过传统的DriverManager进行数据库连接
    public static Connection getConnection2(){
        try {
            Class.forName(driverName);
            Connection conn = DriverManager.getConnection(url,user,password);
            return conn;
        } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
        return null;
    }
    
    
    public static void Close(Connection conn){
        if(conn!=null){                                                                       
            try {                                                                               
                conn.close();                                                                     
            } catch (Exception e) {                                                             
                e.printStackTrace();                                                              
            }                                                                                   
        }
    }
}



3.数据库连接建立完成之后就是调用存储过程操作数据库了:
package main;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

import jdbc.BaseDAO;

public class test {

    /**
     * @param args
     * @throws SQLException 
     * @throws ClassNotFoundException 
     */
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // TODO Auto-generated method stub
        
        //加载数据库的连接
        Connection conn=BaseDAO.getConnection2();
        
        //创建Oracle存储过程的对象,调用存储过程 
       // CallableStatement c=conn.prepareCall("{call pl_pro(?,?)}");
        //c.setString(1, "张三");
        //c.setString(2, "123456");
        //CallableStatement c=conn.prepareCall("{call AAA(?)}");
        //c.setLong(1, 2);
        
        //创建Oracle存储过程的对象,调用存储过程
        CallableStatement c=conn.prepareCall("{call bbb(?,?,?,?,?)}");
        //一次给存储过程传递参数
        c.setLong(1, 5);
        c.setString(2,"李四");
        c.setString(3,"123456");
        c.setString(4, "30");
        c.setString(5, "542178@.com");
        c.execute();
    }

}