DriverManager不通过框架链接数据库

    public List<SalesRegisterConfig> getSalesRegisterConfigList()
    {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<SalesRegisterConfig> resultList = new ArrayList<>();
        try{
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
             String linkCloudSql = ConfigUtil.pro.get("linkCloudSql").toString();//数据库IP地址
             String linkCloudUsername =ConfigUtil.pro.get("linkCloudUsername").toString();//数据库用户名
             String linkCloudPassword =ConfigUtil.pro.get("linkCloudPassword").toString();//数据库密码
             
             conn = DriverManager
                     .getConnection(
                             linkCloudSql,linkCloudUsername, linkCloudPassword);
             stmt = conn.createStatement();
             rs = stmt.executeQuery("select vin,* from SalesRegisterConfig where vin is not null and len(vin)=7");
             while(rs.next())
             {
                 SalesRegisterConfig config = new SalesRegisterConfig();
                 config.setId(rs.getInt("ID"));
                 config.setCarserialsid(rs.getString("carSerialsID"));
                 config.setCartypeid(rs.getString("carTypeID"));
                 config.setVin(rs.getString("vin"));
                 resultList.add(config);
             }
        }
         catch (Exception e) {
            e.printStackTrace();
        } finally {
             try {
                    if (!conn.isClosed()) {
                        conn.close();
                    }
                    if (!stmt.isClosed()) {
                        stmt.close();
                    }
                    if (!rs.isClosed()) {
                        rs.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
        }
         return resultList;
    }

 

posted @ 2017-07-27 11:26  JLCUI  阅读(113)  评论(0编辑  收藏  举报