javaisnotnull
A. java里如何根據某個欄位是是否為空,從oracle資料庫中分別取出該欄位為空和不為空的數據呢
為空 sql 命令是 is null
不為空 sql 命令是 is not null
B. java如何根據word模板生成word文檔
首先是action的createDoc方法:
[java]
/**
* 通過HttpCient調用報告伺服器的方法生成報告 DOC
*/
public String createDoc() throws Exception {
//定義放回成功與否的判斷碼
String prMsg="";
// 獲取當前登錄的用戶
UserVo userVo = CommonUtils.getUserMessage();
//獲取模版類型
docType = Struts2Utils.getParameter("docType");
//重新創建文檔
String creatOrnot = Struts2Utils.getParameter("creatOrnot");
//獲取組組編號參數
workgroupId = Struts2Utils.getParameter("workgroupId");
//獲取評估用例實例ID參數
evtcaseInstId = Struts2Utils.getParameter("evtcaseInstId");
if(CommonUtils.isNotNull(docType)){
//獲取項目Id
projectId = Struts2Utils.getParameter("projectId");
if(!CommonUtils.isNotNull(projectId)){
if(CommonUtils.isNotNull(this.getIdFromSession("PM_PROJECTID"))){
projectId = this.getIdFromSession("PM_PROJECTID").toString();
}else{
Struts2Utils.getRequest().setAttribute("msg", "請先選擇項目!");
}
}
if(CommonUtils.isNotNull(projectId)){
prMsg = infoSystemDescService.downloadFileByUrl(projectId, userVo.getUserId(), workgroupId, evtcaseInstId, docType, creatOrnot);
}
}
return "docList";
}
註:在我貼出來的代碼中,能看懂就行了,有些不用管他(可能是其他業務方面的判斷),關於最後返回的prMsg---代表各種狀態 主要表示成功與否或者是出錯的信息。
接著我貼出service層的方法downloadFileByUrl
[java]
</pre><p></p><p></p><pre name="code" class="java"><pre name="code" class="java">/**
* 功能:
* 1.(生成報告文檔)
* 2.保存指定URL的源文件到指定路徑下
* @param projectId
* @param userId
* @param workgroupId
* @param evtcaseInstId
* @param docType
* @param creatOrnot
* @return
* @throws Exception
*/
@SuppressWarnings("deprecation")
public synchronized String downloadFileByUrl(String projectId,String userId,String workgroupId,String evtcaseInstId,String docType,String creatOrnot) throws Exception {
String msg = "1";//"1":默認為創建成功的提示信息 "2":標識創建失敗
String srcUrl = ""; //報告伺服器的執行路徑
HttpResponse response = null;
FileOutputStream out = null;
HttpClient httpclient = null;
HttpGet httpget = null;
long time1 = System.currentTimeMillis();
//獲取保存後的路徑
TProjDoc projDoc = projectDocDao.findFileByType(userId, Integer.parseInt(docType), Long.parseLong(projectId), workgroupId,evtcaseInstId);
if(projDoc == null || (projDoc != null && CommonUtils.isNotNull(creatOrnot) && creatOrnot.equals("1"))){ //FT_任務編號_[FID]
try {
//獲取報告伺服器的執行路徑
srcUrl = xmlPathDef.getActionUrl(docType, projectId,userId,workgroupId,evtcaseInstId);
HttpParams httpParams = new BasicHttpParams();
// 設置最大連接數
ConnManagerParams.setMaxTotalConnections(httpParams, 1);
// 設置獲取連接的最大等待時間
//ConnManagerParams.setTimeout(httpParams, 6000);
// 設置每個路由最大連接數
ConnPerRouteBean connPerRoute = new ConnPerRouteBean(1);
ConnManagerParams.setMaxConnectionsPerRoute(httpParams,connPerRoute);
// 設置連接超時時間
HttpConnectionParams.setConnectionTimeout(httpParams, 6000);
// 設置讀取超時時間
if(docType.toString().equals(XmlPathDef.SPOTTEST_DOC) && docType.toString().equals(XmlPathDef.FTEST_DOC)){
HttpConnectionParams.setSoTimeout(httpParams, 2400000);
}else{
HttpConnectionParams.setSoTimeout(httpParams, 600000);
}
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(httpParams, registry);
httpclient = new DefaultHttpClient(connectionManager, httpParams);
httpget = new HttpGet(srcUrl);
//執行返回
response = httpclient.execute(httpget);
//如果是本機既當伺服器,又當報表伺服器,那麼就只生成一遍
String ipvalues = xmlPathDef.getRepUrl();
if(CommonUtils.isNotNull(ipvalues)){
if(ipvalues.indexOf(":") != -1){
ipvalues = ipvalues.substring(0,ipvalues.lastIndexOf(":"));
}
}
HttpEntity entity = response.getEntity();
//獲取保存後的路徑
projDoc = projectDocDao.findFileByType(userId,Integer.parseInt(docType), Long.parseLong(projectId), workgroupId,evtcaseInstId);
String filePath = "";
if(projDoc != null)
filePath = projDoc.getPath();
if(CommonUtils.isNotNull(filePath)){
String basepath = XmlPathDef.getBasePath();
String outFilePath = (basepath + filePath).replaceAll("\\\\", "\\/");
XmlPathDef.isExists(outFilePath);
File wdFile = new File(outFilePath);
out = new FileOutputStream(wdFile);
int l;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
out.write(tmp, 0, l);
}
out.flush();
out.close();
System.out.println("****************************** ");
System.out.println("");
System.out.println("*************** 恭喜! 報告創建成功 結束 ***************");
System.out.println("");
}else{
msg = "8";//說明word創建成功,但是數據沒有保存成功
response = null;
}
}else{
msg = "2";
}
} catch (ClientProtocolException e) {
msg = "7";
e.printStackTrace();
} catch (IOException e) {
msg = "7";
logger.error("資料庫報告伺服器地址配置錯誤或網路不通!!2.連接是否超時" + e.getMessage());
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
msg = "7";
logger.error("資料庫報告伺服器地址配置錯誤或網路不通!!2.連接是否超時" + e.getMessage());
e.printStackTrace();
}
}
}
}
long time2 = System.currentTimeMillis();
long numTime = time2 - time1;
if(docType.toString().equals(XmlPathDef.SPOTTEST_DOC) && docType.toString().equals(XmlPathDef.FTEST_DOC)){
if(numTime >= 2401000){
msg = "9";
}
}else{
if(numTime >= 601000){
msg = "9";
}
}
System.out.println("");
String loggerinfo = "********* 報告類型為 :" + docType + " 執行時間為: " + (time2 - time1) /1000 + " 秒!***************";
System.out.println(loggerinfo);
System.out.println("");
System.out.println("*****************************");
logger.info(loggerinfo);
return msg;
}
C. java菜鳥求助,關於判斷輸入是否非空
1. 檢查字元串是否為空:
static boolean isBlank(CharSequence str) 判斷字元串是否為空或null;
static boolean isNotBlank(CharSequence str) 判斷字元串是否非空或非null;
java 的StringUtils包
StringUtils.isBlank(CharSequence str) 判斷字元串是否為空或null; 這么用就行
D. java notnull 怎麼實現的
not null指判斷某個對象非空
Object obj = new Object();
if(obj != null){
System.out.println("obj is not null");
}
E. java判斷字元串是否為空
方法一:if(變數.isEmpty())返回true表示,變數中存儲的字元串為空,返回false表示變數中存儲的的字元串不為空
方法二:if(變數.length()==0);判斷變數的長度,長度為0表示變數中存儲的字元串為空
方法三:if(變數.equals(""))使用equals比較值,相同返回true,相異返回false;
tips:不推薦用 「變數==null" 來對字元串進行判斷。
舉例: String str=""; 這種情況,雖然str中不存在值,但是也不等於null
於是得出結論: ==是用來判斷路徑是否相同