1 package org.objectweb.modfact.jmi.ps; 2 3 import java.sql.*; 4 5 12 public class PersistantHelper extends PersistantService { 13 14 static Connection connection; 15 16 public PersistantHelper() throws ClassNotFoundException , SQLException { 17 Class.forName("com.mysql.jdbc.Driver"); 18 String url = "jdbc:mysql://localhost:3306/test"; 19 connection = DriverManager.getConnection(url); 20 21 } 22 23 24 25 30 public static boolean ifModelExist(String name) { 31 32 boolean result = false; 33 String 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 60 public static String getModelIdbyName(String name) { 61 62 String id = null; 63 String 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 88 public static void createMetamodel(String name) { 89 90 String 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 110 public static void deleteMetamodelContent(String name) { 111 112 String modelId = getModelIdbyName(name); 113 String 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 135 public static void printFreeMemory(String message) { 136 137 System.out.println( 138 "Free Memory " 139 + message 140 + ":->" 141 + Runtime.getRuntime().freeMemory()); 142 } 143 144 } 145 | Popular Tags |