JDBC连接informix数据库相关操作

momoangel 贡献于2012-06-26

作者 微软用户  创建于2011-03-16 01:51:00   修改者Edward  修改于2012-06-10 09:35:00字数3896

文档摘要:JDBC的任务:同一个数据库建立连接;向数据库发送SQL语句;处理数据库返回的结果。
关键词:

 JDBC的任务: 1. 同一个数据库建立连接; 2. 向数据库发送SQL语句; 3. 处理数据库返回的结果。 JDBC基本工作步骤: Import the necessary classes Load the JDBC driver Identify the database source Allocate a “Connection” object (create) Allocate a “Statement” object (create) Execute a query using the “Statement” object Retrieve data from the returned “ResultSet” object Close the “ResultSet” object Close the “Statement” object Close the “Connection” object 由于用远程访问,所以需要一个ifxjbdc.jar包,将包放到指定目录下,用MyEclipse编译,代码如下: import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Connection; public class InformixDriver { public static void main(String args[]) throws ClassNotFoundException, SQLException{ String sql = "select * from pet"; Class.forName("com.informix.jdbc.IfxDriver"); Connection con = DriverManager.getConnection ("jdbc:informix-sqli://192.168.100.125:6666/test:informixserver=online_web;user=test;password=test654123"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ System.out.println(rs.getString("sex")); System.out.println(rs.getString("name")); } rs.close(); con.close(); } } 调试结果如下: Fluffy m Clasws m Fang f Bowser f Chirpy m Whistler 插入操作: import java.sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Jdbc { public static void main(String args[]) throws ClassNotFoundException, SQLException{ String sql = "select * from pet"; Class.forName("com.informix.jdbc.IfxDriver"); Connection con = DriverManager.getConnection("jdbc:informix-sqli://192.168.100.125:6666/test:informixserver=online_web;user=test;password=test654123"); Statement stmt = con.createStatement(); String r1 = "insert into pet values("+"'Lily','Lucy','dog','m')"; stmt.executeUpdate(r1); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ System.out.printf("%s ",rs.getString("name")); System.out.printf("%s ",rs.getString("owner")); System.out.printf("%s ",rs.getString("species")); System.out.println(rs.getString("sex")); } stmt.close(); con.close(); } } 运行结果: Fluffy Harold cat f Clasws Gwen cat m Fang Benny dog m Bowser Diane dog f Chirpy Gwen bird f Whistler Gwen dog m Lily Lucy dog m 删除操作: import java.sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Jdbc { public static void main(String args[]) throws ClassNotFoundException, SQLException{ String sql = "select * from pet"; Class.forName("com.informix.jdbc.IfxDriver"); Connection con = DriverManager.getConnection("jdbc:informix-sqli://192.168.100.125:6666/test:informixserver=online_web;user=test;password=test654123"); Statement stmt = con.createStatement(); String r1 = "delete from pet where name = 'Lily'"; stmt.executeUpdate(r1); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ System.out.printf("%s ",rs.getString("name")); System.out.printf("%s ",rs.getString("owner")); System.out.printf("%s ",rs.getString("species")); System.out.println(rs.getString("sex")); } stmt.close(); con.close(); } } 运行结果: Fluffy Harold cat f Clasws Gwen cat m Fang Benny dog m Bowser Diane dog f Chirpy Gwen bird f Whistler Gwen dog m 修改操作: import java.sql.DriverManager; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Jdbc { public static void main(String args[]) throws ClassNotFoundException, SQLException{ String sql = "select * from pet"; Class.forName("com.informix.jdbc.IfxDriver"); Connection con = DriverManager.getConnection("jdbc:informix-sqli://192.168.100.125:6666/test:informixserver=online_web;user=test;password=test654123"); Statement stmt = con.createStatement(); String r1 = "update pet set owner='Tom' where name='Lily'"; stmt.executeUpdate(r1); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ System.out.printf("%s ",rs.getString("name")); System.out.printf("%s ",rs.getString("owner")); System.out.printf("%s ",rs.getString("species")); System.out.println(rs.getString("sex")); } stmt.close(); con.close(); } } 运行结果如下: Fluffy Harold cat f Clasws Gwen cat m Fang Benny dog m Bowser Diane dog f Chirpy Gwen bird f Whistler Gwen dog m Lily Tom dog m 当然 前提是要在informix数据库上建立数据库表pet

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档