热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java程序调用Oracle

1.java程序调用存储过程publicvoidprocedureTest(){Stringsql{callpro_test111(?,?,?)};CallableState

1. java程序调用存储过程

public void procedureTest(){String sql = "{ call pro_test111(?,?,?)}";CallableStatement call = null;try {call = conn.prepareCall(sql);//IN需要set OUT需要reistercall.setString(1, "jdbc");call.setInt(2, 2);call.setString(3, "default");call.registerOutParameter(3, Types.VARCHAR);call.execute();//拿到返回值Object object = call.getObject(3);System.out.println(object);} catch (SQLException e) {e.printStackTrace();} finally {//关闭连接DBConfig.close(call, conn);}}

2. DBUtils工具调用Oracle


2.1 首先引入oracle6.jar commons-dbutils-1.4.jar c3p0-0.9.1.2.jar包


2.2 在新建db.properties

poolName=c3p0
className=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=chalco1129
password=chalco1129
initialPoolSize=10
minPoolSize=20
maxPoolSize=50
maxIdleTime=30

2.3 新建数据库连接配置信息工具类

package com.isoftstone.utils;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/*** 数据库连接配置信息* @author mjzhud**/
public class DBConfiguration {private static Properties pro;static {//加载属性文件,读取数据库连接配置信息pro = new Properties();try {pro.load(DBConfiguration.class.getResourceAsStream("/db.properties"));} catch (IOException e) {e.printStackTrace();System.out.println("未找到配置文件!!!");}}public static Connection getConnection() {if ("c3p0".equals(pro.getProperty("poolName"))) return getC3P0Connection();if ("dbcp".equals(pro.getProperty("poolName"))) return getC3P0Connection();return null;}private static Connection getC3P0Connection(){ComboPooledDataSource c3p0 = new ComboPooledDataSource();try {c3p0.setDriverClass(pro.getProperty("className"));c3p0.setJdbcUrl(pro.getProperty("url"));c3p0.setUser(pro.getProperty("username"));c3p0.setPassword(pro.getProperty("password"));c3p0.setInitialPoolSize(Integer.valueOf(pro.getProperty("initialPoolSize")));c3p0.setMinPoolSize(Integer.valueOf(pro.getProperty("minPoolSize")));c3p0.setMaxPoolSize(Integer.valueOf(pro.getProperty("maxPoolSize")));c3p0.setMaxIdleTime(Integer.valueOf(pro.getProperty("maxIdleTime")));return c3p0.getConnection();} catch (PropertyVetoException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();System.out.println("连接失败,检查用户名和密码");}return null;}public static void close(Statement statement,Connection conn){try {if (statement != null) statement.close();} catch (SQLException e) {e.printStackTrace();}try {if (conn != null) conn.close();} catch (SQLException e) {e.printStackTrace();}}public static void close(ResultSet rs,Statement statement,Connection conn){try {if (rs != null) rs.close();} catch (SQLException e) {e.printStackTrace();}close(statement,conn);}
}

2.4 新建JdbcDBUtil工具类

package com.isoftstone.utils;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.junit.Before;
import org.junit.Test;
import bios.report.core.chart.t;
import com.isoftstone.manager.bean.Student;
import com.isoftstone.utils.DBConfiguration;public class JdbcDBUtil {private static Connection conn = DBConfiguration.getConnection();public static QueryRunner runner = new QueryRunner();public static int add(String sql) throws SQLException{int count = -1;try {conn.setAutoCommit(false);count = runner.update(conn , sql);} catch (Exception e) {conn.rollback();} finally {conn.commit();}if (count > 0) {System.out.println("一条新增成功");return 1;}System.out.println("一条新增失败");return -1;}public static int add(String sql,Object[] params) throws SQLException{int count = -1;try {conn.setAutoCommit(false);count = runner.update(conn , sql, params);} catch (Exception e) {conn.rollback();} finally {conn.commit();}if (count > 0) {System.out.println("一条新增成功");return 1;}System.out.println("一条新增失败");return -1;}public static int update(String sql) throws SQLException{try {conn.setAutoCommit(false);runner.update(conn , sql);} catch (Exception e) {conn.rollback();return -1;} finally {conn.commit();}System.out.println("一条更新成功");return 1;}public static int update(String sql,Object[] params) throws SQLException{try {conn.setAutoCommit(false);runner.update(conn , sql, params);} catch (Exception e) {conn.rollback();return -1;} finally {conn.commit();}System.out.println("一条更新成功");return 1;}public static int delete(String sql) throws SQLException{try {conn.setAutoCommit(false);runner.update(conn , sql);} catch (Exception e) {conn.rollback();return -1;} finally {conn.commit();}System.out.println("一条删除成功");return 1;}public static int delete(String sql,Object[] params) throws SQLException{try {conn.setAutoCommit(false);runner.update(conn , sql, params);} catch (Exception e) {conn.rollback();return -1;} finally {conn.commit();}System.out.println("一条删除成功");return 1;}/*** DBUtils批处理,只能处理同一预处理sql语句* @param sql* @param paramArr* @return* @throws SQLException*/public static int dbUtilsBatch(String sql,Object[][] paramArr) throws SQLException{int[] count = null;try {conn.setAutoCommit(false);count = runner.batch(conn , sql, paramArr);} catch (Exception e) {conn.rollback();return -1;} finally {conn.commit();}int updateCount = 0;if (count != null && count.length > 0) {for (int i : count) {if (i > 0) updateCount++;}}System.out.println("影响的行数="+updateCount);return 1;}/*** Statement可以批量处理各种sql* @param sqls* @return* @throws SQLException*/public static int statementBatch(String[] sqls) throws SQLException{Statement statement = conn.createStatement();for (String sql : sqls) {statement.addBatch(sql);}int[] count = null;try {conn.setAutoCommit(false);count = statement.executeBatch();} catch (Exception e) {conn.rollback();return -1;} finally {conn.commit();DBConfiguration.close(statement, conn);}int updateCount = 0;if (count != null && count.length > 0) {for (int i : count) {if (i > 0) updateCount++;}}System.out.println("影响的行数="+updateCount);return 1;}public static List> queryList(String sql) throws SQLException{return runner.query(conn, sql, new MapListHandler());}public static List> queryList(String sql,Object[] params) throws SQLException{return runner.query(conn, sql, new MapListHandler(),params);}
}

2.5 新建Bean类

package com.isoftstone.manager.bean;public class Student {private long id;private String name;private int gender;private int age;private String email;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getGender() {return gender;}public void setGender(int gender) {this.gender = gender;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", gender=" + gender+ ", age=" + age + ", email=" + email + "]";}
}

2.6 新建Dao接口

package com.isoftstone.manager.dao;
import java.sql.SQLException;
import java.util.List;
import com.isoftstone.manager.bean.Student;public interface StudentDao {public int add(Student stu) throws SQLException;public int updateById(Student stu) throws SQLException;public int deleteById(int id) throws SQLException;public Student queryById(int id) throws SQLException;public List queryAll() throws SQLException;
}

2.7 新建Dao实现类

package com.isoftstone.manager.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.isoftstone.manager.bean.Student;
import com.isoftstone.manager.dao.StudentDao;
import com.isoftstone.utils.DBConfiguration;
import com.isoftstone.utils.JdbcDBUtil;public class StudentDaoImpl implements StudentDao {public int add(Student stu) throws SQLException {String sql = "insert into stu values (seq_stu.nextval,?,?,?,?)";Object[] params = new Object[]{stu.getName(),stu.getGender(),stu.getAge(),stu.getEmail()};return JdbcDBUtil.add(sql,params);}public int updateById(Student stu) throws SQLException {String sql = "update stu set name = ?,gender=?,age=?,email=? where id = ?";Object[] params = new Object[]{stu.getName(),stu.getGender(),stu.getAge(),stu.getEmail(),stu.getId()};return JdbcDBUtil.update(sql,params);}public int deleteById(int id) throws SQLException {String sql = "delete from stu where id = " + id;return JdbcDBUtil.delete(sql);}public Student queryById(int id) throws SQLException {String sql = "select * from stu where id = ?";return JdbcDBUtil.runner.query(DBConfiguration.getConnection(), sql, new BeanHandler(Student.class),id);}public List queryAll() throws SQLException {String sql = "select * from stu where id = ?";return JdbcDBUtil.runner.query(DBConfiguration.getConnection(), sql, new BeanListHandler(Student.class));}
}

推荐阅读
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 如何在php中将mysql查询结果赋值给变量
    本文介绍了在php中将mysql查询结果赋值给变量的方法,包括从mysql表中查询count(学号)并赋值给一个变量,以及如何将sql中查询单条结果赋值给php页面的一个变量。同时还讨论了php调用mysql查询结果到变量的方法,并提供了示例代码。 ... [详细]
  • WhenIusepythontoapplythepymysqlmoduletoaddafieldtoatableinthemysqldatabase,itdo ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
author-avatar
Sek_5123_533_477
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有