我就不吃芹菜

导航

 

在使用JDBC的时候不能直接将运行存储过程的语句写成sql去执行,

直接写sql语句插入数据:

public int insertStu(String sname,String ssex) throws Exception {
    Connection con = Dbhelper.getconnection();
    PreparedStatement ps = null;
    int result = -1;
    if(con!=null) {
        try {
            String sql = "insert into t_student values("+sname+","+ssex+")";
            ps = con.prepareStatement(sql);
            ps.executeUpdate();
            con.close();
        } 
        catch(Exception ex) {
            System.out.println(ex);
        } 
        finally{
            try {
                ps.close();
            } catch(Exception ex1) {
                System.out.println(ex1);
            }
        }
    }
    return result;
}

 

调用存储过程插入语句:

public int insertStu(String sname,String ssex) throws Exception {
    Connection con = Dbhelper.getconnection();
    PreparedStatement ps = null;
    int result = -1;
    if(con!=null) {
        try {
            //String sql = "exec indata('"+sname+"','"+ssex+"')";     //这样是不行的
            String sql = "{call indata('"+sname+"','"+ssex+"')}";     //应该写成这样
            System.out.println(sql);
            ps = con.prepareCall(sql);
            ps.executeUpdate();
            con.close();
        } catch(Exception ex) {
            System.out.println(ex);
        } finally{
            try {
                ps.close();
            } catch(Exception ex1) {
                System.out.println(ex1);
            }
        }
    }
    return result;
}

 

posted on 2016-05-08 13:19  我就不吃芹菜  阅读(302)  评论(0编辑  收藏  举报