The regular expression judges whether the port and host are qualified, and the connection time to the database is too long. (layui)
Determine whether the port is qualified
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("端口不合格")
}
Determine whether the host is normal
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("主机不合格")
}
The problem that Java connects to the database for too long and timeout is solved as follows:
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. The database connection test loads the driver class
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);
}
}