码迷,mamicode.com
首页 > 数据库 > 详细

java jdbc连接数据库,Properties 属性设置参数方法

时间:2018-11-20 21:12:32      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:sqlserver   min   查询语句   get   rop   print   lse   next   jdbc   

今天在整合为数据库发现在配置中实现的赋值方式,可以用代码实现。特记录下共以后参考:

 

代码:
        // 操作数据库
        Connection conn;

        String strDatabase ="northeasttycoon";
            try {
                String url = "jdbc:sqlserver:127.0.0.1:1433;DatabaseName=strDatabase;";
                Properties pro = new Properties();
                pro.setProperty("initialSize", "10");
                pro.setProperty("maxActive", "100");
                pro.setProperty("maxIdle", "70");
                pro.setProperty("minIdle", "10");
                pro.setProperty("testOnBorrow", "true");

                pro.setProperty("validationQuery", "select 1");
                pro.setProperty("removeAbandonedTimeout", "120");
                pro.setProperty("removeAbandoned", "true");
                pro.setProperty("username", strUserName);
                pro.setProperty("password", strPassWord);

                conn = DriverManager.getConnection(url, pro);
                // Statement stmt;
                PreparedStatement stmt;
                ResultSet rs;

                String sql = "select * from  t_northeasttycoon";

                // 建立Statement对象
                stmt = conn.prepareStatement(sql);

                /**
                 * Statement createStatement() 创建一个 Statement 对象来将 SQL 语句发送到(northeasttycoon)数据库。
                 */
                // 执行数据库查询语句
                rs = stmt.executeQuery();
                /**
                 * ResultSet executeQuery(String sql) throws SQLException 执行给定的
                 * SQL 语句,该语句返回单个 ResultSet 对象
                 */
                while (rs.next()) {
        // 查询结果
                }
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
                if (stmt != null) {
                    stmt.close();
                    stmt = null;
                }
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

java jdbc连接数据库,Properties 属性设置参数方法

标签:sqlserver   min   查询语句   get   rop   print   lse   next   jdbc   

原文地址:https://www.cnblogs.com/northeastTycoon/p/9991545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!