java连接as400数据库,原创:用JDBC访问AS/400数据 配置与JDBC编程要点Windows系统 -电脑资料...
发了半天了,只见鱼过,不见冒泡,正在疑惑各位大大是否对此题目不感兴趣呢?luoshengzh大大的鼓励来了,谢谢luoshengzh,
发了半天了,只见鱼过,不见冒泡,正在疑惑各位大大是否对此题目不感兴趣呢?luoshengzh大大的鼓励来了,谢谢luoshengzh。余文一并发出,不喜欢的就拍块砖吧,潜水的不要。 :em08:
四.配置JDBC
4.1 在配置JDBC之前,请确保工作站上的JDK已正确配置,且可以下常使用。
4.2 找到JDBC软件包
AS/400 Toolbox forJava安装后,用户访问AS/400数据的JDBC软件包即生成在IFS(集成文件系统)中,其路径是:/QIBM/ProdData/HTTP/Public/jt400/lib/ jt400.zip 。用户可以使用Client Aclearcase/" target="_blank" >ccess 或NetServer 将此路径MAP成一个本地磁盘驱动器,也可索性用FTP将其jt400.zip下载下来使用。
4.3 设置环境路径(以jt400.zip在I:\jt400\ 目录为例)
4.3.1Windows98 & 95环境,在AUTOEXEC.BAT中增加一行:
set classpath = %CLASSPATH%;I:\jt400\jt400.zip
[此有一图,谁能教我如何加图?谢谢]
4.3.2WindowsNT环境
4.3.2.1双击[我的电脑]图标
4.3.2.2双击[我的电脑]文件夹中的[控制面板]图标
4.3.2.3在[控制面板]文件夹中双击[系统]图标
4.3.2.4选择[环境变量]面板
4.3.2.5增加CLASSPATH变量,再在下一行输入变量值,如下图:
[此又有一图,谁能教我如何加图?谢谢]
五.JDBC编程要点
5.1 注册JDBC驱动器程序
访问AS/400数据的JDBC驱动器程序叫com.ibm.as400.access.AS400JDBCDriver, 在JDBC编程中要建立程序与数据库的连接,首先得注册这个JDBC驱动器,请使用如下语句:
java.sql.DriverManager.registerDriver (new com.ibm.as400.access.AS400JDBCDriver ());
5.2 建立数据库连接
在JDBC驱动器注册之后,第二步要做的就是建立数据库连接。可使用类似于如下语句的语句,更多的连接方式请见 附录A:
Connection c = DriverManager.getConnection(
"jdbc:as400://mySystem;naming=sql;errors=full",
"auser", "apassword");
5.3 使用SQL语句执行SQL操作
5.3.1 使用Statement接口
Statement对象可用来执行一个简单的SQL语句,使用一个Connection对象创建一个Statement对象。如:c.createStatement()。具体使用如下例所示:
// Connect to the AS/400.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
// Create a Statement object.
Statement s = c.createStatement();
// Run an SQL statement that creates a table in the database.
s.executeUpdate("CREATE TABLE MYLIBRARY.MYTABLE (NAME VARCHAR(20), ID INTEGER)");
// Run an SQL statement that inserts a record into the table.
s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('DAVE', 123)");
// Run an SQL statement that inserts a record into the table.
s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('CINDY', 456)");
// Run an SQL query on the table.
ResultSet rs = s.executeQuery("SELECT * FROM MYLIBRARY.MYTABLE");
// Close the Statement and the Connection.
s.close();
c.close();
5.3.2 使用PreparedStatement接口
PreparedStatement接口提供了一种灵活的SQL语句的执行方式。它可以在欲运行的SQL语句中预留下参数变量,在真正运行时将不用的参数调用则可获得不同的结果,这个接口在对数据进行批量同类执行时非常有用。请参见下面的例子:
// Connect to the AS/400.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
// Create the PreparedStatement object. It precompiles the specified SQL
// statement. The question marks indicate where parameters must be set before
// the statement is run.
PreparedStatement ps = c.prepareStatement("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES (?, ?)");
// Set parameters and run the statement.
ps.setString(1, "JOSH");
ps.setInt(2, 789);
ps.executeUpdate();
// Set parameters and run the statement again.
ps.setString(1, "DAVE");
ps.setInt(2, 456);
ps.executeUpdate();
// Close PreparedStatement and the Connection.
ps.close();
c.close();
5.3.3 使用CallableStatement接口
通过CallableStatement接口,可以运行SQL存储过程(stored procedures)。存储过程是随数据库保存的小程序。请参见下面的例子
// Connect to the AS/400.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
// Create the CallableStatement object. It precompiles the specified call to a stored
// procedure. The question marks indicate where input parameters must be set and
// where output parameters can be retrieved. The first two parameters are
// input parameters, and the third parameter is an output parameter.
CallableStatement cs = c.prepareCall("CALL MYLIBRARY.ADD (?, ?, ?)");
// Set input parameters.
cs.setInt (1, 123);
cs.setInt (2, 234);
// Register the type of the output parameter.
cs.registerOutParameter (3, Types.INTEGER);
// Run the stored procedure.
cs.execute ();
// Get the value of the output parameter.
int sum = cs.getInt (3);
// Close the CallableStatement and the Connection.
cs.close();
c.close();
5.3.4 使用ResultSet接口
使用ResultSet接口可以得到SQL语句所获得的数据结果集,并且可以使用游标 (Cursor)来方便地浏览数据结果集。请参见以下的例子:
// Connect to the AS/400.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
// Create a Statement object. Set the result set
// type to scroll insensitive.
Statement s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// Run a query. The result is placed in a ResultSet object.
ResultSet rs = s.executeQuery ("SELECT NAME,ID FROM
MYLIBRARY.MYTABLE");
// Iterate through the rows of the ResultSet.Go to the next row if the user selected 'N'
// or previous record if the user selected 'P'. Assume that getUserSelection() is
// defined elsewhere.
boolean done = false;
while (! done)
{
switch (getUserSelection ())
{
case 'N':
rs.next ();
break;
case 'P':
rs.previous ();
break;
default:
done = true;
}
// Get the values from the ResultSet. The first value is a string, and
// the second value is an integer.
String name = rs.getString("NAME");
int id = rs.getInt("ID");
System.out.println("Name = " + name);
System.out.println("ID = " + id);
}
// Close the Statement and the Connection.
s.close();
c.close();
附录A : 使用JDBC 驱动器连接AS/400 数据库
你可以使用DriverManager.getConnection() 方法来连接AS/400数据库. DriverManager.getConnection() 使用一个URL字符串作为参数. JDBC驱动器管理器将为尝试连接在URL字符串中所指的数据库:
"jdbc:as400://systemName/defaultSchema;listOfProperties"
以下是一些连接方式的例子
例一:URL不给出系统名。这种情况需要用户在使用时给出欲连接的系统名:
"jdbc:as400:"
例二:URL只给出系统名
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
例三:URL给出系统名,且给出缺省的Schema
Connection c2 = DriverManager.getConnection("jdbc:as400://mySys2/mySchema");
例四:连接AS/400 数据库,且使用java.util.Properties 定义更多的JDBC 连接属性。
// Create a properties object.
Properties p = new Properties();
// Set the properties for the connection.
p.put("naming", "sql");
p.put("errors", "full");
// Connect using the properties object.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem",p);
例五:连接AS/400数据库,并且给出URL的相关属性.
// Connect using properties. The properties are set on the URL
// instead of through a properties object.
Connection c = DriverManager.getConnection( "jdbc:as400://mySystem;naming=sql;errors=full");
例六:连接AS/400数据库且给出用户名与口令
// Connect using properties on the URL and specifying a user ID and password
Connection c = DriverManager.getConnection(
"jdbc:as400://mySystem;naming=sql;errors=full",
"auser", "apassword");
例七:关闭数据库连接
使用close() 方法将连接关闭,如 c.close();
xuguopeng 回复于:2004-03-18 17:41:01不错~~ 很棒~ 可惜看不懂~~ 没用过JDBC,原创:用JDBC访问AS/400数据 配置与JDBC编程要点Windows系统》(https://www.unjs.com)。。。。。。。
moshco 回复于:2004-03-19 09:34:06我是专门搜索你写的贴子了!写的很好!
rollingpig 回复于:2004-03-19 13:35:05加点注解
关于注册Driver部分
[quote:9653859d8b]
5.1 注册JDBC驱动器程序
访问AS/400数据的JDBC驱动器程序叫com.ibm.as400.access.AS400JDBCDriver, 在JDBC编程中要建立程序与数据库的连接,首先得注册这个JDBC驱动器,请使用如下语句:
java.sql.DriverManager.registerDriver (new com.ibm.as400.access.AS400JDBCDriver ());
[/quote:9653859d8b]
通常,由于Driver类不一定已经存在于ClassPath里(可以在运行时才加上)
同时,Drvier类在初始化时会自动注册Driver
所以,这一段通常是这么写的
[code:1:9653859d8b]
try{
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
}
catch(ClassNotFoundException e){
}
[/code:1:9653859d8b]
也可以达到同样效果,而且可以把DrvierName放在Properties里,随时改动Driver而不需重新改代码,如:
[code:1:9653859d8b]
String getProperties(String name){
.....//获取配置的属性
}
...
String driverClass = getProperties("driver");
String user=getProperties("user");
String pass=getProperties("pass");
String url=getProperties("url");
try{
Class.forName(driverClass);
}
catch(ClassNotFoundException e){
}
Connection c = DriverManager.getConnection( ...);
[/code:1:9653859d8b]
fh2001 回复于:2004-03-19 19:24:28谢谢 rollingpig 大大补充。
neosue 回复于:2004-03-20 10:19:39請問如何才能得到 COLHDG 的內容?
andrewleading_he 回复于:2004-03-22 14:36:49呵呵,不錯啊,幫忙頂一下。不過聽說jdbc的效率比直接用ile rpg來操作數據要慢很多。尤其是add ,update動作!
ecamel 回复于:2004-03-23 11:43:09不错~!
rufujian 回复于:2004-06-12 11:53:52请问AS/400是不是必须安装AS/400 Toolbox for Java,如果我拿到jdbc驱动包
服务器上没有安装AS/400 Toolbox for Java,可不可以连接上
fh2001 回复于:2004-06-12 22:39:46[quote:385091cb76="rufujian"]请问AS/400是不是必须安装AS/400 Toolbox for Java,如果我拿到jdbc驱动包
服务器上没有安装AS/400 Toolbox for Java,可不可以连接上[/quote:385091cb76]
你可以试试看
Amichael 回复于:2004-06-14 16:54:00内容详细,谢谢了。
mario663 回复于:2004-11-08 21:10:25那里可以得到Jdbc驱动呀,急急!
mario663 回复于:2004-11-08 21:50:11我怎么找不到jt400.zip呀,我的Client Access 是英文版权所5.1,在安装显示AS/400 Toolbox for Java已安装,请那位高手指点,谢谢.
bpcsusr 回复于:2004-11-09 09:15:55得仔细看看.
bobofish29 回复于:2004-11-09 09:52:20[quote:8a8165d1f3="mario663"]我怎么找不到jt400.zip呀,我的Client Access 是英文版权所5.1,在安装显示AS/400 Toolbox for Java已安装,请那位高手指点,谢谢.[/quote:8a8165d1f3]
到目录"C:\Program Files\IBM\Client Access\jt400\lib\jt400.jar"中看一看,我的C/A是安装在C盘上的!
另外,请教楼主一问题,我用JAVA写了一个测试的小程序
代码如下:
[code:1:8a8165d1f3]
import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;
public class CommandCall extends Object{
public static void main(String[] parmeters){
//Created a reader to get input from the user
BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in),1);
//Declare varialbes to hold the system name and the command to run
String systemString = null;
String commandString = null;
System.out.println("");
//Get the system name and the command to run from the user
try{
System.out.print("System name:");
systemString = inputStream.readLine();
}
catch (Exception e){};
System.out.println("");
//Create an as400 object.This is the system we send the command to
AS400 as400 = new AS400(systemString);
//Create a command call object specifying the server that will receive the command
CommandCall command = new CommandCall(as400);
//Run the command
try{
if (command.run(commandString))
System.out.print("Command successful");
else
System.out.print("Command failed");
//If messeage were produced from the command,print them
AS400Messeage[] messeageList = command.getMesseageList();
if (messeageList.length>0){
System.out.println(",messeage from the command:");
System.out.print("");
}
for (int i=0;iSystem.out.print(messeageList[i].getID());
System.out.print(":");
System.out.print(messeageList[i].getText());
}
}
catch (Exception e){
System.out.println("Command" + command.getCommand() + "didn't run!");
}
System.exit(0);
}
}
[/code:1:8a8165d1f3]
以下是报错信息:
[code:1:8a8165d1f3]---------- javac ----------
CommandCall.java:23: cannot resolve symbol
symbol : constructor CommandCall (com.ibm.as400.access.AS400)
location: class CommandCall
CommandCall command = new CommandCall(as400);
^
CommandCall.java:26: cannot resolve symbol
symbol : method run (java.lang.String)
location: class CommandCall
if (command.run(commandString))
^
CommandCall.java:32: cannot resolve symbol
symbol : class AS400Messeage
location: class CommandCall
AS400Messeage[] messeageList = command.getMesseageList();
^
CommandCall.java:32: cannot resolve symbol
symbol : method getMesseageList ()
location: class CommandCall
AS400Messeage[] messeageList = command.getMesseageList();
^
CommandCall.java:44: cannot resolve symbol
symbol : method getCommand ()
location: class CommandCall
System.out.println("Command" + command.getCommand() + "didn't run!");
^
5 errors
Output completed (5 sec consumed) - Normal Termination
[/code:1:8a8165d1f3]
just400 回复于:2004-11-09 10:06:34ding....
原文转自:http://www.ltesting.net
java连接as400数据库,原创:用JDBC访问AS/400数据 配置与JDBC编程要点Windows系统 -电脑资料...相关推荐
- java连接Orcale数据库并查询、插入、删除数据
java连接Orcale数据库并查询.插入.删除数据 oci和thin是Oracle提供的两套Java访问Oracle数据库方式. thin是一种瘦客户端的连接方式 oci是一种胖客户端的连接方式 J ...
- java连接as400数据库,连接到AS400 DB2服务器JDBC时出错
我正在尝试使用java程序中的JDBC连接到AS400 DB2数据库,但我无法连接,因为它提供了一个SQLException,指出没有为jdbc找到合适的驱动程序:as400://192.168.1. ...
- 用python和Java连接MySQL数据库,插入百万,千万条数据
python代码: 需要用到 pymysql模块,python没有的话可以通过以下方式安装: 运行Anaconda Prompt pydemo是自己创建的python开发环境的名字 (C:\Progr ...
- Java连接MySQL数据库的超级详细步骤(Windows)
1. 数据准备 1. 下载JDK 下载Java开发工具包JDK,下载地址:https://www.oracle.com/java/technologies/javase-jdk14-downloads ...
- java连接mysql数据库 R_Java连接Mysql数据库详细代码实例
这篇文章主要介绍了Java连接Mysql数据库详细代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 隔了一段时间没连过数据库,代码都忘记了,网 ...
- Java连接mysql数据库的详细教程(增查)
java连接mysql数据库[便于理解的jdbc] 一.开发环境 二.创建数据表 1. 用Navicat图形化创建 2. 用命令行创建 三.创建java工程 1.在eclipse中创建一个工程:Fir ...
- Java 连接Access数据库
Java 连接Access数据库 maven中央仓库下载UCanAccess驱动包 <!-- https://mvnrepository.com/artifact/net.sf.ucanacce ...
- Java 连接 Access 数据库
测试代码如下: package com.songyanjun.util;import java.sql.*;/*** @描述: TODO java连接Access数据库 * * <p>* ...
- java linux mysql数据库_Linux Java连接MySQL数据库
Linux(Ubuntu平台)Java通过JDBC连接MySQL数据库,与Windows平台类似,步骤如下: 解压 jdbc: tar -zxvf mysql-connector-java-5.1.1 ...
最新文章
- [BZOJ 2523][Ctsc2001]聪明的学生(递归)
- mysql传不进去汉字,Mysql下插入汉字失败
- Mongo学习---mongo入门1
- 深度梳理这10个国家的AI发展战略
- MariaDB exists 学习
- 关于Java的“找不到或无法加载主类”
- mysql 魔术设置_PHP之十六个魔术方法详细介绍
- PE文件感染和内存驻留
- JAVA环境指定xms512_【JAVA学习】java虚拟机内存配置,-Xss256m -Xms512m -Xmx800m -XX:MaxPermSize=512m...
- ubuntu查看pip安装的软件路径
- 前置递增运算符(JS)
- 这个时代,开发简单多了
- Java之父:Solaris前景堪忧
- 抽象代数之幂零群的两种等价表示即上中心列和下中心列
- 【PR】PR剪辑视频编辑软件视频去字幕
- Unity3D游戏框架之dll反编译和混淆
- xp系统一直跳出宽带连接服务器,xp系统一直显示正在获取网络地址的操作方案...
- 【ACM省赛】第九届河南省程序设计大赛 B 宣传墙
- 运维开发面试题集锦(25k-35k)
- 墨水屏(电子纸)的介绍与使用(附STM32程序~~~)
热门文章
- word去掉页面横线
- 【无机纳米材料科研制图——OriginLab 0201】Origin光谱图、曲线图绘制
- 数学-蕴涵->真值表理解
- Android Studio 报Error: Program type already present: **.BuildConfig
- scratch案例开发1
- 开源不仅是Red Hat的软件
- springcloud微服务项目架构搭建第一天
- 英利分布式:国内最高光伏空调3大亮点引关注
- 2021年熔化焊接与热切割考试试卷及熔化焊接与热切割模拟试题
- mtk+android+之mt6577驱动笔记,MTK6577+Android之音频(audio)移植