1 25 package org.objectweb.easybeans.tests.common.db; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 33 import javax.naming.NamingException ; 34 import javax.sql.DataSource ; 35 36 import org.objectweb.easybeans.log.JLog; 37 import org.objectweb.easybeans.log.JLogFactory; 38 import org.objectweb.easybeans.server.Embedded; 39 import org.objectweb.easybeans.tests.common.helper.DBHelper; 40 41 46 public class TableManager { 47 48 51 protected static final int PRIMARY_KEY = 1; 52 53 56 private static JLog logger = JLogFactory.getLog(Embedded.class); 57 58 61 private DataSource ds = null; 62 63 68 public TableManager(final String dbName) throws NamingException { 69 this(DBHelper.getDataSource(dbName)); 70 } 71 72 76 public TableManager(final DataSource ds) { 77 this.ds = ds; 78 if (ds == null) { 79 throw new IllegalArgumentException ("DataSource is null"); 80 } 81 } 82 83 89 public void insertTable(final String tableName) throws SQLException { 90 Connection connection = null; 91 try { 92 logger.debug("Before insert table."); 93 connection = ds.getConnection(); 94 logger.debug("Connection opened."); 95 96 PreparedStatement stmUpdate = null; 97 try { 98 stmUpdate = connection.prepareStatement("CREATE TABLE " + tableName + " (CodeTest integer, NameTest varchar(30), " 99 + "PRIMARY KEY (CodeTest))"); 100 stmUpdate.executeUpdate(); 101 } finally { 102 if (stmUpdate != null) { 103 stmUpdate.close(); 104 } 105 } 106 logger.debug("Table created."); 107 108 PreparedStatement stmUpdateField = null; 110 try { 111 stmUpdateField = connection.prepareStatement("INSERT INTO " + tableName + " (CodeTest) VALUES (" 112 + PRIMARY_KEY + ")"); 113 stmUpdateField.executeUpdate(); 114 } finally { 115 if (stmUpdateField != null) { 116 stmUpdateField .close(); 117 } 118 } 119 } finally { 120 if (connection != null) { 121 connection.close(); 122 logger.debug("Connection closed."); 123 } 124 } 125 } 126 127 133 public void deleteTable(final String tableName) throws SQLException { 134 Connection connection = null; 135 try { 136 connection = ds.getConnection(); 137 PreparedStatement stmUpdate = null; 139 try { 140 stmUpdate = connection.prepareStatement("DROP TABLE " + tableName + " CASCADE"); 141 stmUpdate.executeUpdate(); 142 } finally { 143 if (stmUpdate != null) { 144 stmUpdate.close(); 145 } 146 } 147 } finally { 148 if (connection != null) { 149 connection.close(); 150 } 151 } 152 } 153 154 159 public void verifyTable(final String tableName) throws SQLException { 160 Connection connection = null; 161 try { 162 connection = ds.getConnection(); 163 Statement stmt = null; 164 try { 165 stmt = connection.createStatement(); 166 ResultSet rs = stmt.executeQuery("SELECT * FROM " + tableName + " WHERE CodeTest = " + PRIMARY_KEY); 167 if(!rs.next()){ 168 throw new SQLException ("There are not values in the table"); 169 } 170 } finally { 171 if(stmt != null){ 172 stmt.close(); 173 } 174 } 175 } finally { 176 if (connection != null) { 177 connection.close(); 178 } 179 } 180 } 181 182 187 public void test(final String tableName) throws SQLException { 188 insertTable(tableName); 189 deleteTable(tableName); 190 } 191 } 192 | Popular Tags |