-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
70 lines (49 loc) · 1.5 KB
/
Copy pathMain.java
File metadata and controls
70 lines (49 loc) · 1.5 KB
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
package pool_jdbc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
@Repository
public class Main {
private static final String SQL_INSERT = "INSERT INTO test (id, name) VALUES (?, ?)";
private DataSource dataSource;
@Autowired
public Main(DataSource ds) {
this.dataSource = ds; this.addPerson();
}
public void addPerson(){
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = dataSource.getConnection();
stmt = conn.prepareStatement(SQL_INSERT);
stmt.setInt(1, 10);
stmt.setString(2, "Bernd");
stmt.execute();
} catch (SQLException e){
e.printStackTrace();
} finally {
try {
if(stmt != null) stmt.close();
if (conn != null) {
conn.close();
} else {
System.out.println("Connection ist null :/");
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ConfigurableApplicationContext ac = new AnnotationConfigApplicationContext(DataSourceConfiguration.class);
Arrays.stream(ac.getBeanDefinitionNames()).forEach(System.out::println);
DataSource dsc = (DataSource) ac.getBean("dataSourceJDBC");
new Main(dsc);
}
}