在工作中碰到要向另一个数据库进行操作的需求,例如数据源为mysql的工程某个方法内需要向oracle数据库进行某些查询操作

接口类

package com.y.erp.pur.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat; import com.y.erp.pur.entity.PqhFile;
import com.y.erp.sal.entity.PhxpFile;
import com.y.erp.yBase.entity.ItemFile; public class OraclePqpUtil {
public static SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@10.0.20.21:1521:TOPDB";
String username="t41";
String password="t41";
connection=DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//插入
public static String insertPmr(PqhFile pqhFile,String bnun,String id) throws SQLException{
//拼接sql
String pqhsql=pqhSql(pqhFile,id);
Connection con = getConnection();
//注明手动提交事务
con.setAutoCommit(false);
Statement stmt = con.createStatement();
StringBuffer sql=null;
sql=new StringBuffer();
sql.append("insert into ");
sql.append(bnun+".PMR_FILE ");
sql.append(pqhsql);
try {
stmt.executeUpdate(sql.toString());
} catch (Exception e) {
con.rollback();
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
return null;
}
con.commit();
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
return id;
} //主表插入sql
public static String pqhSql(PqhFile pqhFile,String id) {
StringBuffer values=new StringBuffer();
StringBuffer rowSql=new StringBuffer();
StringBuffer allSql=new StringBuffer();
allSql.append("(");
if(null!=pqhFile.getPqh01()) {
rowSql.append("pmr01,");
values.append("'"+id+"',");
}
if(null!=pqhFile.getPqh02()) {
rowSql.append("pmr02,");
values.append("'"+pqhFile.getPqh02()+"',");
}
if(null!=pqhFile.getPqh03()) {
rowSql.append("pmr03,");
values.append("'"+pqhFile.getPqh03()+"',");
}
if(null!=pqhFile.getPqh04()) {
rowSql.append("pmr04,");
values.append("'"+pqhFile.getPqh04()+"',");
}
if(null!=pqhFile.getPqh05()) {
rowSql.append("pmr05,");
values.append("'"+pqhFile.getPqh05()+"',");
}
if(null!=pqhFile.getPqh05t()) {
rowSql.append("pmr05t,");
values.append("'"+pqhFile.getPqh05t()+"',");
}
allSql.append(rowSql.toString().substring(0,rowSql.toString().length()-1));
allSql.append(") VALUES (");
allSql.append(values.toString().substring(0,values.toString().length()-1));
allSql.append(")");
return allSql.toString();
}
//查询
public static ItemFile getItemTt(PhxpFile phxp,String bnun) throws SQLException, ParseException {
String querySql="SELECT * FROM "+bnun+".IMA_FILE where IMA01 = '" +phxp.getPhxp14()+"'";
Connection con = getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(querySql);
ItemFile item =new ItemFile();
while (rs.next()) {
item.setItem01(rs.getString("IMA01"));
item.setItem02(rs.getString("IMA02"));
item.setItem03(rs.getString("IMA03"));
item.setItem04(rs.getString("IMA04"));
item.setItem05(rs.getString("IMA05"));
}
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
return item;
} }

pom.xml

<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>

JDBC使用的更多相关文章

  1. Java数据库连接技术——JDBC

    大家好,今天我们学习了Java如何连接数据库.之前学过.net语言的数据库操作,感觉就是一通百通,大同小异. JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力. JDBC API ...

  2. 玩转spring boot——结合AngularJs和JDBC

    参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...

  3. [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率

    使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...

  4. JDBC MySQL 多表关联查询查询

    public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...

  5. JDBC增加删除修改

    一.配置程序--让我们程序能找到数据库的驱动jar包 1.把.jar文件复制到项目中去,整合的时候方便. 2.在eclipse项目右击"构建路径"--"配置构建路径&qu ...

  6. JDBC简介

    jdbc连接数据库的四个对象 DriverManager  驱动类   DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...

  7. JDBC Tutorials: Commit or Rollback transaction in finally block

    http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...

  8. FineReport如何用JDBC连接阿里云ADS数据库

    在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...

  9. JDBC基础

    今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...

  10. Spring学习记录(十四)---JDBC基本操作

    先看一些定义: 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1.core即核心包,它包含了JDBC的核心功能.此包内有很多重要的类,包括:JdbcTemplate类.SimpleJ ...

随机推荐

  1. WPF Trigger for IsSelected in a DataTemplate for ListBox items

    <DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}"> <vw:HeaderSlugView /> ...

  2. win7下使用IIS服务器及自定义服务器端包含模块(SSI)步骤

    配置完过段时间就容易忘记,特此记录. 1.开启IIS服务器. 默认没有安装,需要先安装. 打开控制面板–> 打开“程序和功能”–> 左侧选择“启用或关闭windows功能”–> 找到 ...

  3. XCode 添加自定义framework运行时出现dyld: Library not loaded的解决方法

    XCode添加自定义framework运行时出现dyld: Library not loaded的解决方法 在使用自定义的framework运行时,会出现如下的错误: dyld: Library no ...

  4. POJ 1061 青蛙的约会 扩展欧几里德--解不定方程

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 81606   Accepted: 14116 Descripti ...

  5. C#动态创建lambda表达式

    /// <summary> /// 创建lambda表达式:p=>true /// </summary> /// <typeparam name="T&q ...

  6. Google JavaScript样式指南

    Google JavaScript样式指南   目录 1简介 1.1术语说明 1.2指南说明 2源文件基础知识 2.1文件名 2.2文件编码:UTF-8 2.3特殊字符 3源文件结构 3.1许可或版权 ...

  7. XSS 相关 payload 集合

    Ajax 获取数据 GET function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest){// code for IE7+, Fire ...

  8. CSS 小结笔记之定位

    定位也是Css中一个非常强大的属性.定位主要是用来移动盒子,将其移动到我们想要的位置. 定位分为两部分 1.边偏移 left | right |top |bottom:偏移大小:(边偏移一般制定上就不 ...

  9. 聊聊 getClientRects 和 getBoundingClientRect 方法

    开始表演 今天来聊一下两个相似的方法,它们就是:getBoundingClientRect().getClientRects(). 只见它们俩手拉手地登上了舞台,一个鞠躬,便开始滔滔不绝起来. 自述 ...

  10. eclipse安装lua

    单击Eclipse->Help->Install New Software… 在Work with中输入网址 Kepler - http://download.eclipse.org/re ...