查看: 2033|回复: 0
收起左侧

[数据库部分] 201、 用JDBC如何调用存储过程

[复制链接]

566

主题

713

帖子

3823

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3823
发表于 2016-9-9 18:24:11 | 显示全部楼层 |阅读模式
201、 用JDBC如何调用存储过程

代码如下:

package net.cfei.framework.test;



import java.sql.CallableStatement;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Types;



public class JdbcTest {



/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection cn = null;

CallableStatement cstmt = null;

try {

//这里最好不要这么干,因为驱动名写死在程序中了

Class.forName("com.mysql.jdbc.Driver");

//实际项目中,这里应用DataSource数据,如果用框架,

//这个数据源不需要我们编码创建,我们只需Datasource ds = context.lookup()

//cn = ds.getConnection();

cn = DriverManager.getConnection("jdbc:mysql:///test","root","root");

cstmt = cn.prepareCall("{call insert_Student(?,?,?)}");

cstmt.registerOutParameter(3,Types.INTEGER);

cstmt.setString(1, "wangwu");

cstmt.setInt(2, 25);

cstmt.execute();

//get第几个,不同的数据库不一样,建议不写

System.out.println(cstmt.getString(3));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally

{



/*try{cstmt.close();}catch(Exception e){}

try{cn.close();}catch(Exception e){}*/

try {

if(cstmt != null)

cstmt.close();

if(cn != null)

cn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


来自安卓客户端来自安卓客户端
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 打开微信扫一扫