1 |
|
package ca.spaz.sql; |
2 |
|
|
3 |
|
import java.sql.*; |
4 |
|
|
5 |
|
import org.apache.log4j.Logger; |
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
public class SQLInsert extends SQLStatement implements Columns { |
13 |
|
|
14 |
|
|
15 |
|
|
16 |
0 |
private static final Logger logger = Logger.getLogger(SQLInsert.class); |
17 |
|
|
18 |
|
private SQLColumnSet cols; |
19 |
|
|
20 |
|
public SQLInsert(String tableName) { |
21 |
0 |
super(tableName, true, class="keyword">true); |
22 |
0 |
cols = new SQLColumnSet(); |
23 |
0 |
} |
24 |
|
|
25 |
|
protected void doExecute(Connection con) throws SQLException { |
26 |
0 |
Statement stmt = con.createStatement(); |
27 |
0 |
String query = this.getQueryString(); |
28 |
0 |
if (logger.isDebugEnabled()) { |
29 |
0 |
logger.debug("executeQuery() - Statement to be executed: " + query); |
30 |
|
} |
31 |
|
|
32 |
0 |
stmt.execute(query); |
33 |
0 |
} |
34 |
|
|
35 |
|
protected ResultSet doExecuteQuery(Connection con) throws SQLException { |
36 |
0 |
Statement stmt = con.createStatement(); |
37 |
0 |
String query = this.getQueryString(); |
38 |
0 |
if (logger.isDebugEnabled()) { |
39 |
0 |
logger.debug("executeQuery() - Statement to be executed: " + query); |
40 |
|
} |
41 |
|
|
42 |
0 |
stmt.executeUpdate(query); |
43 |
0 |
if (con.getMetaData().supportsGetGeneratedKeys()) { |
44 |
0 |
return stmt.getGeneratedKeys(); |
45 |
|
} else { |
46 |
0 |
return null; |
47 |
|
} |
48 |
|
} |
49 |
|
|
50 |
|
|
51 |
|
protected String getQueryString() { |
52 |
0 |
StringBuffer sb = new StringBuffer(); |
53 |
0 |
sb.append("INSERT INTO "); |
54 |
0 |
sb.append(this.table); |
55 |
|
|
56 |
0 |
sb.append(cols.getNameString()); |
57 |
|
|
58 |
0 |
sb.append("\n VALUES "); |
59 |
0 |
sb.append(cols.getValueString()); |
60 |
0 |
sb.append(";"); |
61 |
0 |
return sb.toString(); |
62 |
|
} |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
public SQLColumnSet getColumns() { |
69 |
0 |
return cols; |
70 |
|
} |
71 |
|
|
72 |
|
} |