jdbc连接postgresql数据库

概览:

java jdbc 连接 postgresql数据库

代码

Github传送门

代码节选

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
************ About ME ************
* @author : Mardan (ka1i)
* @date : Thu Jul 18 10:00:24 CST 2019
* @e-mail : GuzalInspire@hotmail.com
*
*********** About this ***********
* @project : hello
* @name : JdbcUtils.java
* @Last Modified by : Mardan
* @Last Modified time: Fri Jul 19 17:17:22 CST 2019
*/


package com.UESTC.Mardan.WebApp.utils;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;



public class JdbcUtils {

private static ComboPooledDataSource dataSource;

static {
dataSource = new ComboPooledDataSource();
dataSource.setUser("mardan"); //用户名
dataSource.setPassword("123456"); //数据库密码
dataSource.setJdbcUrl("jdbc:postgresql://192.168.160.132:5432/maven"); //数据库地址
try {
dataSource.setDriverClass("org.postgresql.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}

public static DataSource getDataSource() {
return dataSource;
}
}

运行

1
2
3
mvn clean
mvn verify
mvn clean package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Mardan@DESKTOP-TFALK9M MINGW64 /e/eclipse-workspace/WebApp (master)
$ mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.UESTC.Mardan:WebApp >-----------------------
[INFO] Building WebApp Maven Webapp 0.0.1
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ WebApp ---
[INFO] Deleting E:\eclipse-workspace\WebApp\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ WebApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\eclipse-workspace\WebApp\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ WebApp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to E:\eclipse-workspace\WebApp\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ WebApp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\eclipse-workspace\WebApp\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ WebApp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\eclipse-workspace\WebApp\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ WebApp ---
[INFO] Surefire report directory: E:\eclipse-workspace\WebApp\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.UESTC.Mardan.WebApp.operation.SQLOperationTest
7 19, 2019 5:10:57 下午 com.mchange.v2.log.MLog
信息: MLog clients using java 1.4+ standard logging.
7 19, 2019 5:10:57 下午 com.mchange.v2.c3p0.C3P0Registry
信息: Initializing c3p0-0.9.5.4 [built 23-March-2019 23:00:48 -0700; debug? true; trace: 10]
7 19, 2019 5:10:57 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, dataSourceName -> 1hge98ea42mvoc1qrofig|c46bcd4, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> org.postgresql.Driver, extensions -> {}, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, forceUseNamedDriverClass -> false, identityToken -> 1hge98ea42mvoc1qrofig|c46bcd4, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:postgresql://192.168.160.132:5432/maven, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, privilegeSpawnedThreads -> false, properties -> {password=******, user=******}, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, userOverrides -> {}, usesTraditionalReflectiveProxies -> false ]

FindALL
[Account [accountId=1101, email=email-1, password=88888888, nickName=ka1i], Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i], Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i], Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i], Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i], Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i], Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i], Account [accountId=505, email=mardan@huadu.com, password=12345678, nickName=mardan], Account [accountId=507, email=mardan@huadu-co.com, password=22345678, nickName=Mardan M]]
pprint:
Account [accountId=1101, email=email-1, password=88888888, nickName=ka1i]
Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i]
Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i]
Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i]
Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i]
Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i]
Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i]
Account [accountId=505, email=mardan@huadu.com, password=12345678, nickName=mardan]
Account [accountId=507, email=mardan@huadu-co.com, password=22345678, nickName=Mardan M]

find by id 1102
Account [accountId=1102, email=email-1, password=88888888, nickName=ka1i]
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.649 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ WebApp ---
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/Mardan/.m2/repository/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar) to field java.util.Properties.defaults
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[INFO] Packaging webapp
[INFO] Assembling webapp [WebApp] in [E:\eclipse-workspace\WebApp\target\WebApp]
[INFO] Processing war project
[INFO] Copying webapp resources [E:\eclipse-workspace\WebApp\src\main\webapp]
[INFO] Webapp assembled in [874 msecs]
[INFO] Building war: E:\eclipse-workspace\WebApp\target\WebApp.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO]
[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ WebApp ---
[INFO] Including junit:junit:jar:4.12 in the shaded jar.
[INFO] Including org.hamcrest:hamcrest-core:jar:1.3 in the shaded jar.
[INFO] Including org.postgresql:postgresql:jar:42.2.6 in the shaded jar.
[INFO] Including com.mchange:c3p0:jar:0.9.5.4 in the shaded jar.
[INFO] Including com.mchange:mchange-commons-java:jar:0.2.15 in the shaded jar.
[INFO] Including commons-dbutils:commons-dbutils:jar:1.7 in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing E:\eclipse-workspace\WebApp\target\WebApp-0.0.1.war with E:\eclipse-workspace\WebApp\target\WebApp-0.0.1-shaded.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.117 s
[INFO] Finished at: 2019-07-19T17:11:00+08:00
[INFO] ------------------------------------------------------------------------
------ 文章已经结束,谢谢你的阅读 ------
------ article has ended,thank you for reading ------
Thanks support Me

---------------- The End ----------------