当前位置:Gxlcms > 数据库问题 > 使用JDBC调用存储过程

使用JDBC调用存储过程

时间:2021-07-01 10:21:17 帮助过:2人阅读

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;
}

 

使用JDBC调用存储过程

标签:

人气教程排行