正则表达式判断端口,主机是否合格,连接数据库时间超长解决。(layui)
判断端口是否合格
var reg=/^(0-9]|1-9]\d{1,3}|1-5]\d{4}|60-4]\d{4}|650-4]\d{2}|6550-2]\d|65530-5])$/
if(reg.test(value)==false){
console.log("端口不合格")
}
判断主机是否正常
var reg=/^((20-4]\d|250-5]|01]?\d\d?)\.){3}(20-4]\d|250-5]|01]?\d\d?)$/
if(reg.test(value)==false){
console.log("主机不合格")
}
Java连接数据库时间过长超时的问题,解决如下:
public static boolean ConnectTime(String ip,int port){
Socket socket=new Socket();
try{
SocketAddress ipaddress=new InetSocketAddress(ip,port);
socket.connect(ipaddress,3000);
System.out.println("连接成功!");
return true;
}catch(SocketTimeoutException e){
if(!socket.isClosed()&&socket.isConnected()){
System.out.println("读取超时");
}else{
System.out.println("连接超时")
}
return false;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String] args){
boolean connectTime=ConnectTime("29.22.25.144",1521);
if(!connectTime){
System.out.println("连接失败");
}else{
System.out.println("该主机和端口可以使用");
}
}
2.数据库连接测试加载驱动类
public class DBConnectTest{
Connection conn;
Statement stmt;
public DBConnectTest(String url,String user,String pwd){
try{
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.setLoginTimeout(3);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
conn=DriverManager.getConnection(url,user,pwd);
stmt=conn.createStatement();
System.out.println("连接成功");
}catch(Exception e){
System.out.println("连接出错!")
e.printStackTrace();
}
}
public static void main(String] args){
String url="jdbc:sqlserver://29.22.25.144:1433;DatabaseName=master";
String user="sa";
String pwd="123456";
new DBConnectTest(url,user,pwd);
}
}