3
3
import com .microsoft .sqlserver .jdbc .SQLServerException ;
4
4
import org .slf4j .Logger ;
5
5
import org .slf4j .LoggerFactory ;
6
+ import top .mryan2005 .simplifiedjava .SQLs .Exceptions .SQLServerDatabaseNotFoundException ;
6
7
import top .mryan2005 .simplifiedjava .SQLs .Exceptions .SQLServerNotNULLException ;
7
8
import top .mryan2005 .simplifiedjava .SQLs .Exceptions .SQLServerPrimaryKeyException ;
9
+ import top .mryan2005 .simplifiedjava .SQLs .Exceptions .throwSQLServerException ;
8
10
9
11
import java .sql .*;
10
12
import java .util .ArrayList ;
@@ -18,11 +20,15 @@ public class SQLServer {
18
20
public String database ;
19
21
20
22
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
+ }
26
32
return con ;
27
33
}
28
34
@@ -46,11 +52,15 @@ public ResultSet runSQL(String sql) throws SQLException {
46
52
if (e .getMessage ().matches ("(.*)PRIMARY KEY(.*)" )) {
47
53
throw new SQLServerPrimaryKeyException (e .getMessage ());
48
54
} else if (e .getMessage ().matches ("(.*)INSERT(.*)" )) {
49
- if (e .getMessage ().matches ("(.*)NULL(.*)" )) {
55
+ if (e .getMessage ().matches ("(.*)NULL(.*)" )) {
50
56
throw new SQLServerNotNULLException (e .getMessage ());
51
- } else {
52
- throw e ;
53
57
}
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 ;
54
64
}
55
65
}
56
66
if (result && stmt != null ) {
@@ -88,7 +98,7 @@ public static void main(String[] args) throws ClassNotFoundException, SQLExcepti
88
98
SQLServer sqlServer = new SQLServer ();
89
99
sqlServer .ConnectToSQLServer ("localhost" , "1433" , "sa" , "123456" , false );
90
100
sqlServer .CloseConnection ();
91
- sqlServer .ConnectToSQLServer ("localhost" , "1433" , "master " , "sa" , "123456" , false );
101
+ sqlServer .ConnectToSQLServer ("localhost" , "1433" , "1111 " , "sa" , "123456" , false );
92
102
sqlServer .CloseConnection ();
93
103
}
94
104
}
0 commit comments