分享

mysql 创建存储过程 java程序调用该存储过程

 langqy 2015-06-25
  1. <pre name="code" class="html">  </pre><pre name="code" class="html"></pre><pre name="code" class="html">CREATE TABLE SCOTT.USERS_NING   
  2.    (    ID NUMBER,   
  3.     PWD NUMBER  
  4.    );  
  5.    
  6. insert into users_ning values(1,1234);  
  7. insert into users_ning values(123,1234);  
  8.   CREATE  PROCEDURE login_ning(IN p_id int,IN p_pwd int,OUT flag int)  
  9.     BEGIN  
  10.      DECLARE v_pwd int;  
  11.       select pwd INTO v_pwd from users_ning  
  12.        where id = p_id;  
  13.         if v_pwd = p_pwd then     
  14.             set flag:=1;  
  15.         else   
  16.             set flag := 0;  
  17.         end if;  
  18.    END   
  19. package demo20130528;  
  20. import java.sql.*;  
  21.   
  22. import demo20130526.DBUtils;  
  23.   
  24. /** 
  25.  * 测试JDBC API调用过程 
  26.  * @author tarena 
  27.  * 
  28.  */  
  29. public class ProcedureDemo2 {  
  30.   
  31.   /** 
  32.    * @param args 
  33.  * @throws Exception  
  34.    */  
  35.   public static void main(String[] args) throws Exception {  
  36.     System.out.println(login(1231234));  
  37.   }  
  38.   /** 
  39.    * 调用过程,实现登录功能 
  40.    * @param id 考生id 
  41.    * @param pwd 考试密码 
  42.    * @return if成功:1; if密码错:0; if没有用户:-1 
  43.  * @throws Exception  
  44.    */  
  45.   public static int login(int id, int pwd) throws Exception{  
  46.     int flag = -1;  
  47.     String sql = "{call login_ning(?,?,?)}";//*****  
  48.     Connection conn = DBUtils.getConnMySQL();  
  49.     CallableStatement stmt = null;  
  50.     try{  
  51.       stmt = conn.prepareCall(sql);  
  52.       //传递输入参数  
  53.       stmt.setInt(1, id);  
  54.       stmt.setInt(2, pwd);  
  55.       //注册输出参数,第三个占位符的数据类型是整型  
  56.       stmt.registerOutParameter(3, Types.INTEGER);//*****  
  57.       //执行过程  
  58.       stmt.execute();  
  59.       //获得过程执行后的输出参数  
  60.       flag = stmt.getInt(3);//*****  
  61.         
  62.     }catch(Exception e){  
  63.       e.printStackTrace();  
  64.     }finally{  
  65.     stmt.close();  
  66.     DBUtils.dbClose();  
  67.     }  
  68.       
  69.       
  70.     return flag;  
  71.   }  
  72.   
  73. }  
  74.   
  75. package demo20130526;  
  76.   
  77. import java.io.File;  
  78. import java.io.FileInputStream;  
  79. import java.io.FileNotFoundException;  
  80. import java.io.IOException;  
  81. import java.sql.Connection;  
  82. import java.sql.DatabaseMetaData;  
  83. import java.sql.DriverManager;  
  84. import java.sql.PreparedStatement;  
  85. import java.sql.ResultSet;  
  86. import java.sql.ResultSetMetaData;  
  87. import java.sql.SQLException;  
  88. import java.sql.Statement;  
  89. import java.util.Properties;  
  90.   
  91. public class DBUtils {  
  92.     static Connection conn = null;  
  93.     static PreparedStatement stmt = null;  
  94.     static ResultSet rs = null;  
  95.     static Statement st = null;  
  96.     static String username = null;  
  97.     static String password = null;  
  98.     static String url = null;  
  99.     static String driverName = null;  
  100.   
  101.     public static Connection getConnMySQL() throws Exception {// 连接mysql 返回conn  
  102.         getUrlUserNamePassWordClassNameMySQL();  
  103.         conn = DriverManager.getConnection(url, username, password);  
  104.         // conn.setAutoCommit(false);设置自动提交为false  
  105.         return conn;  
  106.     }  
  107.   
  108.     public static Connection getConnORCALE() throws Exception {// 连接orcale  
  109.                                                                 // 返回conn  
  110.         getUrlUserNamePassWordClassNameORCALE();  
  111.         conn = DriverManager.getConnection(url, username, password);  
  112.         // conn.setAutoCommit(false);  
  113.         return conn;  
  114.     }  
  115.   
  116.     private static void getUrlUserNamePassWordClassNameORCALE()  
  117.             throws Exception {  
  118.         // 从资源文件 获取 orcale的username password url等信息  
  119.         Properties pro = new Properties();  
  120.         File path = new File("src/all.properties");  
  121.         pro.load(new FileInputStream(path));  
  122.         String paths = pro.getProperty("filepath");  
  123.         File file = new File(paths + "orcale.properties");  
  124.         getFromProperties(file);  
  125.   
  126.     }  
  127.   
  128.     public static void getUrlUserNamePassWordClassNameMySQL() throws Exception {  
  129.         // 从资源文件 获取mysql的username password url等信息  
  130.         Properties pro = new Properties();  
  131.         File path = new File("src/all.properties");  
  132.         pro.load(new FileInputStream(path));  
  133.         String paths = pro.getProperty("filepath");  
  134.         File file = new File(paths + "mysql.properties");  
  135.         getFromProperties(file);  
  136.     }  
  137.   
  138.     public static void getFromProperties(File file) throws IOException,  
  139.             FileNotFoundException, ClassNotFoundException {// 读资源文件的内容  
  140.         Properties pro = new Properties();  
  141.         pro.load(new FileInputStream(file));  
  142.         username = pro.getProperty("username");  
  143.         password = pro.getProperty("password");  
  144.         url = pro.getProperty("url");  
  145.         driverName = pro.getProperty("driverName");  
  146.         Class.forName(driverName);  
  147.     }  
  148.   
  149.     public static void dbClose() throws Exception {// 关闭所有  
  150.         if (rs != null)  
  151.             rs.close();  
  152.         if (st != null)  
  153.             st.close();  
  154.         if (stmt != null)  
  155.             stmt.close();  
  156.         if (conn != null)  
  157.             conn.close();  
  158.     }  
  159.   
  160.     public static ResultSet getById(String tableName, int id) throws Exception {// 用id来查询结果  
  161.         st = conn.createStatement();  
  162.         rs = st.executeQuery("select * from " + tableName + "  where id=" + id  
  163.                 + " ");  
  164.         return rs;  
  165.     }  
  166.   
  167.     public static ResultSet getByAll(String sql, Object... obj)  
  168.             throws Exception {// 用关键字 实现查询 关键字额可以任意  
  169.         sql = sql.replaceAll(";""");  
  170.         sql = sql.trim();  
  171.         stmt = conn.prepareStatement(sql);  
  172.         String[] strs = sql.split("\\?");// 将sql 以? 非开  
  173.         int num = strs.length;// 得到?的个数  
  174.         int size = obj.length;  
  175.         for (int i = 1; i <= size; i++) {  
  176.             stmt.setObject(i, obj[i - 1]);// 数组下标从0开始  
  177.         }  
  178.         if (size < num) {  
  179.             for (int k = size + 1; k <= num; k++) {  
  180.                 stmt.setObject(k, null);// 数组下标从0开始  
  181.             }  
  182.         }  
  183.         rs = stmt.executeQuery();  
  184.         return rs;  
  185.     }  
  186.   
  187.     public static void doInsert(String sql) throws SQLException {// 传入 sql 语句  
  188.                                                                     // 实现插入操作  
  189.         st = conn.createStatement();  
  190.         st.execute(sql);  
  191.     }  
  192.   
  193.     public static void doInsert(String sql, Object... args) throws Exception {// 传入参数  
  194.                                                                                 // 利用  
  195.                                                                                 // PreparedStatement  
  196.                                                                                 // 实现插入  
  197.         // 传入的参数是任意多个 因为有Object 。。。args  
  198.         int size = args.length;// 获得 Object ...obj 传过来的参数的个数  
  199.         stmt = conn.prepareStatement(sql);  
  200.         for (int i = 1; i <= size; i++) {  
  201.             stmt.setObject(i, args[i - 1]);// 数组下标从0开始  
  202.         }  
  203.         stmt.execute();  
  204.     }  
  205.   
  206.     public static int doUpdate(String sql) throws Exception {// 传入 sql 实现更新操作  
  207.         st = conn.createStatement();  
  208.         int num = st.executeUpdate(sql);  
  209.         return num;  
  210.     }  
  211.   
  212.     public static void doUpdate(String sql, Object... obj) throws Exception {  
  213.         // 传入参数 利用 PreparedStatement实现更新  
  214.         // 传入的参数是任意多个 因为有Object 。。。args  
  215.         int size = obj.length;// 获得 Object ...obj 传过来的参数的个数  
  216.         stmt = conn.prepareStatement(sql);  
  217.         for (int i = 1; i <= size; i++) {  
  218.             stmt.setObject(i, obj[i - 1]);// 数组下标从0开始  
  219.         }  
  220.         stmt.executeUpdate(sql);  
  221.     }  
  222.   
  223.     public static boolean doDeleteById(String tableName, int id)  
  224.             throws SQLException {// 删除记录 by id  
  225.         st = conn.createStatement();  
  226.         boolean b = st.execute("delete from " + tableName + " where id=" + id  
  227.                 + "");  
  228.         return b;  
  229.     }  
  230.   
  231.     public static boolean doDeleteByAll(String sql, Object... args)  
  232.             throws SQLException {// 删除记录 可以按任何关键字  
  233.         sql = sql.replaceAll(";""");  
  234.         sql = sql.trim();  
  235.         stmt = conn.prepareStatement(sql);  
  236.         String[] strs = sql.split("\\?");// 将sql 以? 非开  
  237.         int num = strs.length;// 得到?的个数  
  238.         int size = args.length;  
  239.         for (int i = 1; i <= size; i++) {  
  240.             stmt.setObject(i, args[i - 1]);// 数组下标从0开始  
  241.         }  
  242.         if (size < num) {  
  243.             for (int k = size + 1; k <= num; k++) {  
  244.                 stmt.setObject(k, null);// 数组下标从0开始  
  245.             }  
  246.         }  
  247.         boolean b = stmt.execute();  
  248.         return b;  
  249.     }  
  250.   
  251.     public static void getMetaDate() throws Exception {// 获取数据库元素数据  
  252.         conn = DBUtils.getConnORCALE();  
  253.         DatabaseMetaData dmd = conn.getMetaData();  
  254.         System.out.println(dmd.getDatabaseMajorVersion());  
  255.         System.out.println(dmd.getDatabaseProductName());  
  256.         System.out.println(dmd.getDatabaseProductVersion());  
  257.         System.out.println(dmd.getDatabaseMinorVersion());  
  258.     }  
  259.   
  260.     public static String[] getColumnNamesFromMySQL(String sql) throws Exception {  
  261.         conn = DBUtils.getConnMySQL();  
  262.         return getColumnName(sql);  
  263.   
  264.     }  
  265.   
  266.     public static String[] getColumnNamesFromOrcale(String sql)  
  267.             throws Exception {  
  268.         conn = DBUtils.getConnORCALE();  
  269.         return getColumnName(sql);  
  270.   
  271.     }  
  272.   
  273.     private static String[] getColumnName(String sql) throws Exception {// 返回表中所有的列名  
  274.         conn = DBUtils.getConnORCALE();  
  275.         st = conn.createStatement();  
  276.         rs = st.executeQuery(sql);  
  277.         ResultSetMetaData rsmd = rs.getMetaData();  
  278.         int num = rsmd.getColumnCount();  
  279.         System.out.println("ColumnCount=" + num);  
  280.         String[] strs = new String[num];  
  281.         // 显示列名  
  282.         for (int i = 1; i <= rsmd.getColumnCount(); i++) {  
  283.             String str = rsmd.getColumnName(i);  
  284.             strs[i - 1] = str;  
  285.             System.out.print(str + "\t");  
  286.         }  
  287.         return strs;  
  288.     }  
  289.   
  290.     public static void getColumnDataFromMySQL(String sql) throws Exception {// 输出表中的数据  
  291.         conn = DBUtils.getConnMySQL();  
  292.         getColumnData(sql);  
  293.     }  
  294.   
  295.     public static void getColumnDataFromORCALEL(String sql) throws Exception {// 输出表中的数据  
  296.         conn = DBUtils.getConnORCALE();  
  297.         getColumnData(sql);  
  298.     }  
  299.   
  300.     public static void getColumnData(String sql) throws Exception {// 输出表中的数据  
  301.         st = conn.createStatement();  
  302.         rs = st.executeQuery(sql);  
  303.         ResultSetMetaData rsmd = rs.getMetaData();  
  304.         System.out  
  305.                 .println("\n------------------------------------------------------------------------------------------------------------------------");  
  306.         while (rs.next()) {  
  307.             for (int i = 1; i <= rsmd.getColumnCount(); i++) {  
  308.                 System.out.print(rs.getString(i) + "\t");  
  309.             }  
  310.             System.out.println();  
  311.         }  
  312.         System.out  
  313.                 .println("------------------------------------------------------------------------------------------------------------------------");  
  314.   
  315.     }  
  316.   
  317.     public static void getTableDataFromOrcale(String sql) throws Exception {// 输出表的列名  
  318.                                                                             // 和表中的全部数据  
  319.         conn = DBUtils.getConnORCALE();  
  320.         getTableData(sql);  
  321.   
  322.     }  
  323.   
  324.     public static void getTableDataFromMysql(String sql) throws Exception {// 输出表的列名  
  325.                                                                             // 和表中的全部数据  
  326.         conn = DBUtils.getConnMySQL();  
  327.         getTableData(sql);  
  328.   
  329.     }  
  330.   
  331.     private static void getTableData(String sql) throws SQLException {  
  332.         // getTableDataFromMysql  
  333.         // getTableDataFromOrcale  
  334.         st = conn.createStatement();  
  335.         rs = st.executeQuery(sql);  
  336.         ResultSetMetaData rsmd = rs.getMetaData();  
  337.         int num = rsmd.getColumnCount();  
  338.         System.out.println("ColumnCount=" + num);  
  339.         String[] strs = new String[num];  
  340.         // 显示列名  
  341.         for (int i = 1; i <= rsmd.getColumnCount(); i++) {  
  342.             String str = rsmd.getColumnName(i);  
  343.             strs[i - 1] = str;  
  344.             System.out.print(str + "\t");  
  345.         }  
  346.         System.out  
  347.                 .println("\n------------------------------------------------------------------------------------------------------------------------");  
  348.         while (rs.next()) {  
  349.             for (int i = 1; i <= rsmd.getColumnCount(); i++) {  
  350.                 System.out.print(rs.getString(i) + "\t");  
  351.             }  
  352.             System.out.println();  
  353.         }  
  354.         System.out  
  355.                 .println("------------------------------------------------------------------------------------------------------------------------");  
  356.     }  
  357. }  
  358. </pre><br>  
  359. <br>  
  360. <pre></pre>  
  361. <pre name="code" class="sql"></pre><pre name="code" class="java"><pre name="code" class="java"><pre></pre>  
  362. <pre></pre>  
  363. <pre></pre>  
  364. <pre></pre>  
  365. <pre></pre>  
  366. <pre></pre>  
  367. <pre></pre>  
  368. <pre></pre>  
  369. <pre></pre>  
  370. </pre></pre> 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多