以下是最近整理的数据库监控资料,经测试正常,支持MySql,Oracle,SqlServer
package com.bitnet.test.jkgl;
import java.sql.Connection;
import java.sql.DriverManager;
public class sjkjkZtjk {
//********SqlService数据库连接测试*****************
private static String ip="10.0.2.201";
private static int port=1433;
private static String database="asd";
private static String type="SqlServer";
private static String username="sa";
private static String password="sa";
@SuppressWarnings("static-access")
public static void main(String args[]){
sjkjkZtjk sjkjk = new sjkjkZtjk();
boolean sjkjkzt=sjkjk.isHostConnectable(ip, port,database,type,username,password);
if(sjkjkzt==true){
System.out.println("连接成功,服务正常!");
}else{
System.out.println("连接失败,服务异常!");
}
}
public static boolean isHostConnectable(String ip, int port,String database,String type,String username1,String password1) {
String driver = null;
String URL = null;
String userName=username;
String passWord=password;
Connection conn = null;
if(type.equals("MySql")){
driver="com.mysql.jdbc.Driver";
URL = "jdbc:mysql://"+ip+":"+port+"/"+database+"";
}else if(type.equals("Oracle")){
driver="oracle.jdbc.driver.OracleDriver";
URL = "jdbc:oracle:thin:@"+ip+":"+port+":"+database+"";
}else if(type.equals("SqlServer")){
driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
URL = "jdbc:sqlserver://"+ip+":"+port+";DatabaseName="+database+"";
}
boolean sjkljzt=false;
try {
Class.forName(driver);
} catch (java.lang.ClassNotFoundException e) {
System.out.println("Connect Successful.");
System.out.println("Cant't load Driver");
sjkljzt=false;
}
try {
conn = DriverManager.getConnection(URL, userName, passWord);
System.out.println("Connect Successful.");
sjkljzt=true;
} catch (Exception e) {
System.out.println("Connect fail:" + e.getMessage());
sjkljzt=false;
}finally {
try {
conn.close();
} catch (Exception e2) {
System.out.println("Close Connection error.");
}
}
return sjkljzt;
}
}