Skip to content

Commit ca485c2

Browse files
committed
add: using ErrorCode to throw SQLServerException
1 parent 928e9bf commit ca485c2

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package top.mryan2005.simplifiedjava.SQLs.Exceptions;
2+
3+
import java.sql.SQLException;
4+
5+
public class SQLServerDatabaseNotFoundException extends SQLException {
6+
public SQLServerDatabaseNotFoundException(String message) {
7+
super(message);
8+
}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package top.mryan2005.simplifiedjava.SQLs.Exceptions;
2+
3+
import java.sql.SQLException;
4+
5+
public class throwSQLServerException {
6+
public static void throwExeption(int ErrorCode, String message) throws SQLException {
7+
switch (ErrorCode) {
8+
case 4060:
9+
throw new SQLServerDatabaseNotFoundException(message);
10+
default:
11+
throw new SQLException(message);
12+
}
13+
}
14+
}

src/main/java/top/mryan2005/simplifiedjava/SQLs/SQLServer.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import com.microsoft.sqlserver.jdbc.SQLServerException;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
6+
import top.mryan2005.simplifiedjava.SQLs.Exceptions.SQLServerDatabaseNotFoundException;
67
import top.mryan2005.simplifiedjava.SQLs.Exceptions.SQLServerNotNULLException;
78
import top.mryan2005.simplifiedjava.SQLs.Exceptions.SQLServerPrimaryKeyException;
9+
import top.mryan2005.simplifiedjava.SQLs.Exceptions.throwSQLServerException;
810

911
import java.sql.*;
1012
import java.util.ArrayList;
@@ -18,11 +20,15 @@ public class SQLServer {
1820
public String database;
1921

2022
public Connection ConnectToSQLServer(String inputIp, String inputPort, String inputDatabase, String inputUsername, String inputPassword, boolean encrypt) throws ClassNotFoundException, SQLException {
21-
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver");
22-
con = DriverManager.getConnection("jdbc:sqlserver://" + inputIp + ":" + inputPort + ";databaseName=" + inputDatabase + ";user=" + inputUsername + ";password=" + inputPassword + ";encrypt=" + encrypt + ";");
23-
ip = inputIp;
24-
port = inputPort;
25-
database = inputDatabase;
23+
try {
24+
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
25+
con = DriverManager.getConnection("jdbc:sqlserver://" + inputIp + ":" + inputPort + ";databaseName=" + inputDatabase + ";user=" + inputUsername + ";password=" + inputPassword + ";encrypt=" + encrypt + ";");
26+
ip = inputIp;
27+
port = inputPort;
28+
database = inputDatabase;
29+
} catch (SQLException e) {
30+
throwSQLServerException.throwExeption(e.getErrorCode(), e.getMessage());
31+
}
2632
return con;
2733
}
2834

@@ -46,11 +52,15 @@ public ResultSet runSQL(String sql) throws SQLException {
4652
if (e.getMessage().matches("(.*)PRIMARY KEY(.*)")) {
4753
throw new SQLServerPrimaryKeyException(e.getMessage());
4854
} else if(e.getMessage().matches("(.*)INSERT(.*)")) {
49-
if(e.getMessage().matches("(.*)NULL(.*)")) {
55+
if (e.getMessage().matches("(.*)NULL(.*)")) {
5056
throw new SQLServerNotNULLException(e.getMessage());
51-
} else {
52-
throw e;
5357
}
58+
} else if(e.getMessage().matches("(.*)数据库(.*)不存在")) {
59+
throw new SQLServerDatabaseNotFoundException(e.getMessage());
60+
} else if(e.getMessage().matches("(.*)1111(.*)")) {
61+
throw new SQLServerDatabaseNotFoundException(e.getMessage());
62+
} else {
63+
throw e;
5464
}
5565
}
5666
if (result && stmt != null) {
@@ -88,7 +98,7 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
8898
SQLServer sqlServer = new SQLServer();
8999
sqlServer.ConnectToSQLServer("localhost", "1433", "sa", "123456", false);
90100
sqlServer.CloseConnection();
91-
sqlServer.ConnectToSQLServer("localhost", "1433", "master", "sa", "123456", false);
101+
sqlServer.ConnectToSQLServer("localhost", "1433", "1111", "sa", "123456", false);
92102
sqlServer.CloseConnection();
93103
}
94104
}

0 commit comments

Comments
 (0)