本文来自博客园,作者:Lovemywx2,转载请注明原文链接:https://www.cnblogs.com/1187163927ch/p/8669859.html
JAVA连接ORACLE数据库
1,首先需要在Oracle安装完成之后新建一个用户
--新建用户
create user chenh identified by chenh;
--解锁用户
alter user chenh account unlock;
alter user scott account unlock;
--修改密码
alter user chenh identified by chenh;
--授予权限
grant sysdba to chenh;
--授予chenh用户创建session的权限,即登陆权限
grant create session to chenh;
--授予chenh用户使用表空间的权限
grant unlimited session to chenh;
--授予创建表的权限
grant create table to chenh;
--授予删除表的权限
grant drop table to chenh;
--插入表的权限
grant insert table to chenh;
--修改表的权限
grant update table to chenh;
--查看当前数据库实例名
select name from v$database;
--提交事务
commit;
2.用户新建完成之后,可能会出现某个用户对标users没有权限的提示
1 -- 解决用户 XX 对 users表空间没有权限的问题
2 alter user chenh quota unlimited on users;
3.新建一个测试表以及向其中插入几条数据
--删除表
drop table a;
--新建表
create table a(
col1 varchar2(10),
col2 varchar2(10),
col3 varchar2(10),
col4 varchar2(10)
);
--查询
select * from a;
--插入语句 多执行几次,因为没有主键限制,故此处可以执行多次
insert into a (col1,col2,col3,col4) values ('a1','b','c','d');commit;
4.需要在eclipse中新建一个java项目
打开eclipse(myeclipse)-> File(文件)-> New(新建) ->Project(工程) -> Java Project (Java 工程)
Project Name: 项目名称 接着一路Next(下一步)或者直接Finish(完成)
5.需提前下载ojdbc6.jar 连接数据库驱动的JAR包
这里提供下载地址:https://files.cnblogs.com/files/1187163927ch/blog.rar
解压密码:1187163927
6.在eclipse(myeclipse)新建的项目中将ojdbc6.jar导入,记得build path.
打开该项目树状图,将下载好的ojdbc6.jar直接拖到该项目中,然后鼠标左键选中该文件,右键Build Path即可。
7.新建一个java类,在其中连接数据库
7.1连接数据库驱动
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class DBConnection {public static Connection dbConn(String name, String pass) {Connection c = null;try {Class.forName("oracle.jdbc.driver.OracleDriver");// 要是导入驱动没有成功的话都是会出现classnotfoundException.自己看看是不是哪里错了,例如classpath这些设置} catch (ClassNotFoundException e) {e.printStackTrace();}try {c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:chenh", name, pass);// 连接数据的方法有四种, 这个属于最简单的,一般用网页程序 chenh是你的数据库实例名称,在下载的文件test.sql中可以执行语句查看// "jdbc:oracle:thin:@计算机名称:监听端口:系统实例名", username, password,// 计算机名称,要是自己不知道可以在计算机属性查知.// 监听端口一般默认是1521, 要是改变了就看自己的监听文件listener.ora// 系统实例名一般是默认orcl, 要是不是的话就用 select name from v$database; 看看当前的实例名.在本程序中我更改了实例为chenh// username,password,就是登陆数据库的用户名和密码.} catch (SQLException e) {e.printStackTrace();}return c;}
}
7.2 查询数据库
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class DB extends DBConnection {private static Connection con = null;private static Statement sql = null;private static ResultSet rs = null;public static void main(String[] args) throws SQLException {String COL1;String COL2;String COL3;String COL4;try {con = dbConn("chenh", "chenh");if (con == null) {System.out.print("连接失败");System.exit(0);}sql = con.createStatement();rs = sql.executeQuery("select * from a");System.out.println("COL1" + " " + "COL2" + " "+ "COL3"+ " "+ "COL4");while (rs.next()) {COL1 = rs.getString(1);COL2 = rs.getString(2);COL3 = rs.getString(3);COL4 = rs.getString(4);System.out.println(COL1 + " " + COL2 + " " + COL3+ " " + COL4);}} catch (Exception e) {e.printStackTrace();} finally {con.close();}}
}
检查不报错之后,执行按钮,选中项目 右键 -> Run As -> Java Application 可看到如下结果:
本文来自博客园,作者:Lovemywx2,转载请注明原文链接:https://www.cnblogs.com/1187163927ch/p/8670409.html
JAVA连接SAP
1.首先需要在SAP事务码SE37中新建一个可以被远程调用的RFC
事务码:SE37
新建一个函数组:输入事务码SE37回车后,来到函数构建器屏幕,到上面一排菜单栏:转到 -> 函数组 -> 创建组
输入描述信息,方便以后使用,以后功能相似的函数都可以放到该函数组下
函数组创建完毕后,回到SE37初始界面,创建函数,键入函数名后,点击创建按钮
在属性页签下,输入函数的描述,将远程启用的模块选上
在导入导出参数页签下设置输入输出参数(远程调用模块的注入,输出),要注意参考类型,可选性和传递值
在源代码中
FUNCTION zchenh001.
*"----------------------------------------------------------------------
*"*"局部接口:
*" IMPORTING
*" VALUE(P1) TYPE INT4 DEFAULT 0
*" VALUE(P2) TYPE INT4 DEFAULT 0
*" VALUE(OPERATOR) TYPE CHAR1 DEFAULT '+'
*" EXPORTING
*" VALUE(RESULT) TYPE INT4
*" VALUE(MSG) TYPE CHAR255
*"----------------------------------------------------------------------DATA:err_text TYPE string,e TYPE REF TO cx_root.TRY .CASE operator.WHEN '+'. result = p1 + p2.WHEN '-'. result = p1 - p2.WHEN '*'. result = p1 * p2.WHEN '/'. result = p1 / p2.WHEN OTHERS.CONCATENATE '操作符' operator ',SAP无法识别' into msg.ENDCASE.CATCH cx_root INTO e.err_text = e->get_text( ).msg = err_text.ENDTRY.ENDFUNCTION.
在SAP中测试如下:
测试一:
测试二:
测试三:
测试四:
接下来需要下载连接SAP的驱动sapjco3.jar包,
本处提供下载:sapjco3.jar
解压密码:1187163927
激活后可以在SAP内部测试 ,至此SAP部分已完成
2在eclipse(myeclipse)新建的项目中将sapjco3.jar导入,记得build path.
打开该项目树状图,将下载好的sapjco3.jar直接拖到该项目中,然后鼠标左键选中该文件,右键Build Path即可。
2.1 配置与SAP系统的连接(此处最好在SAP系统中新建一个RFC用户)
package com.cee.conn;import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.logging.Logger;import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;/*** 与SAP连接配置** @author jay*/
public class SAPConn {private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";static {Properties connectProperties = new Properties();connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxxx.xxxx.xxxx.xxxx");// 服务器connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx"); // 系统编号connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); // SAP集团connectProperties.setProperty(DestinationDataProvider.JCO_USER, "xxxx"); // SAP用户名connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "xxxxx"); // 密码connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); // 登录语言:ZH ENconnectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);}/*** 创建SAP接口属性文件。** @param name* ABAP管道名称* @param suffix* 属性文件后缀* @param properties* 属性文件内容*/private static void createDataFile(String name, String suffix, Properties properties) {File cfg = new File(name + "." + suffix);if (cfg.exists()) {cfg.deleteOnExit();}try {FileOutputStream fos = new FileOutputStream(cfg, false);properties.store(fos, "for tests only !");fos.close();} catch (Exception e) {System.out.println("Create Data file fault, error msg: " + e.toString());throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);}}/** * 获取SAP连接** @return SAP连接对象*/public static JCoDestination connect() {JCoDestination destination = null;try {destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);} catch (JCoException e) {System.out.println("Connect SAP fault, error msg: " + e.toString());}return destination;}
}
2.2 在java代码中测试连接
package com.cee.test;
import java.io.ObjectInputStream.GetField;
import com.cee.conn.SAPConn;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;public class CheckSnFromSAP {public static void main(String[] args) {JCoFunction function = null;JCoDestination destination = SAPConn.connect();int result=0;//调用接口返回状态String message="";//调用接口返回信息try {//调用ZCHENH001函数function = destination.getRepository().getFunction("ZCHENH001");JCoParameterList input = function.getImportParameterList();input.setValue("P1", 10);input.setValue("P2", 2);input.setValue("OPERATOR", "?"); // 输入参数function.execute(destination);result= function.getExportParameterList().getInt("RESULT");//调用接口返回结果message= function.getExportParameterList().getString("MSG");//调用接口返回信息System.out.println("调用返回结果--->"+result+";调用返回状态--->"+message);}catch (Exception e) {e.printStackTrace();}}
}
运行结果如下:
测试一: 注入参数分别为:10,2,?
测试二: 注入参数分别为:10,2,/
JAVA调用SAP多个环境数据
1.生成通用配置文件
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;/*** 与SAP连接配置** @author 陈辉* @version v1.01**/
public class SAPConn {private static final String S4I100 = "ABAP_AS_WITH_POOL_S4DI00";private static final String S4D100 = "ABAP_AS_WITH_POOL_S4D100";private static final String S4D200 = "ABAP_AS_WITH_POOL_S4D200";private static final String S4D360 = "ABAP_AS_WITH_POOL_S4D360";private static final String S4Q800 = "ABAP_AS_WITH_POOL_S4Q800";private static final String S4P800 = "ABAP_AS_WITH_POOL_S4P800";static {Properties connectProperties = new Properties();connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxxx.xxxx.xxxx");// 服务器connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx"); // 系统编号connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); // SAP集团connectProperties.setProperty(DestinationDataProvider.JCO_USER, "XXXX"); // SAP用户名connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXXXXXXX"); // 密码connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); // 登录语言:ZH ENconnectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程createDataFile(S4I100, "jcoDestination", connectProperties);connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxxx.xxxx.xxxx");// 服务器connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx"); // 系统编号connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); // SAP集团connectProperties.setProperty(DestinationDataProvider.JCO_USER, "XXXX"); // SAP用户名connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXXXXXXX"); // 密码connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); // 登录语言:ZH ENconnectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程createDataFile(S4D100, "jcoDestination", connectProperties);connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxxx.xxxx.xxxx");// 服务器connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx"); // 系统编号connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); // SAP集团connectProperties.setProperty(DestinationDataProvider.JCO_USER, "XXXX"); // SAP用户名connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXXXXXXX"); // 密码connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); // 登录语言:ZH ENconnectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程createDataFile(S4D200, "jcoDestination", connectProperties);connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxxx.xxxx.xxxx");// 服务器connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx"); // 系统编号connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); // SAP集团connectProperties.setProperty(DestinationDataProvider.JCO_USER, "XXXX"); // SAP用户名connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXXXXXXX"); // 密码connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); // 登录语言:ZH ENconnectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程createDataFile(S4D360, "jcoDestination", connectProperties);connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "xxxx.xxxx.xxxx");// 服务器connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "xx"); // 系统编号connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "xxx"); // SAP集团connectProperties.setProperty(DestinationDataProvider.JCO_USER, "XXXX"); // SAP用户名connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXXXXXXX"); // 密码connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); // 登录语言:ZH ENconnectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程createDataFile(S4Q800, "jcoDestination", connectProperties);// 组登录connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME, "S4P");// SYSTEM IDconnectProperties.setProperty(DestinationDataProvider.JCO_MSHOST, "10.10.10.10");// 服务器connectProperties.setProperty(DestinationDataProvider.JCO_MSSERV, "3601"); // Message Server 服务端口connectProperties.setProperty(DestinationDataProvider.JCO_GROUP, "S4P"); // 组名称connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "800"); // SAP集团connectProperties.setProperty(DestinationDataProvider.JCO_USER, "XXXXXX"); // SAP用户名connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXXXXX"); // 密码connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); // 登录语言:ZH ENconnectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程connectProperties.setProperty(DestinationDataProvider.JCO_MAX_GET_TIME, "360"); // 最大连接时间connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, "");// ROUTERcreateDataFile(S4P800, "jcoDestination", connectProperties);}/*** 创建SAP接口属性文件。** @param name ABAP管道名称* @param suffix 属性文件后缀* @param properties 属性文件内容*/private static void createDataFile(String name, String suffix, Properties properties) {File cfg = new File(name + "." + suffix);if (cfg.exists()) {cfg.deleteOnExit();}try {FileOutputStream fos = new FileOutputStream(cfg, false);properties.store(fos, "仅仅测试使用");fos.close();} catch (Exception e) {System.out.println("创建参数配置文件失败: " + e.toString());throw new RuntimeException("无法创建创建参数文件:" + cfg.getName(), e);}}/** * 获取SAP连接** @return SAP连接对象*/public static JCoDestination connect(String SYSTEM) {JCoDestination destination = null;try {switch (SYSTEM) {case "S4I100":destination = JCoDestinationManager.getDestination(S4I100);break;case "S4D100":destination = JCoDestinationManager.getDestination(S4D100);break;case "S4D200":destination = JCoDestinationManager.getDestination(S4D200);break;case "S4D360":destination = JCoDestinationManager.getDestination(S4D360);break;case "S4Q800":destination = JCoDestinationManager.getDestination(S4Q800);break;case "S4P800":destination = JCoDestinationManager.getDestination(S4P800);break;default:destination = JCoDestinationManager.getDestination(S4D360);break;}System.out.println("已经连接到SAP: " + SYSTEM + "环境...");} catch (JCoException e) {System.out.println("无法连接到SAP: " + e.toString());}return destination;}
}
2.调用测试RFC_READ_TABLE
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoField;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;public class FM_RFC_READ_TABLE {enum SAP_SYSTEM {S4I100, S4D100, S4D200, S4D360, S4Q800, S4P800,}public static void main(String[] args) {JCoFunction function = null;JCoDestination destination = SAPConn.connect(SAP_SYSTEM.S4I100.toString());try {function = destination.getRepository().getFunction("RFC_READ_TABLE");JCoParameterList input = function.getImportParameterList();input.setValue("QUERY_TABLE", "T001");// 表名input.setValue("DELIMITER", ";");// 查询分隔符input.setValue("ROWCOUNT", "100");// 条目数JCoTable OPTIONS = function.getTableParameterList().getTable("OPTIONS");OPTIONS.appendRow();OPTIONS.setValue("TEXT", "BUKRS > 1000 AND LAND1 = 'CN' ");// 条件JCoTable FIELDS = function.getTableParameterList().getTable("FIELDS");FIELDS.appendRow();FIELDS.setValue("FIELDNAME", "MANDT");FIELDS.appendRow();FIELDS.setValue("FIELDNAME", "BUKRS");FIELDS.appendRow();FIELDS.setValue("FIELDNAME", "BUTXT");FIELDS.appendRow();FIELDS.setValue("FIELDNAME", "ORT01");// 其他字段...function.execute(destination);FIELDS = function.getTableParameterList().getTable("FIELDS");for (int i = 0; i < FIELDS.getNumRows(); i++) {FIELDS.setRow(i);for (JCoField fields : FIELDS) {if (fields.getName().toString().equals("FIELDNAME")) {System.out.print(String.format("%s\t", fields.getValue()));}}}System.out.println();JCoTable DATA = function.getTableParameterList().getTable("DATA");// 调用接口返回信息for (int i = 0; i < DATA.getNumRows(); i++) {DATA.setRow(i);for (JCoField fields : DATA) {String[] field = fields.getValue().toString().split(";");for (int j = 0; j < field.length; j++) {System.out.print(String.format("%s\t", field[j]));}}System.out.println();}} catch (Exception e) {e.printStackTrace();}}}