`

JAVA调用MYSQL存储过程

    博客分类:
  • java
阅读更多

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

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cdh1213/archive/2010/05/30/5635292.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics