您的位置:首页 > 编程语言 > Java开发

JAVA调用MYSQL存储过程

2016-10-17 12:18 387 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。

Java调用MySQL存储过程

 

工程视图:

 



 

 

代码清单:

 

myconn.java

 

 

[c-sharp] view
plain copy

 print?

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

 

 

[c-sharp] view
plain copy

 print?

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();  

  

    }  

  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: