设为首页 加入收藏

TOP

mysql创建存储过程并通过java程序调用该存储过程(一)
2015-07-24 11:33:03 来源: 作者: 【 】 浏览:14
Tags:mysql 创建 存储 过程 通过 java 程序 调用
create table users_ning(id primary key auto_increment,pwd int);
 insert into users_ning values(id,1234);
  insert into users_ning values(id,12345);
 insert into users_ning values(id,12);
 insert into users_ning values(id,123);

  CREATE  PROCEDURE login_ning(IN p_id int,IN p_pwd int,OUT flag int)
BEGIN
DECLARE	v_pwd int;
  select pwd INTO v_pwd from users_ning
  where id = p_id;
 if v_pwd = p_pwd then
      
set flag:=1;

  else 
select v_pwd;
  set flag := 0;
  end if;
END 

package demo20130528;
import java.sql.*;

import demo20130526.DBUtils;

/**
 * 测试JDBC API调用过程
 * @author tarena
 *
 */
public class ProcedureDemo2 {

  /**
   * @param args
 * @throws Exception 
   */
  public static void main(String[] args) throws Exception {
    System.out.println(login(123, 1234));
  }
  /**
   * 调用过程,实现登录功能
   * @param id 考生id
   * @param pwd 考试密码
   * @return if成功:1; if密码错:0; if没有用户:-1
 * @throws Exception 
   */
  public static int login(int id, int pwd) throws Exception{
    int flag = -1;
    String sql = "{call login_ning(?,?,?)}";//*****
    Connection conn = DBUtils.getConnMySQL();
    CallableStatement stmt = null;
    try{
      stmt = conn.prepareCall(sql);
      //传递输入参数
      stmt.setInt(1, id);
      stmt.setInt(2, pwd);
      //注册输出参数,第三个占位符的数据类型是整型
      stmt.registerOutParameter(3, Types.INTEGER);//*****
      //执行过程
      stmt.execute();
      //获得过程执行后的输出参数
      flag = stmt.getInt(3);//*****
      
    }catch(Exception e){
      e.printStackTrace();
    }finally{
    stmt.close();
    DBUtils.dbClose();
    }    
    
    return flag;
  }

}
 
 
 
 
package demo20130526;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class DBUtils {
	static Connection conn = null;
	static PreparedStatement stmt = null;
	static ResultSet rs = null;
	static Statement st = null;
	static String username = null;
	static String password = null;
	static String url = null;
	static String driverName = null;


	public static Connection getConnMySQL() throws Exception {// 连接mysql 返回conn
		getUrlUserNamePassWordClassNameMySQL();
		conn = DriverManager.getConnection(url, username, password);
		// conn.setAutoCommit(false);设置自动提交为false
		return conn;
	}


	public static Connection getConnORCALE() throws Exception {// 连接orcale
																// 返回conn
		getUrlUserNamePassWordClassNameORCALE();
		conn = DriverManager.getConnection(url, username, password);
		// conn.setAutoCommit(false);
		return conn;
	}


	private static void getUrlUserNamePassWordClassNameORCALE()
			throws Exception {
		// 从资源文件 获取 orcale的username password url等信息
		Properties pro = new Properties();
		File path = new File("src/all.properties");
		pro.load(new FileInputStream(path));
		String paths = pro.getProperty("filepath");
		File file = new File(paths + "orcale.properties");
		getFromProperties(file);


	}


	public static void getUrlUserNamePassWordClassNameMySQL() throws Exception {
		// 从资源文件 获取mysql的username p
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MySQL日期类型的'0000-00-00&.. 下一篇[MySQL参数取值] Status取值ERROR..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·nginx 监听一个端口 (2025-12-25 00:19:30)
·整个互联网就没有一 (2025-12-25 00:19:27)
·囧次元最新官网入口 (2025-12-25 00:19:24)
·如何利用Python做数 (2025-12-24 23:48:36)
·如何使用python进行 (2025-12-24 23:48:34)