添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

使用JDBC type4连接DB2

Categories: Database ; Tagged with: ; @ October 2nd, 2010 22:57

1. 首先要在工程中加入DB2连接文件db2jcc.jar, 该文件可从DB2安装目录中找到.
2. 建立连接:

	public void setUp() throws Exception {
		Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
		Properties connProperties = new Properties();
		connProperties.put("user", "ADMINISTRATOR");
		connProperties.put("password", "liguoliang.com");
		connection = DriverManager.getConnection(db2.getJdbcUrl("192.168.1.113", 50000, "meta"), connProperties);

3. 执行SQL语句举例:

	public void validateConnection(Connection connection) throws SQLException {
		if(connection == null) {
			throw new SQLException("Connection is null");
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = connection.createStatement();
			rs = stmt.executeQuery("VALUES(1)"); // 该语句将返回1
			rs.next();
			if(rs.getInt(1) != 1) {
				throw new SQLException("SQL Exception: values(1) != 1");
		}finally {
			JDBCUtilities.close(rs);

有用的链接:

JDBC连接DB2的一些总结

用java访问数据库DB2代码的实际操作  http://database.51cto.com/art/201008/216908.htm

使用 JDBC 连接不同版本 DB2 数据库的兼容性问题 http://www.ibm.com/developerworks/cn/data/library/techarticles/0402chenjunwei/0402chenjunwei.html#author1

通过JDBC连接DB2错误Connection timed out错误排除

Categories: Database; Tagged with: ; @ October 2nd, 2010 22:45

com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.59.81] Exception java.net.ConnectException: Error opening socket to server /192.168.1.113 on port 50,000 with message: Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001

第一次使用JDBC连接DB2数据库时, 出现上述错误.

(more…)

[旧]Java: JDBC 取得ResultSet的长度

Categories: Java; Tagged with: ; @ August 29th, 2010 18:19

JDBC 取得ResultSet的长度:

resultSet.last(); // 游标移到最后, 获得rs长度
int length = resultSet.getRow();
resultSet.first(); // 还原游标到rs开头

[旧文存档]JDBC: java.sql.SQLException: 结果集已耗尽 & 对只转发结果集的无效操作

Categories: Java; Tagged with: ; @ August 29th, 2010 18:01

1. java.sql.SQLException: 结果集已耗尽

原因: 执行的数据库查询没有查询到任何结果,却调用了resultSet.next().

2. java.sql.SQLException: 对只转发结果集的无效操作:

如果conn在创建时, 若不给定参数,预设是ResultSet.TYPE_FORWARD_ONLY、 ResultSet.CONCUR_READ_ONL, 此时不可进行游标操作, 会报错: java.sql.SQLException: 对只转发结果集的无效操作.
欲操作游标, 则应使用stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

Flex BlazeDS Java JDBC MySql 快速配置

Categories: FlexJava; Tagged with: ; @ February 20th, 2009 16:48

从下往上:image

1. Java – JDBC – MySql

1. 装好MySql, 启动服务.

2. 将mysql-connector-java-5.1.7-bin.jar 拖入WEB-INF/lib下.

2. Java端BlazeDS配置

1. Java端:

将BlazeDS压缩包内的WEB-INF里面的东西拷贝工程内的WEB-INF下的相应位置中.[ Flex目录下有四个xml配置文件, lib下是需要使用的jar] 包括web.xm

2. 配置services-config.xml中channels标签中的内容:

其中, http://localhost:8080/DepartmentManagement/ 是该Web工程的地址 [可在web-content下建立一空index.html, 运行后查看地址便可]

3. 编写Java类, 并配置remoting-config.xml文件的<service>标签下, 增加服务, 如下:

com.insprise.guoliang.DepartmentManagement

3. Flex端建立工程

1. 建立 Flex与Java通信载体RemoteObject

为方便在整个工程中使用, 可建立一个Singleton – 关于AS中的Singleton可见: http://liguoliang.com/2008/10/128/

在该类中建立RemoteObject.

首先需要确定RemoteObject的destination, 在本例中 为”DepartmentManagement”; – 在remoting-config.xml中已配置.

其次需要确定ro的channel – 本例中为:”http://localhost:8080/DepartmentManagement/messagebroker/amf"; - 在services-config.xml中已配置

	public static const DEFAULT_DEST:String = "DepartmentManagement";
	public static const DEFAULT_CHANNEL_URL:String = "http://localhost:8080/DepartmentManagement/messagebroker/amf";
	private static var _ro:RemoteObject;
	 * Constractor - Singleton
	 public function AppContext():void {
	 	throw new Error("AppContext is Singleton!");
	 * Get RemoteObject
	public static function getRemoteObject():RemoteObject {
		if(_ro == null) {
			_ro = createRemoteObject(DEFAULT_DEST, DEFAULT_CHANNEL_URL);
		return _ro;
	 * Constructs a new remote object with new channel.
	 * @param roDestination Destination of the RemoteObject; should match a destination name in the services-config.xml file.
	 * @param channelURI the URI used to create the whole endpoint URI for this channel; this uri can be relative uri (to the folder containing the SWF).
	 * @param channelId the id of the channel, if set to null, a random Id will be assigned.
	protected static function createRemoteObject(roDestination:String, channelURI:String, channelId:String = null):RemoteObject {
		var channelSet:ChannelSet = new ChannelSet();
		var channel:AMFChannel = new AMFChannel(channelId == null ? "channel-" : channelId, channelURI);	//Create new Channel
		channelSet.addChannel(channel);	
		var ro:RemoteObject = new RemoteObject(roDestination);
		ro.channelSet = channelSet;
		return ro;

4 配置完成,.

在Java端建立相关的Class,

启动服务器.

在Flex端通过 Appcontext.getRemoteObject.getOperation(“方法名称”)来调用服务器端方法.

具体实例:

Java端的Class – 通过JDBC 读取 MySql中数据:

* Load all Department and return an ArrayList * @return public ArrayList loadDepartments() { ArrayList departmentsAL = new ArrayList(); log.info("Loading Departments..."); try { //Get Connection Connection conn = JdbcUtilities.getConnection(); //Create statement String sql = " SELECT * FROM Department d ORDER BY d.Department_ID"; PreparedStatement ps = conn.prepareStatement(sql); ResultSet res = ps.executeQuery(); log.debug("Exectuing: " + sql); while (res.next()) { int id = res.getInt("Department_ID"); String name = res.getString("name"); Department dp = new Department(); dp.setId(id); dp.setName(name); departmentsAL.add(dp); log.debug("从数据库获得部门: " + dp); JdbcUtilities.closeConn(res, ps, conn); log.info("加载部门信息结束, 共加载部门: " + departmentsAL.size()); } catch (Exception e) { log.error("SQL Error", e); throw new Error(e); return departmentsAL;

Flex端的代码:

* Load the Department's Employee private var op:AbstractOperation; public function loadEmployees():void { op = AppContext.getRemoteObject().getOperation("loadEmployees"); //获得Operation op.arguments = [id]; //设定参数 var at:AsyncToken = op.send(); //Send at.addResponder(this); //为本实例增加responder

Flex端responder剩余代码见:http://liguoliang.com/2009/02/777/

运行Flex工程, Flex取得RemoteObject, 然后通过channel建立到destination的连接,由Java读取数据库中信息, 并返回给Flex.

Newer Posts Older Posts