java调用存储过程 mysql_JAVA调用MYSQL存储过程

JAVA调用MYSQL存储过程

工程视图:

代码清单:

myconn.java

view plaincopy to clipboardprint?

package org.apache.sh_mysql.test;

import java.sql.*;

public class MyConn {

private static final String DRIVER = "com.mysql.jdbc.Driver";

private static final String URL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";

private static final String USER = "root";

private static final String PASSWORD ="";

static {

try {

Class.forName(DRIVER);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 获取连接

*

* @return

* @throws Exception

*/

public Connection getConnection() throws Exception {

return DriverManager.getConnection(URL, USER, PASSWORD);

}

/**

* 释放资源

*

* @param rs

* @param stmt

* @param conn

*/

public void close(ResultSet rs, CallableStatement stmt, Connection conn) {

try{

if (rs != null) {

rs.close();

}

if (stmt != null) {

stmt.close();

}

if (conn != null) {

conn.close();

}

}catch(Exception e){

e.printStackTrace();

}

}

}

package org.apache.sh_mysql.test;

import java.sql.*;

public class MyConn {

private static final String DRIVER = "com.mysql.jdbc.Driver";

private static final String URL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";

private static final String USER = "root";

private static final String PASSWORD ="";

static {

try {

Class.forName(DRIVER);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 获取连接

*

* @return

* @throws Exception

*/

public Connection getConnection() throws Exception {

return DriverManager.getConnection(URL, USER, PASSWORD);

}

/**

* 释放资源

*

* @param rs

* @param stmt

* @param conn

*/

public void close(ResultSet rs, CallableStatement stmt, Connection conn) {

try{

if (rs != null) {

rs.close();

}

if (stmt != null) {

stmt.close();

}

if (conn != null) {

conn.close();

}

}catch(Exception e){

e.printStackTrace();

}

}

}

代码清单:

mytest.java

view plaincopy to clipboardprint?

package org.apache.sh_mysql.test;

import java.sql.*;

public class MyTest {

MyConn c = new MyConn();

//带单个返回值存储过程调用

public void handleSoleData() {

try {

Connection conn = c.getConnection();

CallableStatement call = conn

.prepareCall("{call pro_stu_count(?)}");

call.registerOutParameter(1, Types.INTEGER);

call.execute();

int count = call.getInt(1);

System.out.println(count);

} catch (Exception e) {

e.printStackTrace();

}

}

//带多个返回值存储过程调用

public void handleBothData() {

try {

Connection conn=c.getConnection();

CallableStatement call=conn.prepareCall("call pro_vi()");

call.execute();

ResultSet rst=call.getResultSet();

while(rst.next())

{

System.out.println(rst.getInt(1)+"\t"+rst.getString(2)+"\t"+rst.getInt(3)+"\t"+rst.getString(4)+"\t"+rst.getDate(5)+"\t"+rst.getString(6));

}

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

MyTest t = new MyTest();

//        t.handleSoleData();

//        t.handleBothData();

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2010-06-21 23:58

浏览 1640

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值