工作中一直都是用swing,很少接触数据库,最近事少就帮着把公司服务器用csv存储用户资料信息,转移到mysql中去存储,才得以学习下mysql的存储过程。
      首先在mysql中练习下存储过程的小例子:   
 mysql> delimiter //
mysql> delimiter //
 mysql> create procedure hello()
mysql> create procedure hello()
 -> begin
    -> begin
 -> select 'It is not a HelloWorld';
    -> select 'It is not a HelloWorld';
 -> end
    -> end
 -> //
    -> //
 Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
在mysql中查询上面的过程hello():
 mysql> call hello()//
mysql> call hello()//
 +------------------------+
+------------------------+
 | It is not a HelloWorld |
| It is not a HelloWorld |
 +------------------------+
+------------------------+
 | It is not a HelloWorld |
| It is not a HelloWorld |
 +------------------------+
+------------------------+
 1 row in set (0.00 sec)
1 row in set (0.00 sec)
建立一个简单的测试用表:
 mysql> DROP TABLE IF EXISTS `userinfo`.`mapping`;
mysql> DROP TABLE IF EXISTS `userinfo`.`mapping`;
 -> CREATE TABLE  `userinfo`.`mapping` (
    -> CREATE TABLE  `userinfo`.`mapping` (
 ->   `cFieldID` smallint(5) unsigned NOT NULL,
    ->   `cFieldID` smallint(5) unsigned NOT NULL,
 ->   `cFieldName` varchar(30) NOT NULL,
    ->   `cFieldName` varchar(30) NOT NULL,
 ->   PRIMARY KEY  (`cFieldID`)
    ->   PRIMARY KEY  (`cFieldID`)
 -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 -> //
    -> //
 Query OK, 0 rows affected (0.14 sec)
Query OK, 0 rows affected (0.14 sec)
向table mapping中插入一些初始化的数据:
 mysql> load data infile 'd:\\userInfo\\field.txt' into table mapping
mysql> load data infile 'd:\\userInfo\\field.txt' into table mapping
 -> fields terminated by ',' lines terminated by '\r\n' //
    -> fields terminated by ',' lines terminated by '\r\n' //
 Query OK, 5 rows affected (0.02 sec)
Query OK, 5 rows affected (0.02 sec)
 Records: 5  Deleted: 0  Skipped: 0  Warnings: 0
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0
 mysql> select *from mapping//
mysql> select *from mapping//
 +----------+-------------+
+----------+-------------+
 | cFieldID | cFieldName  |
| cFieldID | cFieldName  |
 +----------+-------------+
+----------+-------------+
 |        1 | MarketValue |
|        1 | MarketValue |
 |        2 | P/L         |
|        2 | P/L         |
 |        3 | EName       |
|        3 | EName       |
 |        4 | Nominal     |
|        4 | Nominal     |
 |        5 | Chg         |
|        5 | Chg         |
 +----------+-------------+
+----------+-------------+
 5 rows in set (0.02 sec)
5 rows in set (0.02 sec)
现在简历一个向mapping中插入一条记录并返回记录的总和
 mysql> drop procedure if exists mappingProc;
mysql> drop procedure if exists mappingProc;
 ->  create procedure mappingProc(out cnt int)
    ->  create procedure mappingProc(out cnt int)
 ->  begin
    ->  begin
 ->  declare maxid int;
    ->  declare maxid int;
 ->  select max(cFieldID)+1 into maxid from mapping;
    ->  select max(cFieldID)+1 into maxid from mapping;
 ->  insert into mapping(cFieldID,cFieldName) values(maxid,'hello');
    ->  insert into mapping(cFieldID,cFieldName) values(maxid,'hello');
 ->  select count(cFieldID) into cnt from mapping;
    ->  select count(cFieldID) into cnt from mapping;
 ->  end
    ->  end
 ->  //
    ->  //
查找mappingProc():
 mysql> call mappingProc(@a)//
mysql> call mappingProc(@a)//
 mysql> select @a//
mysql> select @a//
 +------+
+------+
 | @a   |
| @a   |
 +------+
+------+
 | 6    |
| 6    |
 +------+
+------+
 mysql> select * from mapping//
mysql> select * from mapping//
 +----------+-------------+
+----------+-------------+
 | cFieldID | cFieldName  |
| cFieldID | cFieldName  |
 +----------+-------------+
+----------+-------------+
 |        1 | MarketValue |
|        1 | MarketValue |
 |        2 | P/L                 |
|        2 | P/L                 |
 |        3 | EName          |
|        3 | EName          |
 |        4 | Nominal     |
|        4 | Nominal     |
 |        5 | Chg         |
|        5 | Chg         |
 |        6 | hello       |
|        6 | hello       |
 +----------+-------------+
+----------+-------------+
下面是java代码用来调用MySQL的存储过程:
 package kissJava.sql;
package kissJava.sql;
 import java.sql.CallableStatement;
import java.sql.CallableStatement;
 import java.sql.Connection;
import java.sql.Connection;
 import java.sql.DriverManager;
import java.sql.DriverManager;
 import java.sql.SQLException;
import java.sql.SQLException;
 import java.sql.Types;
import java.sql.Types;

 public class SQLUtils
public class SQLUtils  {
{
 String url = "jdbc:mysql://127.0.0.1:3306/userInfo";
    String url = "jdbc:mysql://127.0.0.1:3306/userInfo"; 
 String userName = "root";
    String userName = "root";
 String password = "zhui007";
    String password = "zhui007";

 public Connection getConnection()
    public Connection getConnection()  {
{
 Connection con=null;
        Connection con=null;

 try
        try {
{
 DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
 con = DriverManager.getConnection(url, this.userName, this.password);
            con = DriverManager.getConnection(url, this.userName, this.password);

 }catch(SQLException sw)
        }catch(SQLException sw) {
{ 
 }
         }
 return con;
        return con;
 }
    }

 public void testProc()
    public void testProc() {
{
 Connection conn = getConnection();
        Connection conn = getConnection();
 CallableStatement stmt = null;
        CallableStatement stmt = null;

 try
        try {
{
 stmt = conn.prepareCall("{call mappingProc(?)}");
            stmt = conn.prepareCall("{call mappingProc(?)}");    
 stmt.registerOutParameter(1, Types.INTEGER);
            stmt.registerOutParameter(1, Types.INTEGER);
 stmt.execute();
            stmt.execute();
 int i= stmt.getInt(1);
            int i= stmt.getInt(1);
 System.out.println("count = " + i);
            System.out.println("count = " + i);

 }catch(Exception e)
        }catch(Exception e) {
{
 System.out.println("hahad = "+e.toString());
            System.out.println("hahad = "+e.toString());

 }finally
        }finally {
{

 try
            try  {
{
 stmt.close();
                stmt.close();
 conn.close();
                conn.close();

 }catch (Exception ex)
            }catch (Exception ex)  {
{
 System.out.println("ex : "+ ex.getMessage());
                System.out.println("ex : "+ ex.getMessage());
 }
            }
 }
        }
 }
    }

 public static void main(String[] args)
    public static void main(String[] args)  {
{
 new SQLUtils().testProc();
        new SQLUtils().testProc();
 }
    }
 }
}
在到MySQL中查询可看到插入一条新的记录