DDL.java
1.04 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
package transaction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import javax.inject.Inject;
import br.gov.frameworkdemoiselle.annotation.Name;
import br.gov.frameworkdemoiselle.transaction.Transactional;
public class DDL {
@Name("conn1")
@Inject
private Connection connection;
@Transactional
public void dropAndCreate() throws Exception {
dropTable();
createTable();
}
private void dropTable() throws Exception {
Statement st = connection.createStatement();
try {
String sql = "DROP TABLE myentity";
st.executeUpdate(sql);
st.close();
} catch (Exception e) {
}
}
private void createTable() throws Exception {
StringBuffer sql = new StringBuffer();
sql.append("CREATE TABLE myentity ( ");
sql.append(" id int NOT NULL, ");
sql.append(" description varchar(10) NOT NULL, ");
sql.append("CONSTRAINT myentity_pk PRIMARY KEY (id) ");
sql.append("); ");
PreparedStatement pstmt = connection.prepareStatement(sql.toString());
pstmt.execute();
pstmt.close();
}
}