CallableStatement 调用存贮过程

 代码:

DButil.java

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.mysql.jdbc.Connection;

public class DButil {
		/*
		 * 打开数据库
		 */
	    private static String driver;//连接数据库的驱动
	    private static String url;
	    private static String username;
	    private static String password;
	    
	    static {
	    	driver="com.mysql.jdbc.Driver";//需要的数据库驱动
	    	url="jdbc:mysql://localhost:3306/test";//数据库名路径
	    	username="root";
	    	password="root";
	    }
		public static Connection open()
		{
			try {
				Class.forName(driver);
				return (Connection) DriverManager.getConnection(url,username, password);
			} catch (Exception e) {
				System.out.println("数据库连接失败!");
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//加载驱动

			return null;
		}
        
		/*
		 * 关闭数据库
		 */
		public static void close(Connection conn)
		{
			if(conn!=null)
			{
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

}

   Tesgt.java

import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Connection;

public class Tesgt {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        test2();
	}

	static void test1()
	{
		Connection conn=DButil.open();
		try {
			CallableStatement cstmt=(CallableStatement) conn.prepareCall("{call all_customers()}");//查询存贮过程
			ResultSet rs=cstmt.executeQuery();
			while(rs.next())
			{
				int id=rs.getInt(1);
				String name=rs.getString(2);
				String email=rs.getString(3);
				System.out.println(id+","+name+","+email);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static void test2()
	{
		Connection conn=DButil.open();
		try {
			CallableStatement cstmt=(CallableStatement) conn.prepareCall("{call insert_customers(?,?)}");//查询插入数据的存贮过程
			cstmt.setString(1,"haige");
			cstmt.setString(2,"8512@qq.com");
			int r=cstmt.executeUpdate();
			System.out.println(r);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值