笔者本人真正意义上接触编程开发是在2004年,最早用的就是VB,然后是Delphi等,后来转到.Net,中间断断续续还用过PowerBuilder等,无一例外,所研发设计的项目或系统都是WinForm应用程序,基于C/S模式的窗口应用程序开发那个时候还是正道,而Web开发还没有现在这么红火,技术也没有现在这么多姿多彩。
后来,B/S技术日渐成熟起来,jquery、node.js、flex、sPRing、structs、hibernate等技术框架相继涌现出来,进行Web开发的门槛降了下来,开发效率提高了许多,C/S开发慢慢地受到了冷落。
不过,在闲暇之余,笔者本人仍会时常设计一些窗口小程序,或者用WinForm程序开发的方式测试Web开发中遇到的算法问题。尤其是有些时候,笔者仅仅是想测试一个类或者接口方法,如果在Web工程中编码测试的话,你就要配置一大堆东西,最后还要启动个IIS,或者Tomcat,或者JBoss什么的,破费周折;当然了,你也可以脱离Web工程环境,写个类似dos的小程序也行,不过那样的话人机交互性实在太差劲。就好像下面这个窗口:
就以上弊病而言,WinForm开发算是有得天独厚的优势了,考虑到跨平台的问题,再加上最近一年笔者都在持续地研究大数据方面的技术,基本上都是在用java进行技术研发和探索,所以很自然地,Swing就成了首选技术。
感觉Swing好像被冷落了好多年了,这也难怪,从当初的JCreator、JBuilder到NetBean,以及现在的Eclipse等,C/S开发好像都是非主流的,B/S才是主流,而且RAD特性也远远没有微软的VS好。好在有一点它是非常突出的,那就是跨平台,可以轻松部署在linux系统中。
经过最近半年多试用Swing,笔者慢慢地积累了一些东西,断断续续的,偏偏面面的,各位不要太过于责怪,笔者会尽量将其归类整理,并给出相关的运行实例程序代码。
首先,在前面几个章节,我们会构建个Java基础功能包,介绍一些设计实现思路,封装设计一些接口方法,作为我们进行Swing应用开发的基础,有了这个基础功能包,我们接下来的Swing开发会有事半功倍的效果。
1、自定义JdbcTemplateHibernate中的JdbcTemplate组件功能已经很强悍了,不过现在我们要自定义设计个轻量级别的JdbcTemplate组件,作为我们Swing开发的基础功能包的一部分。我们主要对常用的关系数据库进行接口封装,包括Oracle和MySQL两种数据库。
1.1 数据库类型枚举类/*** Description: 数据库类型。<br>
* Copyright: Copyright (c) 2015<br>
* Company: 河南电力科学研究院智能电网所<br>
*@authorshangbingbing 2015-01-01编写
*@version1.0*/publicenumDBType {
ORACLE,
MYSQL
}
1.2 数据库JDBC操作类/*** Description: 关系数据库数据操作接口模板。<br>
* Copyright: Copyright (c) 2015<br>
* Company: 河南电力科学研究院智能电网所<br>
*@authorshangbingbing 2015-01-01编写
*@version1.0*/publicclassJdbcTemplate {privateDBType dataBaseType =DBType.ORACLE;privateString userName = "";privateString passWord= "";privateString driver = "";privateString connectionUrl = "";/*** 获取数据库用户名称
*@return*/publicString getUserName() {returnthis.userName;
}/*** 设置数据库用户名称
*@paramuserName*/publicvoidsetUserName(String userName) {this.userName =userName;
}/*** 获取数据库用户密码
*@return*/publicString getPassword() {returnthis.password;
}/*** 设置数据库用户密码
*@parampassword*/publicvoidsetPassword(String password) {this.password =password;
}/*** 获取数据库驱动程序
*@return*/privateString getDriver() {if(this.dataBaseType ==DBType.ORACLE) {this.driver = "oracle.jdbc.driver.OracleDriver";
}elseif(this.dataBaseType ==DBType.MYSQL) {this.driver = "com.mysql.jdbc.Driver";
}returndriver;
}/*** 获取数据库连接URL
*@return*/publicString getConnectionUrl() {returnthis.connectionUrl;
}/*** 设置数据库连接URL
*@paramurl*/publicvoidsetConnectionUrl(String url) {this.connectionUrl =url;
}/*** 默认构造函数。
*@paramdataBaseType*/publicJdbcTemplate() { }/*** 构造函数,并确定数据库类型。
*@paramdataBaseType*/publicJdbcTemplate(DBType dataBaseType) {this.dataBaseType =dataBaseType;
}/*** 数据库连接对象*/privateConnection conn =null;/*** 获取数据库连接对象
*@return*/publicConnection getConnection() {try{if(this.conn ==null||this.conn.isClosed()) {
Class.forName(this.getDriver());this.conn =DriverManager.getConnection(getConnectionUrl(),getUserName(),getPassword());
}
}catch(Exception e) {
e.printStackTrace();
}returnthis.conn;
}/*** SQL语句对象*/privateStatement stmt =null;/*** 执行增删改SQL语句
*@paramstrSql
*@return*/publicbooleanexecuteSql(String strSql) {if(strSql ==null||strSql.isEmpty()) {returnfalse;
}try{
LogInfoUtil.printLog(strSql);this.stmt =this.getConnection().createStatement();this.stmt.execute(strSql);//execute返回值出现问题,即使SQL执行成功也会返回false的。returntrue;
}catch(Exception e) {
e.printStackTrace();returnfalse;
}}/*** 批量执行增删该SQL语句
*@paramstrSqlList*/publicvoidexecuteSql(List<String>strSqlList) {if(strSqlList ==null|| strSqlList.size() == 0) {return;
}try{this.stmt =this.getConnection().createStatement();//每500作为一个插入批量操作intbatchSize = 500;intcount = 0;for(String strSql : strSqlList) {
LogInfoUtil.printLog(strSql);this.stmt.addBatch(strSql);if(++count % batchSize == 0) {this.stmt.executeBatch();
}
}//插入剩余的数据this.stmt.executeBatch();
}catch(Exception e) {
e.printStackTrace();
}}/*** 获取SQL检索语句结果集中的列名称及类型信息。
*@paramstrSql
*@return*/publicTreeMap<String,Integer>getColumnTypeList(String strSql) {
TreeMap<String,Integer> columnTypeList =newTreeMap<String,Integer>();try{
LogInfoUtil.printLog(strSql);this.stmt =this.getConnection().createStatement();
ResultSet rs=this.stmt.executeQuery(strSql);//获取数据集的列信息ResultSetMetaData rsmd =rs.getMetaData();intcolumnCount =rsmd.getColumnCount();for(inti=1; i<=columnCount; i++) {
String columnName=rsmd.getColumnName(i);
columnTypeList.put(columnName,rsmd.getColumnType(i));
}
rs.close();returncolumnTypeList;
}catch(Exception e) {
e.printStackTrace();returncolumnTypeList;
}}/*** 获取SQL检索语句结果集中的列名称及类型信息。
*@paramstrSql
*@return*/publicTreeMap<String,String>getColumnTypeNameList(String strSql) {
TreeMap<String,String> columnTypeList =newTreeMap<String,String>();try{
LogInfoUtil.printLog(strSql);this.stmt =this.getConnection().createStatement();
ResultSet rs=this.stmt.executeQuery(strSql);//获取数据集的列信息ResultSetMetaData rsmd =rs.getMetaData();intcolumnCount =rsmd.getColumnCount();for(inti=1; i<=columnCount; i++) {
String columnName=rsmd.getColumnName(i);
columnTypeList.put(columnName,rsmd.getColumnTypeName(i));
}
rs.close();returncolumnTypeList;
}catch(Exception e) {
e.printStackTrace();returncolumnTypeList;
}}/*** 执行SQL查询语句,将结果以List<Properties>形式组装返回.
*@paramstrSql
*@return*/publicList<Properties>executeQuery(String strSql) {
List<Properties> propertiesList =newArrayList<Properties>();if(strSql ==null||strSql.isEmpty()) {returnpropertiesList;
}try{
LogInfoUtil.printLog(strSql);this.stmt =this.getConnection().createStatement();
ResultSet rs=this.stmt.executeQuery(strSql);//获取数据集的列信息ResultSetMetaData rsmd =rs.getMetaData();
HashMap<String,Integer> columnList =newHashMap<String,Integer>();intcolumnCount =rsmd.getColumnCount();for(inti=1; i<=columnCount; i++) {
String columnName=rsmd.getColumnName(i);
columnList.put(columnName,rsmd.getColumnType(i));
}while(rs.next()) {
Properties properties=newProperties();for(String columnLabel : columnList.keySet()) {intcolumnType =columnList.get(columnLabel);
Object columnValue=null;switch(columnType) {caseTypes.VARCHAR :
columnValue=rs.getString(columnLabel);
break;caseTypes.NUMERIC:
columnValue=rs.getBigDecimal(columnLabel);
break;caseTypes.DATE:
columnValue= rs.getDate(columnLabel).toString() + " " +rs.getTime(columnLabel).toString();
break;caseTypes.NVARCHAR:
columnValue=rs.getNString(columnLabel);
break;caseTypes.DECIMAL:
columnValue=rs.getBigDecimal(columnLabel);
break;caseTypes.FLOAT:
columnValue=rs.getFloat(columnLabel);
break;caseTypes.DOUBLE:
columnValue=rs.getDouble(columnLabel);
break;caseTypes.BOOLEAN:
columnValue=rs.getBoolean(columnLabel);
break;caseTypes.INTEGER:
columnValue=rs.getInt(columnLabel);
break;caseTypes.BIGINT:
columnValue=rs.getLong(columnLabel);
break;caseTypes.TIME:
columnValue=rs.getTime(columnLabel);
break;caseTypes.BLOB:
columnValue=rs.getBlob(columnLabel);
break;caseTypes.CLOB:
columnValue=rs.getClob(columnLabel);
break;default:
columnValue=rs.getObject(columnLabel);
break;
}if(columnValue ==null) {
properties.put(columnLabel,"");
}else{
properties.put(columnLabel, columnValue);
}
}
propertiesList.add(properties);
}
rs.close();returnpropertiesList;
}catch(Exception e) {
e.printStackTrace();returnpropertiesList;
}}/*** 获取指定表中的记录总数
*@paramtableName 指定表名称
*@return表中的记录总数*/publicintgetTableRecordTotalCount(String tableName) {inttotalCount = 0;
String strSql= "select count(rowid) as RECORD_COUNT from " +tableName;try{
LogInfoUtil.printLog(strSql);this.stmt =this.getConnection().createStatement();
ResultSet rs=this.stmt.executeQuery(strSql);while(rs.next()) {
totalCount= rs.getInt("RECORD_COUNT");
}
rs.close();
}catch(Exception e) {
e.printStackTrace();
}returntotalCount;
}/*** 释放连接资源*/publicvoidclose() {try{if(this.stmt !=null) {this.stmt.close();this.stmt =null;
}if(this.conn !=null) {this.conn.close();this.conn =null;
}
}catch(Exception e) {
e.printStackTrace();
}
}/*** 读取指定的JDBC Properties文件,初始化数据连接信息。
*@paramjdbcPropertiesFile*/publicvoidinitJdbcTemplate(String jdbcPropertiesFile) {
HashMap<String,String> pps =PropertiesUtil.readProperties(jdbcPropertiesFile);
String url= pps.get("jdbc.url");
String userName= pps.get("jdbc.username");
String password= pps.get("jdbc.password");this.connectionUrl =url;this.userName =userName;this.password =password;
}
}
我们仅仅设计了几个常用的接口方法,看官可以根据自己的实际需要进行丰富和完善,譬如可以增加读写Blob字段的功能等等。
1.3 应用开发实例操作Oracle数据库JdbcTemplate jdbcTemplate =newJdbcTemplate();
或者
JdbcTemplate jdbcTemplate=newJdbcTemplate(DBType.ORACLE);
jdbcTemplate.setConnectionUrl(“jdbc:oracle:thin:@ ......”);
jdbcTemplate.setUserName(“admin”);
jdbcTemplate.setPassword(“admin”);
String strSql= "SELECT * FROM SYSTEM_USER";
List<Properties> list = jdbcTemplate.executeQuery(strSql);
操作MySql数据库JdbcTemplate jdbcTemplate =newJdbcTemplate(DBType.MYSQL);
jdbcTemplate.setConnectionUrl(“jdbc:mysql://10.231.45.34 ......”);jdbcTemplate.setUserName(“admin”);
jdbcTemplate.setPassword(“admin”);
String strSql= "SELECT * FROM SYSTEM_USER";
List<Properties> list = jdbcTemplate.executeQuery(strSql);
【完】
作者:商兵兵
单位:河南省电力科学研究院智能电网所
QQ:52190634
主页:http://www.cnblogs.com/shangbingbing
空间:http://shangbingbing.qzone.qq.com