Skip to content

Commit 11836d2

Browse files
committed
add: some Exceptions I know
1 parent 5049b01 commit 11836d2

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
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 SQLServerNotNULLException extends SQLException {
6+
public SQLServerNotNULLException(String message) {
7+
super(message);
8+
}
9+
}
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 SQLServerPrimaryKeyException extends SQLException {
6+
public SQLServerPrimaryKeyException(String message) {
7+
super(message);
8+
}
9+
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package top.mryan2005.simplifiedjava.SQLs;
22

3+
import com.microsoft.sqlserver.jdbc.SQLServerException;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import top.mryan2005.simplifiedjava.SQLs.Exceptions.SQLServerNotNULLException;
7+
import top.mryan2005.simplifiedjava.SQLs.Exceptions.SQLServerPrimaryKeyException;
8+
39
import java.sql.*;
410
import java.util.ArrayList;
511
import java.util.HashMap;
612

713
public class SQLServer {
14+
private static final Logger log = LoggerFactory.getLogger(SQLServer.class);
815
public Connection con;
916
public String ip;
1017
public String port;
@@ -30,8 +37,23 @@ public void CloseConnection() throws SQLException {
3037
}
3138

3239
public ResultSet runSQL(String sql) throws SQLException {
33-
Statement stmt = con.createStatement();
34-
if(stmt.execute(sql)) {
40+
boolean result = false;
41+
Statement stmt = null;
42+
try {
43+
stmt = con.createStatement();
44+
result = stmt.execute(sql);
45+
} catch (SQLServerException e) {
46+
if (e.getMessage().matches("(.*)PRIMARY KEY(.*)")) {
47+
throw new SQLServerPrimaryKeyException(e.getMessage());
48+
} else if(e.getMessage().matches("(.*)INSERT(.*)")) {
49+
if(e.getMessage().matches("(.*)NULL(.*)")) {
50+
throw new SQLServerNotNULLException(e.getMessage());
51+
} else {
52+
throw e;
53+
}
54+
}
55+
}
56+
if (result && stmt != null) {
3557
return stmt.getResultSet();
3658
} else {
3759
return null;

0 commit comments

Comments
 (0)