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存储过程示例的介绍。

 

原文地址:https://www.cnblogs.com/liuzhuqing/p/7481258.html