蒋同学关于mysql的存储过程的报告。记录一下。
Sql代码:
# MySQL-Front 3.2 (Build 14.8)
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='SYSTEM' */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */;
/*!40101 SET SQL_MODE='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES */;
/*!40103 SET SQL_NOTES='ON' */;
# Host: 192.168.0.227 Database: msn
# ------------------------------------------------------
# Server version 5.0.45-community-nt
DROP DATABASE IF EXISTS `msn`;
CREATE DATABASE `msn` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `msn`;
#
# Table structure for table test
#
CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`user` text NOT NULL,
`age` tinyint(3) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
#
# Source for procedure intest
#
delimiter //
drop procedure if exists intest //
CREATE PROCEDURE `intest`(in p varchar(50),in q int)
begin
declare m varchar(50);
declare n int;
set m=p;
set n=q;
insert into test(user,age)values(m,n);
end //
#
# Source for procedure outtest
#
delimiter //
drop procedure if exists outtest //
CREATE PROCEDURE `outtest`(out p int)
begin
select max(id) into p from test;
end //
#
# Source for procedure resulttest
#
delimiter //
drop procedure if exists resulttest //
CREATE PROCEDURE `resulttest`()
begin
select * from test;
end //
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
或者
1执行效率高
2将sql语句进行了封装
(outtest)
mysql> use msn
Database changed
mysql> delimiter //
mysql> drop procedure if exists outtest //
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create procedure outtest(out p int)
-> begin
-> select max(id) into p from test;
-> end //
Query OK, 0 rows affected (0.00 sec)
mysql> call outtest(@a) //
Query OK, 0 rows affected (0.00 sec)
mysql> select @a //
(intest)
mysql> use msn
Database changed
mysql> delimiter //
mysql> drop procedure if exists intest //
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create procedure intest(in p varchar(50),in q int)
-> begin
-> declare m varchar(50);
-> declare n int;
-> set m=p;
-> set n=q;
-> insert into test(user,age)values(m,n);
-> end //
Query OK, 0 rows affected (0.00 sec)
(resulttest)
mysql> use msn
Database changed
mysql> delimiter //
mysql> drop procedure if exists resulttest //
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create procedure resulttest()
-> begin
-> select * from test;
-> end //
Query OK, 0 rows affected (0.00 sec)
调用存储过程的java代码:
package server.proce;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.apache.log4j.Logger;
import server.log.LoggerFactory;
public class ProceJdbc {
public static void callout() {
Logger log = LoggerFactory.initLogger();
Connection conn = ConnectDb.getConnection();
CallableStatement cs = null;
ResultSet rs = null;
try {
cs = conn.prepareCall("{call outtest(?)}");
log.info(cs);
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug("call outtest() faild, with Exception:");
e.printStackTrace();
}
e.printStackTrace();
}
try {
int i = cs.getInt(1);
System.out.println(i);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (cs != null) {
cs.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 循环输出结果
}
public static void callIn(String in1, int in2) {
// 获取连接
Connection conn = ConnectDb.getConnection();
CallableStatement cs = null;
try {
// 可以直接传入参数
// cs = conn.prepareCall("{call intest(1)}");
// 也可以用问号代替
cs = conn.prepareCall("{call intest(?,?)}");
// 设置第一个输入参数的值为in
cs.setString(1, in1);
cs.setInt(2, in2);
cs.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (cs != null) {
cs.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void callResult() {
Connection conn = ConnectDb.getConnection();
CallableStatement cs = null;
ResultSet rs = null;
try {
cs = conn.prepareCall("{call resulttest()}");
rs = cs.executeQuery();
// 循环输出结果
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
public static void main(String[] args) {
// callIn("jsw", 22);
callResult();
// callout();
}
}
class ConnectDb {
public static Connection getConnection() {
Connection conn = null;
PreparedStatement preparedstatement = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String dbname = "msn";
String url = "jdbc:mysql://127.0.0.1:3306/"
+ dbname
+ "?user=root&password=admin&useUnicode=true&characterEncoding=UTF-8";
conn = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}