oracle---jdbctest--laobai

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;

import oracle.jdbc.OracleTypes;
import oracle.jdbc.oracore.OracleType;


public class Test 
{
	static void 简单的查询()
	{
		String sql="select * from emp where deptno=?";  
		String[] paras={10+""};
		ResultSet rs=JDBCUtil.doQuery(sql, paras);
		try 
		{
			while (rs.next()) 
			{
				int empno = rs.getInt("empno");
				String ename = rs.getString("ename");
				System.out.println(empno + "--" + ename);
			}
		} 
		catch (Exception e) 
		{
			System.out.println("查询异常!");
		}
		finally
		{
			JDBCUtil.close(rs);
		}
	}
	static void 简单的修改()
	{
		String sql="update emp set empno=? where ename=?";
		int result=JDBCUtil.doUpdate(sql, new String[]{"7369","老白"});
		if(result!=1)
		{
			System.out.println("修改失败!");
		}
		else
		System.out.println("修改成功!影响行数为:"+result);
	}
	static void 调用无参存储过程()
	{
		//获取链接
		Connection con=JDBCUtil.getConnection();
		//执行存储过程
		String sql="{call system.update_emp_comm()}";
		int result=0;
		try 
		{
			CallableStatement cmt = con.prepareCall(sql);
			result = cmt.executeUpdate();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			System.out.println("执行过程出现异常!");
			return;
		}
		System.out.println("执行完毕!影响行数为:"+result);
	}
	static void 调用有入参出参存储过程()
	{
		//1 获取连接
		Connection con = JDBCUtil.getConnection();
		//2 执行存储过程
		String sql = "{call update_emp_sal_by_deptno(?,?,?)}";
		int result = 0;
		try 
		{
			CallableStatement cmt = con.prepareCall(sql);
			cmt.setInt(1, 30);
			cmt.setInt(2, 2000);
			//将第3个参数注册出参
			cmt.registerOutParameter(3, OracleType.STYLE_INT);
			result = cmt.executeUpdate();
			//获取第3个参数,也就是出参,执行完毕后的值
			int rowcount = cmt.getInt(3);
			System.out.println("影响了" + rowcount + "条!");
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			System.out.println("执行过程出现异常!");
			return;
		}
		System.out.println("执行完毕,影响行数为:"+result);
	}
	static void 调用有入参的函数()
	{
		//1 获取连接
		Connection con = JDBCUtil.getConnection();
		//2 执行函数
		String sql ="{? = call delete_emp_by_empname(?)}";
		int result = 0;
		try 
		{
			CallableStatement cmt = con.prepareCall(sql);
			//将函数的返回值,当成出参来注册
			cmt.registerOutParameter(1, OracleType.STYLE_INT);
			cmt.setString(2, "白");
			result = cmt.executeUpdate();
			//获取第3个参数,也就是出参,执行完毕后的值
			int rowcount = cmt.getInt(1);
			System.out.println("删除了" + rowcount + "条!");
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			System.out.println("执行过程出现异常!");
			return;
		}
		System.out.println(result);
		System.out.println("执行完毕!");
	}
	static void 调用有返回游标的函数()
	{
		//1 获取连接
		Connection con = JDBCUtil.getConnection();
		//2 执行函数
		String sql ="{?=call get_max_min_sal_by_group()}";
		int result = 0;
		ResultSet rs = null;
		try {
			CallableStatement cmt = con.prepareCall(sql);
			//将函数的返回值,当成出参来注册
			cmt.registerOutParameter(1, OracleTypes.CURSOR);
			result = cmt.executeUpdate();
			//获取第1个参数,也就是返回值,即游标,即resultset
			rs = (ResultSet) cmt.getObject(1);
			while (rs.next()) {
				int deptno = rs.getInt("部门号");
				float max_sal = rs.getFloat("最高工资");
				float min_sal = rs.getFloat("最低工资");
				System.out.println(deptno + ",最高工资" + max_sal + ",最低工资"
						+ min_sal);
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			System.out.println("执行过程出现异常!");
			return;
		}
		finally
		{
			JDBCUtil.close(rs); //关闭游标
		}
		System.out.println(result);
		System.out.println("执行完毕!");
	}
	public static void testProcedure()
	{
		String sql="{call queryEmpInfo(?,?,?,?)}";
		Connection conn=null;
		CallableStatement call=null;
		try 
		{
			conn = JDBCUtil.getConnection();
			call = conn.prepareCall(sql);
			//赋值
			call.setInt(1, 7839);
			//对于out参数,申明
			call.registerOutParameter(2, OracleTypes.VARCHAR);
			call.registerOutParameter(3, OracleTypes.NUMBER);
			call.registerOutParameter(4, OracleTypes.VARCHAR);
			//调用
			call.execute();
			//取出结果
			String name = call.getNString(2);
			double sal = call.getDouble(3);
			String job = call.getString(4);
			System.out.println(name);
			System.out.println(sal);
			System.out.println(job);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	public static void testFunction()
	{
		String sql = "{?=call queryEmpIncome(?)}";
		Connection conn = null;
		CallableStatement call = null;
		try
		{conn = JDBCUtil.getConnection();
		call = conn.prepareCall(sql);
		call.registerOutParameter(1, OracleTypes.NUMBER);
		call.setInt(2, 7839);
		//执行  
		call.execute();
		//取出年收入  
		double income = call.getDouble(1);
		System.out.println(income);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	public static void main(String[] args) 
	{
		//简单的查询();
		//简单的修改();
		//调用无参存储过程();
		//调用有入参出参存储过程();
		//调用有入参的函数();
		//调用有返回游标的函数();
		//testProcedure();
		testFunction();
	}

}

  

转载于:https://www.cnblogs.com/ipetergo/p/6251025.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您介绍一下JDBC技术的应用案例。 JDBCJava Database Connectivity的缩写,是Java EE平台中用于访问数据库的一种标准。下面以一个简单的数据库开发基础案例为例,介绍JDBC技术的应用。 首先,我们需要先在本地安装并配置好MySQL数据库,并创建一个名为"test"的数据库,以及一个名为"user"的表,表结构如下: ```sql CREATE TABLE user ( id INT(11) NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, PRIMARY KEY (id) ); ``` 然后,我们需要在Java项目中导入MySQLJDBC驱动包,这里以MySQL Connector/J驱动为例。在项目中引入驱动包后,我们可以通过以下代码连接到数据库,并执行一些基本的查询和插入操作。 ```java import java.sql.*; public class JDBCTest { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // 加载MySQLJDBC驱动 Class.forName("com.mysql.jdbc.Driver"); // 建立数据库连接 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); // 创建Statement对象,用于执行SQL语句 stmt = conn.createStatement(); // 执行查询操作 rs = stmt.executeQuery("SELECT * FROM user"); // 遍历查询结果集 while (rs.next()) { int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); System.out.println("id: " + id + ", username: " + username + ", password: " + password); } // 执行插入操作 stmt.executeUpdate("INSERT INTO user (username, password) VALUES ('testuser', 'testpassword')"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭连接和Statement对象 try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 以上代码中,我们首先加载MySQLJDBC驱动,然后建立数据库连接,并创建Statement对象,用于执行SQL语句。接着,我们执行一个查询操作,遍历查询结果集并输出每行数据;然后执行一个插入操作,向user表中插入一条记录。最后,我们关闭连接和Statement对象。 这就是一个简单的JDBC技术的应用案例,通过JDBC技术,我们可以在Java程序中方便地访问和操作数据库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值