JAVA调用MYSQL存储过程

Java调用MySQL存储过程

 

工程视图:

 

 

 

代码清单:

 

myconn.java

 

 

[c-sharp]  view plain  copy
 print ?
  1. package org.apache.sh_mysql.test;  
  2.   
  3. import java.sql.*;  
  4.   
  5. public class MyConn {  
  6.       
  7.     private static final String DRIVER = "com.mysql.jdbc.Driver";  
  8.     private static final String URL = "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8";  
  9.     private static final String USER = "root";  
  10.     private static final String PASSWORD ="";  
  11.   
  12.     static {  
  13.         try {  
  14.             Class.forName(DRIVER);  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
  19.   
  20.     /** 
  21.      * 获取连接 
  22.      *  
  23.      * @return 
  24.      * @throws Exception 
  25.      */  
  26.     public Connection getConnection() throws Exception {  
  27.         return DriverManager.getConnection(URL, USER, PASSWORD);  
  28.     }  
  29.   
  30.     /** 
  31.      * 释放资源 
  32.      *  
  33.      * @param rs 
  34.      * @param stmt 
  35.      * @param conn 
  36.      */  
  37.     public void close(ResultSet rs, CallableStatement stmt, Connection conn) {  
  38.         try{  
  39.             if (rs != null) {  
  40.                 rs.close();  
  41.             }  
  42.             if (stmt != null) {  
  43.                 stmt.close();  
  44.             }  
  45.             if (conn != null) {  
  46.                 conn.close();  
  47.             }  
  48.         }catch(Exception e){  
  49.             e.printStackTrace();  
  50.         }  
  51.     }  
  52.   
  53. }  

 

 

代码清单:

 

mytest.java

 

 

[c-sharp]  view plain  copy
 print ?
  1. package org.apache.sh_mysql.test;  
  2.   
  3. import java.sql.*;  
  4.   
  5. public class MyTest {  
  6.   
  7.     MyConn c = new MyConn();  
  8.       
  9.     //带单个返回值存储过程调用  
  10.     public void handleSoleData() {  
  11.   
  12.         try {  
  13.   
  14.             Connection conn = c.getConnection();  
  15.             CallableStatement call = conn  
  16.                     .prepareCall("{call pro_stu_count(?)}");  
  17.             call.registerOutParameter(1, Types.INTEGER);  
  18.             call.execute();  
  19.             int count = call.getInt(1);  
  20.             System.out.println(count);  
  21.   
  22.         } catch (Exception e) {  
  23.   
  24.             e.printStackTrace();  
  25.         }  
  26.   
  27.     }  
  28.   
  29.     //带多个返回值存储过程调用  
  30.     public void handleBothData() {  
  31.   
  32.         try {  
  33.             Connection conn=c.getConnection();  
  34.             CallableStatement call=conn.prepareCall("call pro_vi()");   
  35.             call.execute();  
  36.               
  37.             ResultSet rst=call.getResultSet();  
  38.             while(rst.next())  
  39.             {  
  40.                 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));  
  41.                   
  42.             }             
  43.               
  44.         } catch (Exception e) {  
  45.   
  46.             e.printStackTrace();  
  47.         }  
  48.   
  49.     }  
  50.   
  51.     public static void main(String[] args) {  
  52.         MyTest t = new MyTest();  
  53. //        t.handleSoleData();  
  54. //        t.handleBothData();  
  55.   
  56.     }  
  57.   
  58. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值