Skip to content

Commit 6edd981

Browse files
committed
add some things
1 parent 348aae6 commit 6edd981

File tree

4 files changed

+164
-1
lines changed

4 files changed

+164
-1
lines changed

src/main/java/SQL.java

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import SQLs.*;
2+
3+
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.Statement;
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
9+
public class SQL {
10+
public Connection con;
11+
public String type;
12+
public String ip;
13+
public String port;
14+
public String username;
15+
public String password;
16+
public String databaseName;
17+
public String connectionUrl;
18+
public Connection ConnectToSQL(String type, String ip, String port, String username, String password, String databaseName) {
19+
this.type = type;
20+
this.ip = ip;
21+
this.port = port;
22+
this.username = username;
23+
this.password = password;
24+
this.databaseName = databaseName;
25+
if("SQL Server".matches(type)) {
26+
try {
27+
SQLServer sql = new SQLServer();
28+
sql.ConnectToSQLServer(ip, port, databaseName, username, password);
29+
System.out.println("连接成功!");
30+
con = sql.getSQLer();
31+
} catch (Exception e) {
32+
System.out.println("连接数据库时发生错误!");
33+
System.out.println(e);
34+
}
35+
} else if("MySQL".matches(type)) {
36+
try {
37+
MySQL sql = new MySQL(ip, port, username, password, databaseName);
38+
System.out.println("连接成功!");
39+
con = sql.getSQLer();
40+
} catch (Exception e) {
41+
System.out.println("连接数据库时发生错误!");
42+
System.out.println(e);
43+
}
44+
}
45+
return con;
46+
}
47+
48+
public Connection ConnectToSQL(String type, String databaseName) {
49+
this.type = type;
50+
this.databaseName = databaseName;
51+
if("SQLite".matches(type)) {
52+
try {
53+
SQLite sql = new SQLite(databaseName);
54+
System.out.println("连接成功!");
55+
con = sql.getSQLer();
56+
} catch (Exception e) {
57+
System.out.println("连接数据库时发生错误!");
58+
System.out.println(e);
59+
}
60+
}
61+
return con;
62+
}
63+
64+
public void CloseConnection() {
65+
try {
66+
con.close();
67+
} catch (Exception e) {
68+
System.out.println("关闭数据库连接时发生错误!");
69+
System.out.println(e);
70+
}
71+
}
72+
73+
public ResultSet runSQL(String sql) {
74+
try {
75+
Statement stmt = con.createStatement();
76+
if(stmt.execute(sql)) {
77+
return stmt.getResultSet();
78+
} else {
79+
return null;
80+
}
81+
} catch (Exception e) {
82+
System.out.println("执行SQL语句时发生错误!");
83+
System.out.println(e);
84+
return null;
85+
}
86+
}
87+
88+
public HashMap<Object, ArrayList<Object>> runSQL(String sql, String primaryKey) {
89+
try {
90+
Statement stmt = con.createStatement();
91+
ResultSet rs = stmt.executeQuery(sql);
92+
HashMap<Object, ArrayList<Object>> result = new HashMap<>();
93+
while(rs.next()) {
94+
ArrayList<Object> row = new ArrayList<>();
95+
for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
96+
row.add(rs.getObject(i));
97+
}
98+
result.put(rs.getObject(primaryKey), row);
99+
}
100+
return result;
101+
} catch (Exception e) {
102+
System.out.println("执行SQL语句时发生错误!");
103+
System.out.println(e);
104+
return null;
105+
}
106+
}
107+
}

src/main/java/SQLs/MySQL.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package SQLs;
2+
3+
import java.sql.Connection;
4+
5+
public class MySQL {
6+
public MySQL(String ip, String port, String username, String password, String databaseName) {
7+
}
8+
9+
public Connection getSQLer() {
10+
}
11+
}

src/main/java/SQLs/SQLServer.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package SQLs;
22

3+
import javax.naming.ContextNotEmptyException;
34
import java.sql.*;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
47

58
public class SQLServer {
69
public Connection con;
@@ -10,9 +13,16 @@ public class SQLServer {
1013
public String username;
1114
public String password;
1215

13-
public void ConnectToSQLServer(String inputIp, String inputPort, String inputDatabase, String inputUsername, String inputPassword, boolean encrypt) throws ClassNotFoundException, SQLException {
16+
public Connection ConnectToSQLServer(String inputIp, String inputPort, String inputDatabase, String inputUsername, String inputPassword, boolean encrypt) throws ClassNotFoundException, SQLException {
1417
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver");
1518
con = DriverManager.getConnection("jdbc:sqlserver://" + inputIp + ":" + inputPort + ";databaseName=" + inputDatabase + ";user=" + inputUsername + ";password=" + inputPassword + ";encrypt=" + encrypt + ";");
19+
return con;
20+
}
21+
22+
public Connection ConnectToSQLServer(String inputIp, String inputPort, String inputDatabase, String inputUsername, String inputPassword) throws ClassNotFoundException, SQLException {
23+
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver");
24+
con = DriverManager.getConnection("jdbc:sqlserver://" + inputIp + ":" + inputPort + ";databaseName=" + inputDatabase + ";user=" + inputUsername + ";password=" + inputPassword + ";");
25+
return con;
1626
}
1727

1828
public void CloseConnection() throws SQLException {
@@ -28,6 +38,30 @@ public ResultSet runSQL(String sql) throws SQLException {
2838
}
2939
}
3040

41+
public Connection getSQLer() {
42+
return con;
43+
}
44+
45+
public HashMap<Object, ArrayList<Object>> runSQL(String sql, String primaryKey) {
46+
try {
47+
Statement stmt = con.createStatement();
48+
ResultSet rs = stmt.executeQuery(sql);
49+
HashMap<Object, ArrayList<Object>> result = new HashMap<>();
50+
while(rs.next()) {
51+
ArrayList<Object> row = new ArrayList<>();
52+
for(int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
53+
row.add(rs.getObject(i));
54+
}
55+
result.put(rs.getObject(primaryKey), row);
56+
}
57+
return result;
58+
} catch (Exception e) {
59+
System.out.println("执行SQL语句时发生错误!");
60+
System.out.println(e);
61+
return null;
62+
}
63+
}
64+
3165
public static void main(String[] args) throws ClassNotFoundException, SQLException {
3266
SQLServer sqlServer = new SQLServer();
3367
sqlServer.ConnectToSQLServer("localhost", "1433", "wuzhouDict", "sa", "123456", false);

src/main/java/SQLs/SQLite.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package SQLs;
2+
3+
import java.sql.Connection;
4+
5+
public class SQLite {
6+
public SQLite(String databaseName) {
7+
}
8+
9+
public Connection getSQLer() {
10+
}
11+
}

0 commit comments

Comments
 (0)