java jdbc連接數據庫,Properties 屬性設置參數方法


今天在整合為數據庫發現在配置中實現的賦值方式,可以用代碼實現。特記錄下共以后參考:

 

代碼:
        // 操作數據庫
        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();
            }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM