• java手动连接数据库


    配置连接信息(也可包括其他信息)  config.properties

    uploadResultFileDirs= e://
    uploadFileDirs=e://
    QRPath=e://
    BoxPath=E://BoxQR//
    photoURL=e://ots//img//photo//
    fileURL=e://ots//file//
    defaultPassword = 111111
    personImageURL = e://ots//img//person//
    apiURL=http://localhost:8080/
    drugTxmUrl=D:\txm\drug\
    drugboxUrl=D:\txm\drugbox\


    #HISu7CFBu7EDFu6570u636Eu5E93
    driver_his=oracle.jdbc.driver.OracleDriver
    #url_his=jdbc:oracle:thin:@172.50.249.8:1521:wjm
    #username_his=XYUSER
    #password_his=XYUSER
    url_his=jdbc:oracle:thin:@10.10.10.236:1521:orcl
    username_his=HIS
    password_his=HIS

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    读取配置文件属性工具类  PropertiesUtil

    package cn.xydata.pharmacy.util;

    import java.io.InputStream;
    import java.util.Properties;

    public class PropertiesUtil {

    /**
    * 根据key获取value
    * @param key
    * @return
    */
    public static String getValue(String key){
    try {
    InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("cn/xydata/pharmacy/config.properties");
    Properties properties = new Properties();
    properties.load(in);
    String value = properties.getProperty(key)==null?"":properties.getProperty(key).toString();
    return value;
    } catch (Exception e) {
    return "";
    }
    }

    public static void main(String[] args) {
    System.out.println(PropertiesUtil.getValue("QRPath"));
    }

    }

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    连接数据库工具类   DbConnectionUtil 

    package cn.xydata.pharmacy.util;

    import java.sql.Connection;
    import java.sql.SQLException;

    import org.springframework.jdbc.datasource.DriverManagerDataSource;

    public class DbConnectionUtil {

    public static Connection getDb(){
    Connection connection = null;
    DriverManagerDataSource ds = new DriverManagerDataSource ();
    ds.setDriverClassName(PropertiesUtil.getValue("driver_his"));    //获取参数值
    ds.setUrl(PropertiesUtil.getValue("url_his"));
    ds.setUsername(PropertiesUtil.getValue("username_his"));
    ds.setPassword(PropertiesUtil.getValue("password_his"));
    try {
    connection = ds.getConnection();
    System.out.println("连接HIS系统数据库成功");
    } catch (SQLException e) {
    System.out.println("连接HIS系统数据库异常:"+e);
    }
    return connection;
    }
    }

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    使用连接实例 

    /**
    * 药品同步
    * @throws SQLException
    * @see cn.xydata.pharmacy.service.stout.HairMedicineService#getDataTemp()
    */
    @Override
    public boolean getDataTemp() throws Exception {
    boolean isOk = true;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    //获取链接
    Connection connection = DbConnectionUtil.getDb();
    try {
    String sql = "SELECT drugtype AS drugType,drugname AS drugName,drugcode AS drugCode,specifications AS specifications,unit AS unit,storageunit AS storageUnit,"
    + "storagequantity AS storageQuantity,pharmacyunit AS pharmacyUnit,pharmacyquantity AS pharmacyQuantity,origin AS origin,ITEMTYPE AS type,"
    + "dosageforms AS dosageForms,commonname AS commonName,dosage AS dosage,dosageunit AS dosageUnit,approvalno AS approvalNo,remark AS remark,createtime AS createTime,"
    + "MZSY,ZYSY,DJ FROM VIEW_BASEDATA_DRUG WHERE 1 = 1";

    stmt = connection.prepareStatement(sql);
    System.out.println(sql);
    rs = stmt.executeQuery();
    int i = 0;
    boolean flag = false;
    while (rs.next()) {
    i++;
    DrugBaseDto dd = new DrugBaseDto();
    dd.setDrugName(rs.getString("drugName"));
    dd.setDrugCode(rs.getString("drugCode"));
    dd.setSpecifications(rs.getString("specifications"));
    dd.setUnit(rs.getString("unit"));
    /***由于起初视图有误字段对换导致其他处代码去的字段颠倒,视图修改后同步估计将字段颠倒***********************************/
    dd.setStorageUnit(rs.getString("storageQuantity"));
    dd.setStorageQuantity(rs.getString("storageUnit"));
    dd.setPharmacyUnit(rs.getString("pharmacyQuantity"));
    dd.setPharmacyQuantity(rs.getString("pharmacyUnit"));
    /*********************************************************/
    dd.setOrigin(rs.getString("origin"));
    dd.setType(rs.getString("type"));
    dd.setDosageForms(rs.getString("dosageForms"));
    dd.setCommonName(rs.getString("commonName"));
    dd.setDosage(rs.getString("dosage"));
    dd.setDosageUnit(rs.getString("dosageUnit"));
    dd.setApprovalNo(rs.getString("approvalNo"));
    dd.setRemark(rs.getString("remark"));
    dd.setCreateTime(rs.getTimestamp("createTime"));
    dd.setMzsy(rs.getString("mzsy"));
    dd.setZysy(rs.getString("zysy"));
    dd.setDj(rs.getString("dj"));
    //设置pinyin
    String regEx="[`~!@#$^&*()+=|{}';',\[\].<>/?~!@#¥……&*()——+|{}【】‘;:”“’。,、?☆▲]";
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(dd.getDrugName());
    String drugName = m.replaceAll("").trim();
    String str = PinYinUtil.getPinYinHeadChar(drugName);
    dd.setPinyin(str.toLowerCase());
    //根据类型添加 drug:西药 item:材料
    if(rs.getString("type").equals("drug")){
    dd.setDrugType("30");
    int dcount = hairMedicineDao.findCount(rs.getString("drugCode"));
    if(dcount>0){
    hairMedicineDao.UpdateDrug(dd);
    }else{
    hairMedicineDao.saveDrug(dd);
    flag = true;
    }
    }else{
    dd.setDrugType("40");
    int icount = hairMedicineDao.findICount(rs.getString("drugCode"));
    if(icount>0){
    hairMedicineDao.UpdateItem(dd);
    }else{
    hairMedicineDao.saveItem(dd);
    flag = true;
    }
    }

    }
    System.out.println(i);
    if(flag==true){
    sendMessage();
    }
    } catch (Exception e) {
    e.printStackTrace();
    isOk = false;
    }finally {
    stmt.close();
    rs.close();
    connection.close();
    }

    return isOk;

    }

    当能力支撑不了野心时,就该静下心来学习!
  • 相关阅读:
    理解消息循环和窗口过程(转)
    对话框和控件编程(转)
    俄罗斯方块
    男生女生配(抽屉原理)
    翻转吧,字符串
    数塔
    Pseudoprime numbers伪素数(快速幂+判定素数)
    shǎ崽 OrOrOrOrz
    As Easy As A+B
    求素数(筛选法)
  • 原文地址:https://www.cnblogs.com/1234cjq/p/7872267.html
Copyright © 2020-2023  润新知