Skip to content

Commit d6c6d28

Browse files
committed
add SQLServer
1 parent 148f8d3 commit d6c6d28

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
<artifactId>commons-codec</artifactId>
4444
<version>1.6</version>
4545
</dependency>
46+
<dependency>
47+
<groupId>com.microsoft.sqlserver</groupId>
48+
<artifactId>mssql-jdbc</artifactId>
49+
<version>12.8.1.jre11</version>
50+
</dependency>
4651
</dependencies>
4752

4853
<build>

src/main/java/SQLs/SQLServer.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package SQLs;
2+
3+
import java.sql.*;
4+
5+
public class SQLServer {
6+
public Connection con;
7+
public String ip;
8+
public String port;
9+
public String database;
10+
public String username;
11+
public String password;
12+
13+
public void ConnectToSQLServer(String inputIp, String inputPort, String inputDatabase, String inputUsername, String inputPassword, boolean encrypt) throws ClassNotFoundException, SQLException {
14+
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver");
15+
con = DriverManager.getConnection("jdbc:sqlserver://" + inputIp + ":" + inputPort + ";databaseName=" + inputDatabase + ";user=" + inputUsername + ";password=" + inputPassword + ";encrypt=" + encrypt + ";");
16+
}
17+
18+
public void CloseConnection() throws SQLException {
19+
con.close();
20+
}
21+
22+
public ResultSet runSQL(String sql) throws SQLException {
23+
Statement stmt = con.createStatement();
24+
if(stmt.execute(sql)) {
25+
return stmt.getResultSet();
26+
} else {
27+
return null;
28+
}
29+
}
30+
31+
public static void main(String[] args) throws ClassNotFoundException, SQLException {
32+
SQLServer sqlServer = new SQLServer();
33+
sqlServer.ConnectToSQLServer("localhost", "1433", "wuzhouDict", "sa", "123456", false);
34+
sqlServer.runSQL("INSERT INTO dbo.[users] (id, username, password, name, role) VALUES (1, 'admin', 'password', 'admin', '1')");
35+
ResultSet rs = sqlServer.runSQL("SELECT * FROM dbo.[users]");
36+
while(rs.next()) {
37+
System.out.println(rs.getString("name"));
38+
}
39+
sqlServer.CloseConnection();
40+
}
41+
}

0 commit comments

Comments
 (0)