Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
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
沈俊林
webmagic
Commits
cf1b6113
Commit
cf1b6113
authored
Mar 22, 2018
by
shenjunlin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构代码
parent
ec77eca0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
10 deletions
+43
-10
MySQLTool.java
.../src/main/java/us/codecraft/webmagic/utils/MySQLTool.java
+43
-10
No files found.
webmagic-extension/src/main/java/us/codecraft/webmagic/utils/MySQLTool.java
View file @
cf1b6113
package
us
.
codecraft
.
webmagic
.
utils
;
import
com.alibaba.druid.pool.DruidDataSource
;
import
com.google.common.base.Preconditions
;
import
org.apache.commons.dbutils.QueryRunner
;
import
org.apache.commons.lang3.StringUtils
;
import
java.sql.SQLException
;
public
class
MySQLTool
{
private
DruidDataSource
dataSource
;
...
...
@@ -14,18 +15,50 @@ public class MySQLTool {
private
String
insertSql
;
public
MySQLTool
setInsertSql
(
String
insertSql
){
this
.
insertSql
=
insertSql
;
return
this
;
}
public
MySQLTool
(
String
url
,
String
userName
,
String
password
){
private
MySQLTool
(
Builder
builder
){
dataSource
=
new
DruidDataSource
();
dataSource
.
setDriverClassName
(
"com.mysql.jdbc.Driver"
);
dataSource
.
setUrl
(
url
);
dataSource
.
setUsername
(
userName
);
dataSource
.
setPassword
(
password
);
dataSource
.
setUrl
(
builder
.
url
);
dataSource
.
setUsername
(
builder
.
userName
);
dataSource
.
setPassword
(
builder
.
password
);
queryRunner
=
new
QueryRunner
(
dataSource
);
this
.
insertSql
=
builder
.
insertSql
;
}
public
static
class
Builder
{
private
String
url
;
private
String
userName
;
private
String
password
;
private
String
insertSql
;
public
Builder
setUrl
(
String
url
)
{
this
.
url
=
url
;
return
this
;
}
public
Builder
setUserName
(
String
userName
)
{
this
.
userName
=
userName
;
return
this
;
}
public
Builder
setPassword
(
String
password
)
{
this
.
password
=
password
;
return
this
;
}
public
Builder
setInsertSql
(
String
insertSql
)
{
this
.
insertSql
=
insertSql
;
return
this
;
}
public
MySQLTool
build
(){
Preconditions
.
checkArgument
(
StringUtils
.
isNotEmpty
(
this
.
url
),
"数据库连接信息不能为空"
);
Preconditions
.
checkArgument
(
StringUtils
.
isNotEmpty
(
this
.
userName
),
"数据库用户名不能为空"
);
Preconditions
.
checkArgument
(
StringUtils
.
isNotEmpty
(
this
.
password
),
"数据库密码不能为空"
);
Preconditions
.
checkArgument
(
StringUtils
.
isNotEmpty
(
this
.
insertSql
),
"数据库插入语句不能为空"
);
return
new
MySQLTool
(
this
);
}
}
public
void
insert
(
Object
...
params
)
throws
SQLException
{
...
...
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