JDBC使用
在工作中碰到要向另一个数据库进行操作的需求,例如数据源为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使用的更多相关文章
- Java数据库连接技术——JDBC
大家好,今天我们学习了Java如何连接数据库.之前学过.net语言的数据库操作,感觉就是一通百通,大同小异. JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力. JDBC API ...
- 玩转spring boot——结合AngularJs和JDBC
参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...
- [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...
- JDBC MySQL 多表关联查询查询
public static void main(String[] args) throws Exception{ Class.forName("com.mysql.jdbc.Driver&q ...
- JDBC增加删除修改
一.配置程序--让我们程序能找到数据库的驱动jar包 1.把.jar文件复制到项目中去,整合的时候方便. 2.在eclipse项目右击"构建路径"--"配置构建路径&qu ...
- JDBC简介
jdbc连接数据库的四个对象 DriverManager 驱动类 DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...
- JDBC Tutorials: Commit or Rollback transaction in finally block
http://skeletoncoder.blogspot.com/2006/10/jdbc-tutorials-commit-or-rollback.html JDBC Tutorials: Com ...
- FineReport如何用JDBC连接阿里云ADS数据库
在使用FineReport连接阿里云的ADS(AnalyticDB)数据库,很多时候在测试连接时就失败了.此时,该如何连接ADS数据库呢? 我们只需要手动将连接ads数据库需要使用到的jar放置到%F ...
- JDBC基础
今天看了看JDBC(Java DataBase Connectivity)总结一下 关于JDBC 加载JDBC驱动 建立数据库连接 创建一个Statement或者PreparedStatement 获 ...
- Spring学习记录(十四)---JDBC基本操作
先看一些定义: 在Spring JDBC模块中,所有的类可以被分到四个单独的包:1.core即核心包,它包含了JDBC的核心功能.此包内有很多重要的类,包括:JdbcTemplate类.SimpleJ ...
随机推荐
- mybatis之@Select、@Insert、@Delete、@Param
之前学习的时候,看到别人在使用mybatis时,用到@Select.@Insert.@Delete.@Param这几个注解,故楼主研究了一下,在这里与大家分享 当使用这几个注解的时候,可以省去写Map ...
- spring定时任务(@Scheduled注解)
(一)在xml里加入task的命名空间 xmlns:task="http://www.springframework.org/schema/task" http://www.spr ...
- webAPP 图片上传
关于webAPP 手机上传 用的vue.js 首先是js代码 调用手机app 的 相册或者自己拍照 upload: function(index) { //上传 this.index = index ...
- 适配器(GOF23)
---恢复内容开始--- 摘要:由于应用环境的变化,需要将现存的对象放到新的环境中去,但新环境的接口是现存对象不满足的. 意图:将原本接口不兼容的类通过转换,使得它们能够一起工作,复用现有的类 ada ...
- java Date 当天时间戳处理
1. 代码 private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; private static Date ...
- CSS-带尖角的对话框
效果图: box1的代码: .box{ position: relative; width: 200px; height: 200px; border: 2px solid #000; backgro ...
- Hello world &博客客户端试用
第一篇博客,使用 open live writer客户端进行测试,下载地址见http://openlivewriter.org/,软件为英文,但配置比较简单,选择“其他博客类型”就ok. 同时安装了语 ...
- 【转发】【小程序】微信小程序日常开发中常遇到的错误代码
还在为看不懂小程序错误状态码纠结吗?这里推荐一篇文章 重要的事情说三遍:原文链接 https://www.cnblogs.com/webonline/p/7528778.html 作者:玩世不恭. ...
- Pwn With longjmp
前言 这个是 seccon-ctf-quals-2016 的一个题,利用方式还是挺特殊的记录一下. 题目链接 http://t.cn/RnfeHLv 正文 首先看看程序的安全措施 haclh@ubun ...
- linux 获取shell内置命令帮助信息 help xx
shell,命令解释器 shell内置命令有cd/umask/pwd等 help shell内置命令适用于所有用户获取shell内置命令的帮助信息help umaskhelp if