java Jdbc連線oracle資料庫連線測試

ultradb發表於2016-10-22
 java  Jdbc連線資料庫連線測試
實際環境中發現java連線有問題,寫一測試用例,用於測試,內容如下:

Redhat java  Jdbc連線資料庫連線測試
vi   test.java


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.text.SimpleDateFormat;
public class test {
public static void main(String arg[]) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設定日期格式
int x = 1;
Date datestr = new Date(); 
String now_str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datestr);
Date d1 = new Date(); 
String now_str1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d1);
Date d2 = new Date(); 
String now_str2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d2);
while( x < 20 ) {
try {
d1 = new Date(); 
now_str1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d1);
System.out.println("第"+x+"次連線資料庫,開始連線資料資料庫時間點:"+now_str1 );
x++;
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.93.15)(PORT = 1523))(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.94.15)(PORT = 1523))(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.95.17)(PORT = 1523))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME =ljg)))";
Connection conn = DriverManager.getConnection(url,"ljg","ljg");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select utl_inaddr.get_host_address from dual");
while(rs.next()) {
System.out.println("資料庫ip地址為:"+rs.getString(1));
}
d2 = new Date(); 
now_str2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d2);
System.out.println("        連線資料庫,連線到資料資料庫時間點:"+now_str2);
long diff = d2.getTime() - d1.getTime();
long days = diff / (1000 * 60 * 60 * 24);
System.out.println("連線資料時長為:                                "+diff/1000+"秒");
System.out.println("連線資料時長為:                                "+days+"天");
 conn.close();//關閉資料庫


}catch(Exception e) {
System.out.println(e.toString());
}
}
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24179204/viewspace-2126920/,如需轉載,請註明出處,否則將追究法律責任。

相關文章