zoukankan      html  css  js  c++  java
  • java操作mysql存储过程示例的介绍

    mysql存储过程每个DBA都需要掌握的,下面就为您介绍java操作mysql存储过程的例子,如果您对mysql存储过程方面感兴趣的话,不妨一看。

      1、新建表test

      以下是代码片段:

      create table test(

      field1 int not null

      TYPE=MyISAM ;

      insert into test(field1) values(1);

      2、删除已存在的存储过程

      -- 删除储存过程

      以下是代码片段:

      delimiter // -- 定义结束符号

      drop procedure p_test;//

      3、mysql存储过程定义

      以下是代码片段:

      create procedure p_test()

      begin

      declare temp int;

      set temp = 0;

      update test set field1 =temp;

      end

      //

      4、调用方法

      以下是代码片段:

      call p_test();

      import java.sql.*;

      public class Test Conn{

      private Connection getConn(){

      Connection conn = null;

      try {

      Class.forName("org.gjt.mm.mysql.Driver");

      try {

      conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&

      characterEncoding=GBK","root","ntsky");

      } catch (SQLException e1) {

      e1.printStackTrace();

      }

      }

      catch (ClassNotFoundException e) {

      e.printStackTrace();

      }

      return conn;

      }

      public void testC() {

      Connection conn = getConn();

      ResultSet rs = null;

      CallableStatement cs = null;

      String a = null;

      try {

      CallableStatement cStmt = conn.prepareCall("{call p_test()}");

      cStmt.executeUpdate();

      } catch (Exception e) {

      System.out.println("hahad" + e.getMessage());

      } finally {

      try {

      conn.close();

      } catch (Exception ex) {

      System.out.println("ex : " + ex.getMessage());

      }

      }

      }

      public static void main(String[] args) {

      new TestConn().testC();

      }

      }

      以上java操作mysql存储过程示例的介绍。

     

  • 相关阅读:
    Web安全学习计划
    机器学习资源大全
    推荐引擎的学习资料
    《Servlet与JSP核心编程》读书笔记
    Application "org.eclipse.ui.ide.workbench" could not be found in the registry.问题的解决
    Android 百度地图开发(二)
    Linux系统编程_1_文件夹读取(实现简单ls命令)
    Bootstrap 模态框、轮播 结合使用
    cocos2d-x 3.0游戏实例学习笔记 《跑酷》移植到android手机
    Unique Binary Search Trees II
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7481258.html
Copyright © 2011-2022 走看看