目前开发的RCP中需要对数据库进行操作,希望能在系统启动的时候进行连接,在整个系统运行的生命周期中只初始连接一次。 下面是自己写的一个java 对oracle数据库进行连接(thin方式)与操作的通用类。数据库连接初始化时只需对用户ID,用户Password、数据库主机地址、服务名几个静态变量进行赋值就行(我的做法是从配置文件中读取),然后就可对数据库进行操作。 代码如下: package edu.sengine.scheduler.oracleserver; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import org.apache.log4j.Logger; /** * @author yee * @since 2008-5-10 * @version $ revision:1.0 $ */ public class DBOperation { /**用户ID */ private static String useID; /**用户Password */ private static String usePassword; /**数据库主机地址 */ private static String ipAddress; /**服务名*/ private static String serviceName; /**数据库连接对象 */ private static Connection connection = null; /**数据库select结果 */ // private static ResultSet resultset = null; private static Logger logger = Logger.getLogger(DBOperation.class.toString()); /** * @param useID * The useID to set. */ public static void setUseID(String useID) { DBOperation.useID = useID; } /** * @param usePassword * The usePassword to set. */ public static void setUsePassword(String usePassword) { DBOperation.usePassword = usePassword; } /** * @param ipAdress * The ipAdress to set. */ public static void setIpAddress(String ipAdress) { DBOperation.ipAddress = ipAdress; } /** * @param serviceName * The serviceName to set. */ public static void setServiceName(String serviceName) { DBOperation.serviceName = serviceName; } /** * 连接数据库 */ public static Connection DBConnect() { String dbDriverStr = "oracle.jdbc.driver.OracleDriver"; String conStr = "jdbc:oracle:thin:@" + ipAddress + ":1521:" + serviceName; try { Class.forName(dbDriverStr); connection = DriverManager .getConnection(conStr, useID, usePassword); } catch (ClassNotFoundException e) { throw new RuntimeException( "ClassNotFoundException Exception encountered", e); } catch (SQLException e) { throw new RuntimeException("SQLException Exception encountered", e); } logger.info("数据库连接成功"); return connection; } /** * 数据库查询 * @param querySql 查询sql语句 * @return 查询结果集 */ public static ResultSet DoQuery(String querySql){ if (connection == null) { DBConnect(); } ResultSet resultset; try { resultset = connection.createStatement().executeQuery(querySql); } catch (SQLException e) { throw new RuntimeException("SQLException Exception encountered", e); } return resultset; } /** * 数据库插入 * @param insertSql 插入sql语句 * @return 所操作的表行数 */ public static int DoInsert(String insertSql) { if (connection == null) DBConnect(); int iSelect; try { iSelect = connection.createStatement().executeUpdate(insertSql); } catch (SQLException e) { throw new RuntimeException("SQLException Exception encountered", e); } logger.info("此次插入行数"+iSelect); return iSelect; } /** * 数据库更新操作 * @param updateSql 更新sql语句 * @return 所操作行数 */ public static int DoUpdate(String updateSql) { if (connection == null) DBConnect(); int iUpdate; try { iUpdate = connection.createStatement().executeUpdate(updateSql); } catch (SQLException e) { throw new RuntimeException("SQLException Exception encountered", e); } logger.info("此次更新行数"+iUpdate); return iUpdate; } /** * 数据库删除操作 * @param deleteSql 删除操作sql语句 * @return 所操作函数 */ public static int DoDelete(String deleteSql) { if (connection == null) DBConnect(); int iDelete; try { iDelete = connection.createStatement().executeUpdate(deleteSql); } catch (SQLException e) { throw new RuntimeException("SQLException Exception encountered", e); } logger.info("此次删除行数"+iDelete); return iDelete; } /** * 连接关闭 */ public static void DoClose() { try { if (connection != null) { connection.close(); connection = null; } } catch (SQLException e) { throw new RuntimeException("SQLException Exception encountered", e); } } }
Eclipse RCP中Oracle连接与操作
最新推荐文章于 2024-05-16 19:07:13 发布