KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > ps > PersistantHelper


1 package org.objectweb.modfact.jmi.ps;
2
3 import java.sql.*;
4
5 /**
6  * @author gregory
7  *
8  * This class provide utilities for the Service layer of our package
9  * (Persistant Service)
10  *
11  */

12 public class PersistantHelper extends PersistantService {
13
14     static Connection connection;
15
16     public PersistantHelper() throws ClassNotFoundException JavaDoc, SQLException {
17         Class.forName("com.mysql.jdbc.Driver");
18         String JavaDoc url = "jdbc:mysql://localhost:3306/test";
19         connection = DriverManager.getConnection(url);
20
21     }
22
23     
24     
25     /**
26      * Method ifModelExist.
27      * @param name
28      * @return boolean
29      */

30     public static boolean ifModelExist(String JavaDoc name) {
31
32         boolean result = false;
33         String JavaDoc query =
34             "SELECT * FROM modelcontainer where ModelName='" + name + "';";
35
36         try {
37             Statement statement = connection.createStatement();
38             ResultSet rs = statement.executeQuery(query);
39
40             result = rs.first();
41             rs.close();
42             statement.close();
43
44         } catch (SQLException e) {
45             System.out.println(e.toString());
46             e.printStackTrace();
47         }
48
49         return result;
50
51     }
52
53     
54     
55     /**
56      * Method getModelIdbyName.
57      * @param name
58      * @return String
59      */

60     public static String JavaDoc getModelIdbyName(String JavaDoc name) {
61
62         String JavaDoc id = null;
63         String JavaDoc query =
64             "SELECT ModelContainerID FROM modelcontainer where ModelName='"
65                 + name
66                 + "';";
67
68         try {
69             Statement statement = connection.createStatement();
70             ResultSet rs = statement.executeQuery(query);
71             rs.next();
72             id = rs.getString("ModelContainerId");
73             rs.close();
74             statement.close();
75
76         } catch (SQLException e) {
77             System.out.println(e.toString());
78             e.printStackTrace();
79         }
80
81         return id;
82     }
83
84     /**
85      * Method createMetamodel.
86      * @param name
87      */

88     public static void createMetamodel(String JavaDoc name) {
89
90         String JavaDoc query =
91             "INSERT INTO ModelContainer (ModelName) VALUES ('" + name + "');";
92         try {
93             Statement statement = connection.createStatement();
94             statement.execute(query);
95
96             statement.close();
97
98         } catch (SQLException e) {
99             System.out.println(e.toString());
100             e.printStackTrace();
101         }
102     }
103
104     /**
105      * Method deleteMetamodelContent.
106      * @param name
107      * delete only the content of a metamodel and not the name entry in the
108      * database
109      */

110     public static void deleteMetamodelContent(String JavaDoc name) {
111
112         String JavaDoc modelId = getModelIdbyName(name);
113         String JavaDoc query =
114             "DELETE FROM RefPackage WHERE ModelContainerId = '"
115                 + modelId
116                 + "';";
117         try {
118             Statement statement = connection.createStatement();
119             statement.execute(query);
120             statement.close();
121
122         } catch (SQLException e) {
123             System.out.println(e.toString());
124             e.printStackTrace();
125         }
126     }
127
128     /**
129      * Method printFreeMemory.
130      * @param message
131      * usefull for testing memory load : to be improved; i.e
132      * be more accurate and use a logger instead of System.out as
133      * outputstreamn
134      */

135     public static void printFreeMemory(String JavaDoc message) {
136
137         System.out.println(
138             "Free Memory "
139                 + message
140                 + ":->"
141                 + Runtime.getRuntime().freeMemory());
142     }
143
144 }
145
Popular Tags