今天在整合為數據庫發現在配置中實現的賦值方式,可以用代碼實現。特記錄下共以后參考:
代碼:
// 操作數據庫
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();
}