1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.anttasks;
18
19
20 import java.sql.Connection;
21 import java.sql.SQLException;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.taskdefs.SQLExec;
24 import java.util.Properties;
25 import org.apache.tools.ant.Project;;
26
27 public class ExecuteSQL extends SQLExec
28 {
29
30
31 public void execute() throws BuildException
32 {
33
34
35 try
36 {
37 System.out.println("Executing SQL statement");
38 super.execute();
39 }
40 catch (Exception e)
41 {
42 throw new BuildException(e, getLocation());
43 }
44 finally
45 {
46 if (StartDerby.getConnection(getProject()) != null)
47 {
48 try
49 {
50 if (!(StartDerby.getConnection(getProject()).isClosed()))
51 StartDerby.getConnection(getProject()).close();
52 }
53 catch (Exception e1)
54 {
55
56 }
57 StartDerby.setConnection(getProject(),null);
58 }
59 }
60 }
61
62
63 /***
64 * Creates a new Connection as using the driver, url, userid and password
65 * specified.
66 *
67 * The calling method is responsible for closing the connection.
68 *
69 * @return Connection the newly created connection.
70 * @throws BuildException if the UserId/Password/Url is not set or there
71 * is no suitable driver or the driver fails to load.
72 */
73 protected Connection getConnection() throws BuildException
74 {
75 if (StartDerby.getDriver(getProject()) == null)
76 throw new BuildException("Derby driver not established!", getLocation());
77
78 if (StartDerby.getConnection(getProject())!= null)
79 {
80 System.out.println("Connection already established");
81 return StartDerby.getConnection(getProject());
82 }
83
84
85 if (getUrl() == null) {
86 throw new BuildException("Url attribute must be set!", getLocation());
87 }
88 try {
89
90 log("connecting to " + getUrl(), Project.MSG_VERBOSE);
91 Properties info = new Properties();
92 if (getUserId() != null)
93 info.put("user", getUserId());
94 if (getPassword() != null)
95 info.put("password", getPassword());
96
97 Connection conn = StartDerby.getDriver(getProject()).connect(getUrl(), info);
98 if (conn == null) {
99
100 throw new SQLException("No suitable Driver for " + getUrl());
101 }
102
103 conn.setAutoCommit(isAutocommit());
104 System.out.println("Connection to " + getUrl() + " established");
105 StartDerby.setConnection(getProject(),conn);
106 return conn;
107 } catch (SQLException e) {
108 throw new BuildException(e, getLocation());
109 }
110
111 }
112
113
114 }