Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
poc-api
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
poc
poc-api
Commits
f659fed2
You need to sign in or sign up before continuing.
Commit
f659fed2
authored
Apr 29, 2025
by
alex yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: database数据表查询
parent
ce52568b
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
4 deletions
+33
-4
DatabaseUtil.java
src/main/java/cn/com/poc/common/utils/DatabaseUtil.java
+11
-3
DatabaseRestImpl.java
...java/cn/com/poc/knowledge/rest/impl/DatabaseRestImpl.java
+22
-1
No files found.
src/main/java/cn/com/poc/common/utils/DatabaseUtil.java
View file @
f659fed2
...
...
@@ -19,6 +19,8 @@ public class DatabaseUtil {
private
static
Logger
logger
=
LoggerFactory
.
getLogger
(
DatabaseUtil
.
class
);
private
static
final
String
DRIVER_NAME
=
"com.mysql.cj.jdbc.Driver"
;
private
static
final
int
CONNECT_TIMEOUT
=
5000
;
// 连接超时时间(毫秒)
private
static
final
int
SOCKET_TIMEOUT
=
30000
;
// socket超时时间(毫秒)
...
...
@@ -33,7 +35,7 @@ public class DatabaseUtil {
" AND TABLE_NAME = t.TABLE_NAME) AS TABLE_COLUMN "
+
"FROM INFORMATION_SCHEMA.TABLES t "
+
"WHERE TABLE_SCHEMA = ?"
;
;
/**
* 测试数据库连接
...
...
@@ -49,7 +51,10 @@ public class DatabaseUtil {
String
url
=
"jdbc:mysql://"
+
host
+
":"
+
port
+
"/"
+
database
+
"?connectTimeout="
+
CONNECT_TIMEOUT
// 连接超时(毫秒)
+
"&socketTimeout="
+
SOCKET_TIMEOUT
;
// socket超时(毫秒)
try
(
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
))
{
try
{
Class
.
forName
(
DRIVER_NAME
);
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
connection
.
close
();
return
true
;
}
catch
(
Exception
e
)
{
return
false
;
...
...
@@ -63,7 +68,9 @@ public class DatabaseUtil {
String
url
=
"jdbc:mysql://"
+
host
+
":"
+
port
+
"/"
+
database
+
"?connectTimeout="
+
CONNECT_TIMEOUT
// 连接超时(毫秒)
+
"&socketTimeout="
+
SOCKET_TIMEOUT
;
// socket超时(毫秒)
try
(
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
))
{
try
{
Class
.
forName
(
DRIVER_NAME
);
Connection
connection
=
DriverManager
.
getConnection
(
url
,
username
,
password
);
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
SQL_TABLE_INFO
);
preparedStatement
.
setString
(
1
,
database
);
ResultSet
resultSet
=
preparedStatement
.
executeQuery
();
...
...
@@ -76,6 +83,7 @@ public class DatabaseUtil {
tableInfo
.
setTABLE_COLUMN
(
resultSet
.
getInt
(
"TABLE_COLUMN"
));
tableInfoList
.
add
(
tableInfo
);
}
connection
.
close
();
return
tableInfoList
;
}
catch
(
Exception
e
)
{
logger
.
error
(
"连接数据库失败"
,
e
);
...
...
src/main/java/cn/com/poc/knowledge/rest/impl/DatabaseRestImpl.java
View file @
f659fed2
...
...
@@ -142,7 +142,28 @@ public class DatabaseRestImpl implements DatabaseRest {
List
<
KnowledgeDatabaseQueryInfoQueryItem
>
items
=
bizKnowledgeDatabaseService
.
queryKnowledgeDatabaseQueryInfo
(
condition
,
pagingInfo
);
List
<
BizKnowledgeDatabaseDto
>
result
=
new
ArrayList
<>();
if
(
CollectionUtils
.
isNotEmpty
(
items
))
{
result
=
items
.
stream
().
map
(
BizKnowledgeDatabaseConvert:
:
itemToDto
).
collect
(
Collectors
.
toList
());
result
=
items
.
stream
().
map
(
item
->
{
BizKnowledgeDatabaseDto
bizKnowledgeDatabaseDto
=
BizKnowledgeDatabaseConvert
.
itemToDto
(
item
);
List
<
DatabaseUtil
.
TableInfo
>
tableInfos
=
DatabaseTableInfoCache
.
getCache
(
item
.
getId
().
intValue
());
if
(
tableInfos
==
null
)
{
tableInfos
=
DatabaseUtil
.
getTableInfo
(
item
.
getDbHost
(),
item
.
getDbPort
(),
item
.
getDbUsername
(),
item
.
getDbPassword
(),
item
.
getDbName
());
if
(
tableInfos
==
null
)
{
return
null
;
}
bizKnowledgeDatabaseDto
.
setTableInfos
(
tableInfos
);
DatabaseTableInfoCache
.
updateOrSaveCache
(
item
.
getId
().
intValue
(),
tableInfos
);
}
return
bizKnowledgeDatabaseDto
;
}
).
collect
(
Collectors
.
toList
());
}
return
result
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment