package com.index.util; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; import net.sf.json.JSONSerializer; public class HisDbConnection { private Connection dbConnection = null; private Statement selectPro = null; // 用于 select 操作 private Statement updatePro = null; // 用于 update 操作 private ResultSet dbResultSet = null; // 操作 select 结果集 private String driverName;// 声明oracle驱动类 private String dbHost; private String dbPort; private String dbName; private String dbUserName; private String dbPassword; private String enCoding; private Integer count; /** * 实例化DbConnection对象 * * @param host * 数据库主机(IP) * @param port * 数据库端口 * @param dName * 数据库名称 * @param uName * 用户名 * @param password * 用户密码 * @throws IOException */ public HisDbConnection() throws IOException { Properties p = new Properties(); p.load(CreatTable.class .getResourceAsStream("/com/index/util/His.properties")); driverName = "oracle.jdbc.driver.OracleDriver"; dbHost = p.getProperty("hisHost"); dbPort = p.getProperty("hisPort"); dbName = p.getProperty("hisName"); dbUserName = p.getProperty("hisUserName"); dbPassword = p.getProperty("hisPassword"); //enCoding = "?useUnicode=true&characterEncoding=utf-8&autoReconnect=true"; }// end DbConnection(...) public Connection getConnection() throws SQLException { StringBuilder urlTem = new StringBuilder(); urlTem.append("jdbc:oracle:thin:@"); urlTem.append(dbHost); urlTem.append(":"); urlTem.append(dbPort); urlTem.append(":"); urlTem.append(dbName); //urlTem.append(enCoding); String url = urlTem.toString(); System.out.println("url========="+url); try { Class.forName(driverName).newInstance(); dbConnection = DriverManager.getConnection(url, dbUserName, dbPassword); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return dbConnection; } /** * 执行专门的select操作,注意:在selectSql中的字段和fields中的个数、名称要保持一致 * * @param selectSql * 用于执行的select语句 * @param fields * 要选择的字段 * @return 含有Map的ArrayList,一条记录形成一个Map */ public ArrayList dbSelect(String selectSql, ArrayList fields) { ArrayList<Map> selectResult = new ArrayList<Map>(); Map<String, String> recordInfo; try { selectPro = dbConnection.createStatement();// 定义Statement对象 dbResultSet = selectPro.executeQuery(selectSql); while (dbResultSet.next()) { recordInfo = new HashMap<String, String>(); for (int i = 0; i < fields.size(); i++) if(dbResultSet.getString((String) fields.get(i))==null){ recordInfo.put((String) fields.get(i), ""); }else{ recordInfo.put((String) fields.get(i), new String(dbResultSet .getString((String) fields.get(i)).toString().getBytes("iso-8859-1"),"GBK" ));//这里的数据转码很重要!!! } selectResult.add(recordInfo); } System.out.println(selectResult); dbResultSet.close(); // 断开结果集 selectPro.close(); // 断开Statement对象 }catch (Exception e) { System.out.println("选择操作失败"); System.out.println("Sql = " + selectSql); e.printStackTrace(); } return selectResult; }// end dbSelect(...) } 2、HKEY_LOCAL_MACHINE/SOFTWARE/ORACLE/HOME0/ORACLE/KEY_OraDb10g_home1下NLS_LANG改为:AMERICAN_AMERICA.US7ASCII