View Javadoc

1   /*
2    * 
3    * 
4    */
5   package example.filestorage;
6   
7   import java.sql.*;
8   
9   /***
10   * 
11   * @author Sean C. Sullivan
12   *
13   */
14  public class DerbyDBSetup
15  {
16  
17  	private DerbyDBSetup()
18  	{
19  		// empty
20  	}
21  	
22  	public static void dropAll()
23  	{
24  		String dropTable = "DROP TABLE " + Constants.TABLE_NAME;
25  		
26  		try
27  		{
28  			executeSQL(dropTable);
29  		}
30  		catch (Exception ignored)
31  		{
32  			// ignore
33  		}
34  
35  	}
36  	
37  	private static void executeSQL(String sql)
38  	{
39  		Connection conn = null;
40  		Statement st = null;
41  		
42  		try
43  		{
44  			conn = DBUtil.getConnection();
45  			st = conn.createStatement();
46  			st.executeUpdate(sql);
47  		}
48  		catch (SQLException ex)
49  		{
50  			throw new DataAccessException(sql, ex);
51  		}
52  		finally 
53  		{
54  			DBUtil.close(conn, st, null);
55  		}
56  	}
57  	
58  	public static void create()
59  	{
60  		String createTable = "CREATE TABLE "
61  						+ Constants.TABLE_NAME
62  						+ " ( "
63  						+ Constants.COLUMN_FILE_ID
64  						+ " INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "
65  						+ Constants.COLUMN_FILENAME
66  						+ " VARCHAR( "
67  						+ Constants.MAX_LENGTH_FILENAME
68  						+ " ) NOT NULL, "
69  						+ Constants.COLUMN_FILESIZE
70  						+ " INTEGER NOT NULL, "
71  						+ Constants.COLUMN_FILEDATA 
72  						+ " BLOB (250M)"
73  						+ " ) ";
74  
75  		executeSQL(createTable);
76  		System.out.println("Created table: " + Constants.TABLE_NAME);
77  		
78  	}
79  	
80  	public static void main(String[] args)
81  	{
82  		dropAll();
83  		create();
84  		System.out.println("Done");
85  	}
86  }