Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
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
0
Merge Requests
0
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
曲欣亮
common
Commits
173cb855
Commit
173cb855
authored
Aug 17, 2021
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
ae09ddfe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
191 additions
and
0 deletions
+191
-0
DBPort.java
src/main/java/com/egolm/common/jdbc/DBPort.java
+191
-0
No files found.
src/main/java/com/egolm/common/jdbc/DBPort.java
0 → 100644
View file @
173cb855
package
com
.
egolm
.
common
.
jdbc
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.ObjectInputStream
;
import
java.io.ObjectOutputStream
;
import
java.io.OutputStream
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.FileChannel
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.ResultSetMetaData
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.Base64
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.zip.GZIPInputStream
;
import
java.util.zip.GZIPOutputStream
;
import
javax.sql.DataSource
;
public
class
DBPort
{
private
DataSource
dataSource
;
public
DBPort
(
DataSource
dataSource
)
{
this
.
dataSource
=
dataSource
;
}
public
void
doImport
(
File
file
,
String
tableName
)
throws
IOException
,
SQLException
,
ClassNotFoundException
{
FileInputStream
fis
=
null
;
try
{
fis
=
new
FileInputStream
(
file
);
FileChannel
channel
=
fis
.
getChannel
();
ByteBuffer
byteBuffer
=
ByteBuffer
.
allocate
((
int
)
channel
.
size
());
int
i
=
1
;
while
(
i
>
0
)
{
i
=
channel
.
read
(
byteBuffer
);
}
this
.
doImport
(
byteBuffer
.
array
(),
tableName
);
}
finally
{
if
(
fis
!=
null
)
{
fis
.
close
();
}
}
}
public
void
doExport
(
File
file
,
String
sql
,
Object
[]
args
)
throws
IOException
,
SQLException
{
if
(!
file
.
getParentFile
().
exists
())
{
file
.
getParentFile
().
mkdirs
();
}
FileOutputStream
fos
=
new
FileOutputStream
(
file
);
this
.
doExport
(
fos
,
sql
,
args
);
}
public
void
doExport
(
OutputStream
os
,
String
sql
,
Object
[]
args
)
throws
IOException
,
SQLException
{
try
{
os
.
write
(
this
.
doExport
(
sql
,
args
));
os
.
flush
();
}
finally
{
if
(
os
!=
null
)
{
os
.
close
();
}
}
}
public
byte
[]
doExport
(
String
sql
,
Object
[]
args
)
throws
IOException
,
SQLException
{
Connection
connection
=
dataSource
.
getConnection
();
PreparedStatement
ps
=
connection
.
prepareStatement
(
sql
);
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
Object
argObj
=
args
[
i
];
if
(
argObj
instanceof
java
.
util
.
Date
)
{
java
.
util
.
Date
argDate
=
(
java
.
util
.
Date
)
argObj
;
ps
.
setObject
(
i
+
1
,
new
java
.
sql
.
Timestamp
(
argDate
.
getTime
()));
}
else
if
(
argObj
.
getClass
().
isEnum
())
{
ps
.
setObject
(
i
+
1
,
args
[
i
].
toString
());
}
else
{
ps
.
setObject
(
i
+
1
,
args
[
i
]);
}
}
ResultSet
resultSet
=
ps
.
executeQuery
();
ResultSetMetaData
metaData
=
resultSet
.
getMetaData
();
int
columnCount
=
metaData
.
getColumnCount
();
ArrayList
<
Map
<
String
,
Map
<
String
,
Object
>>>
datas
=
new
ArrayList
<
Map
<
String
,
Map
<
String
,
Object
>>>();
while
(
resultSet
.
next
())
{
Map
<
String
,
Map
<
String
,
Object
>>
rowData
=
new
LinkedHashMap
<
String
,
Map
<
String
,
Object
>>();
for
(
int
i
=
1
;
i
<=
columnCount
;
i
++)
{
String
colType
=
metaData
.
getColumnTypeName
(
i
);
String
columnLabel
=
metaData
.
getColumnLabel
(
i
);
if
(
columnLabel
==
null
||
columnLabel
.
trim
().
length
()
==
0
)
{
columnLabel
=
metaData
.
getColumnName
(
i
);
}
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"type"
,
colType
);
map
.
put
(
"value"
,
resultSet
.
getObject
(
i
));
rowData
.
put
(
columnLabel
,
map
);
}
datas
.
add
(
rowData
);
}
ByteArrayOutputStream
bos
=
null
;
GZIPOutputStream
gzip
=
null
;
ObjectOutputStream
out
=
null
;
try
{
bos
=
new
ByteArrayOutputStream
();
gzip
=
new
GZIPOutputStream
(
bos
);
out
=
new
ObjectOutputStream
(
gzip
);
out
.
writeObject
(
datas
);
out
.
flush
();
gzip
.
flush
();
gzip
.
finish
();
bos
.
flush
();
return
bos
.
toByteArray
();
}
finally
{
if
(
out
!=
null
)
{
try
{
out
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
bos
!=
null
)
{
try
{
bos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
void
doImport
(
byte
[]
data
,
String
tableName
)
throws
IOException
,
ClassNotFoundException
,
SQLException
{
ByteArrayInputStream
bis
=
new
ByteArrayInputStream
(
data
);
GZIPInputStream
gzip
=
new
GZIPInputStream
(
bis
);
ObjectInputStream
out
=
new
ObjectInputStream
(
gzip
);
List
<?>
datas
=
(
List
<?>)
out
.
readObject
();
StringBuffer
sql
=
new
StringBuffer
(
"insert into "
).
append
(
tableName
).
append
(
"("
);
Object
first
=
datas
.
get
(
0
);
Map
<?,
?>
firstMap
=
(
Map
<?,
?>)
first
;
int
columnIndex
=
0
;
for
(
Object
keyObject
:
firstMap
.
keySet
())
{
String
columnName
=
(
String
)
keyObject
;
sql
.
append
(
columnName
);
if
(
columnIndex
<
firstMap
.
size
()
-
1
)
{
sql
.
append
(
", "
);
}
else
{
sql
.
append
(
") values ("
);
for
(
int
k
=
0
;
k
<
firstMap
.
size
();
k
++)
{
sql
.
append
(
"?"
);
if
(
k
<
firstMap
.
size
()
-
1
)
{
sql
.
append
(
", "
);
}
else
{
sql
.
append
(
")"
);
}
}
}
columnIndex
+=
1
;
}
Connection
connection
=
dataSource
.
getConnection
();
connection
.
setAutoCommit
(
false
);
PreparedStatement
ps
=
connection
.
prepareStatement
(
sql
.
toString
());
for
(
int
i
=
0
;
i
<
datas
.
size
();
i
++)
{
Object
obj
=
datas
.
get
(
i
);
Map
<?,
?>
map
=
(
Map
<?,
?>)
obj
;
int
index
=
0
;
for
(
Object
keyObject
:
map
.
keySet
())
{
index
+=
1
;
Object
colObj
=
map
.
get
(
keyObject
);
Map
<?,
?>
columnMap
=
(
Map
<?,
?>)
colObj
;
String
columnType
=
(
String
)
columnMap
.
get
(
"type"
);
Object
columnValue
=
columnMap
.
get
(
"value"
);
if
(
columnType
.
startsWith
(
"image"
)
||
columnType
.
startsWith
(
"blob"
))
{
columnValue
=
new
ByteArrayInputStream
(
Base64
.
getDecoder
().
decode
((
String
)
columnValue
));
}
ps
.
setObject
(
index
,
columnValue
);
}
ps
.
addBatch
();
}
connection
.
commit
();
}
}
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