Skip to content

Commit 86bbc4d

Browse files
committed
Added jdbc batch example
1 parent ffd82bd commit 86bbc4d

File tree

12 files changed

+512
-459
lines changed

12 files changed

+512
-459
lines changed

jdbc/pom.xml

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,70 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
6-
<groupId>com.lwl</groupId>
7-
<artifactId>jdbc</artifactId>
8-
<version>0.0.1-SNAPSHOT</version>
9-
<name>JDBC </name>
10-
<description>JDBC Examples</description>
11-
12-
13-
<properties>
14-
<maven.compiler.source>1.8</maven.compiler.source>
15-
<maven.compiler.target>1.8</maven.compiler.target>
16-
</properties>
17-
18-
19-
<dependencies>
20-
21-
<dependency>
22-
<groupId>mysql</groupId>
23-
<artifactId>mysql-connector-java</artifactId>
24-
<version>8.0.14</version>
25-
</dependency>
26-
27-
<dependency>
28-
<groupId>com.fasterxml.jackson.core</groupId>
29-
<artifactId>jackson-databind</artifactId>
30-
<version>2.11.0</version>
31-
</dependency>
32-
33-
<dependency>
34-
<groupId>org.junit.jupiter</groupId>
35-
<artifactId>junit-jupiter-api</artifactId>
36-
<version>5.6.0</version>
37-
<scope>test</scope>
38-
</dependency>
39-
40-
41-
<dependency>
42-
<groupId>org.projectlombok</groupId>
43-
<artifactId>lombok</artifactId>
44-
<version>1.18.6</version>
45-
<scope>provided</scope>
46-
</dependency>
47-
48-
49-
</dependencies>
50-
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.lwl</groupId>
7+
<artifactId>jdbc</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>JDBC </name>
10+
<description>JDBC Examples</description>
11+
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>mysql</groupId>
16+
<artifactId>mysql-connector-java</artifactId>
17+
<version>8.0.14</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>com.fasterxml.jackson.core</groupId>
21+
<artifactId>jackson-databind</artifactId>
22+
<version>2.11.0</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-api</artifactId>
27+
<version>5.6.0</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter-engine</artifactId>
33+
<version>5.6.0</version>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.zaxxer</groupId>
39+
<artifactId>HikariCP</artifactId>
40+
<version>3.4.5</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<version>1.18.6</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
50+
</dependencies>
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-surefire-plugin</artifactId>
56+
<version>2.22.2</version>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>3.8.1</version>
62+
<configuration>
63+
<source>1.8</source>
64+
<target>1.8</target>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
5170
</project>
Lines changed: 109 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,109 @@
1-
package com.lwl.ems.util;
2-
3-
import java.io.FileInputStream;
4-
import java.io.IOException;
5-
import java.sql.CallableStatement;
6-
import java.sql.Connection;
7-
import java.sql.DriverManager;
8-
import java.sql.PreparedStatement;
9-
import java.sql.ResultSet;
10-
import java.sql.SQLException;
11-
import java.sql.Statement;
12-
import java.util.Properties;
13-
14-
public enum ConnectionUtil {
15-
util;
16-
17-
private static Properties properties;
18-
static {
19-
properties = new Properties();
20-
try {
21-
//properties.load(Thread.currentThread().getClass().getResourceAsStream("/db.properties"));
22-
properties.load(ConnectionUtil.class.getResourceAsStream("/db.properties"));
23-
24-
} catch (IOException e) {
25-
e.printStackTrace();
26-
}
27-
}
28-
29-
public Connection getConnection() {
30-
Connection con = null;
31-
try {
32-
33-
con = DriverManager.getConnection(properties.getProperty("url"),properties);
34-
}catch (Exception e) {
35-
System.out.println("While connecting with db:"+e);
36-
}
37-
return con;
38-
39-
}
40-
public void close(ResultSet rs, Statement st, Connection con) {
41-
try {
42-
if(rs != null)
43-
rs.close();
44-
if(st!=null)
45-
st.close();
46-
if(con != null)
47-
con.close();
48-
}catch (SQLException e) {
49-
System.out.println("While closing resources :"+e);
50-
}
51-
}
52-
public void close(Statement st, Connection con) {
53-
try {
54-
55-
if(st!=null)
56-
st.close();
57-
if(con != null)
58-
con.close();
59-
}catch (SQLException e) {
60-
System.out.println("While closing resources :"+e);
61-
}
62-
}
63-
public void close(ResultSet rs, PreparedStatement pst, Connection con) {
64-
try {
65-
if(rs != null)
66-
rs.close();
67-
if(pst!=null)
68-
pst.close();
69-
if(con != null)
70-
con.close();
71-
}catch (SQLException e) {
72-
System.out.println("While closing resources :"+e);
73-
}
74-
}
75-
76-
public void close(PreparedStatement pst, Connection con) {
77-
try {
78-
79-
if(pst!=null)
80-
pst.close();
81-
if(con != null)
82-
con.close();
83-
}catch (SQLException e) {
84-
System.out.println("While closing resources :"+e);
85-
}
86-
}
87-
public void close(ResultSet rs, CallableStatement cst, Connection con) {
88-
try {
89-
if(rs != null)
90-
rs.close();
91-
if(cst!=null)
92-
cst.close();
93-
if(con != null)
94-
con.close();
95-
}catch (SQLException e) {
96-
System.out.println("While closing resources :"+e);
97-
}
98-
}
99-
public void close(CallableStatement cst, Connection con) {
100-
try {
101-
102-
if(cst!=null)
103-
cst.close();
104-
if(con != null)
105-
con.close();
106-
}catch (SQLException e) {
107-
System.out.println("While closing resources :"+e);
108-
}
109-
}
110-
}
1+
package com.lwl.ems.util;
2+
3+
import java.sql.CallableStatement;
4+
import java.sql.Connection;
5+
import java.sql.PreparedStatement;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
import java.sql.Statement;
9+
10+
import com.zaxxer.hikari.HikariConfig;
11+
import com.zaxxer.hikari.HikariDataSource;
12+
13+
public enum ConnectionUtil {
14+
util;
15+
16+
HikariDataSource ds;
17+
18+
{
19+
HikariConfig config = new HikariConfig(ConnectionUtil.class.getResource("/db.properties").getPath());
20+
ds = new HikariDataSource(config);
21+
22+
}
23+
24+
public Connection getConnection() {
25+
Connection con = null;
26+
try {
27+
return ds.getConnection();
28+
} catch (Exception e) {
29+
System.out.println("While connecting with db:" + e);
30+
}
31+
return con;
32+
33+
}
34+
35+
public void close(ResultSet rs, Statement st, Connection con) {
36+
try {
37+
if (rs != null)
38+
rs.close();
39+
if (st != null)
40+
st.close();
41+
if (con != null)
42+
con.close();
43+
} catch (SQLException e) {
44+
System.out.println("While closing resources :" + e);
45+
}
46+
}
47+
48+
public void close(Statement st, Connection con) {
49+
try {
50+
51+
if (st != null)
52+
st.close();
53+
if (con != null)
54+
con.close();
55+
} catch (SQLException e) {
56+
System.out.println("While closing resources :" + e);
57+
}
58+
}
59+
60+
public void close(ResultSet rs, PreparedStatement pst, Connection con) {
61+
try {
62+
if (rs != null)
63+
rs.close();
64+
if (pst != null)
65+
pst.close();
66+
if (con != null)
67+
con.close();
68+
} catch (SQLException e) {
69+
System.out.println("While closing resources :" + e);
70+
}
71+
}
72+
73+
public void close(PreparedStatement pst, Connection con) {
74+
try {
75+
76+
if (pst != null)
77+
pst.close();
78+
if (con != null)
79+
con.close();
80+
} catch (SQLException e) {
81+
System.out.println("While closing resources :" + e);
82+
}
83+
}
84+
85+
public void close(ResultSet rs, CallableStatement cst, Connection con) {
86+
try {
87+
if (rs != null)
88+
rs.close();
89+
if (cst != null)
90+
cst.close();
91+
if (con != null)
92+
con.close();
93+
} catch (SQLException e) {
94+
System.out.println("While closing resources :" + e);
95+
}
96+
}
97+
98+
public void close(CallableStatement cst, Connection con) {
99+
try {
100+
101+
if (cst != null)
102+
cst.close();
103+
if (con != null)
104+
con.close();
105+
} catch (SQLException e) {
106+
System.out.println("While closing resources :" + e);
107+
}
108+
}
109+
}
Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
user=lakshman
2-
password=lakshman
3-
url=jdbc:mysql://localhost:3306/batch_4
1+
jdbcUrl=jdbc:mysql://localhost:3306/batch_4
2+
username=lakshman
3+
password=lakshman
4+
dataSource.cachePrepStmts=true
5+
dataSource.prepStmtCacheSize=250
6+
dataSource.prepStmtCacheSqlLimit=2048
7+
dataSource.useServerPrepStmts=true
8+
dataSource.useLocalSessionState=true
9+
dataSource.rewriteBatchedStatements=true
10+
dataSource.cacheResultSetMetadata=true
11+
dataSource.cacheServerConfiguration=true
12+
dataSource.elideSetAutoCommits=true
13+
dataSource.maintainTimeStats=false

0 commit comments

Comments
 (0)