Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /*
  2. * DbaseClean
  3. * Drop a database, if exists, and create a new empty one.
  4. * Collect database info from a resource file.
  5. *
  6. * When using embeded in an application must be called outside a transaction
  7. * (@Atomic) or connection to the same database server!
  8. *
  9. * javac DbaseClean.java
  10. * java -cp mysql-connector-java-5.1.38.jar:. DbaseClean
  11. *
  12. * Maven (do not use in the fenix-framework):
  13. * <dependency>
  14. * <groupId>mysql</groupId>
  15. * <artifactId>mysql-connector-java</artifactId>
  16. * <version>5.1.38</version>
  17. * </dependency>
  18. */
  19. import java.io.*;
  20. import java.sql.*;
  21. import java.util.Scanner;
  22.  
  23. public class DbaseClean {
  24. static final String driver = "com.mysql.jdbc.Driver";
  25. static final String url = "jdbc:mysql://localhost/";
  26.  
  27. private String dbase, user, passwd; // dbase params from resource file
  28. // resource file in current-dir ou src/main/resources maven-dir
  29. private static String res = "fenix-framework-jvstm-ojb.properties";
  30. private Connection conn = null;
  31.  
  32. public DbaseClean() { getDbase(res); }
  33. public DbaseClean(String resource) { getDbase(res = resource); }
  34. public DbaseClean(String db, String user, String pw) {
  35. dbase = db;
  36. this.user = user;
  37. passwd = pw;
  38. }
  39.  
  40. public void connect() throws SQLException {
  41. conn = DriverManager.getConnection(url, user, passwd);
  42. }
  43. public void disconnect() throws SQLException {
  44. if (conn != null) conn.close();
  45. conn = null;
  46. }
  47.  
  48. public String name() { return dbase; }
  49. public String user() { return user; }
  50. protected String passwd() { return passwd; }
  51. public void name(String s) { dbase = s; }
  52. public void user(String s) { user = s; }
  53. public void passwd(String s) { passwd = s; }
  54.  
  55. public static void main(String[] args) {
  56. try {
  57. DbaseClean db;
  58. if (args.length == 3) db = new DbaseClean(args[0], args[1], args[2]);
  59. else {
  60. if (args.length > 3) {
  61. System.err.println("DbaseClean [dbase [user [passwd]]]");
  62. return;
  63. }
  64. db = new DbaseClean();
  65. if (args.length > 0) db.name(args[0]);
  66. if (args.length > 1) db.user(args[1]);
  67. if (args.length > 2) db.passwd(args[2]);
  68. }
  69. db.cleanup();
  70. System.out.println("Database created successfully...");
  71. } catch(Exception e) { e.printStackTrace(); }
  72. }
  73.  
  74. public void cleanup() throws Exception {
  75. Class.forName(driver);
  76. System.err.println(user()+"@"+url+user()); // debug
  77. connect();
  78. System.err.println("Connected..."); // debug
  79. try { sql("DROP DATABASE " + name());
  80. } catch (SQLException e) { System.err.println(e); }
  81. sql("CREATE DATABASE " + name());
  82. disconnect();
  83. }
  84.  
  85. public void sql(String com) throws SQLException {
  86. Statement stmt = conn.createStatement();
  87. stmt.executeUpdate(com);
  88. stmt.close();
  89. }
  90.  
  91. /* read database information from resource file */
  92. private void getDbase(String fileName) {
  93. ClassLoader classLoader = getClass().getClassLoader();
  94. File file = new File(classLoader.getResource(fileName).getFile());
  95.  
  96. try (Scanner scanner = new Scanner(file)) {
  97. while (scanner.hasNextLine()) {
  98. String line = scanner.nextLine().trim();
  99. if (line.length() == 0) continue;
  100. if (line.charAt(0) == '#') continue;
  101. String[] attr = line.split("=");
  102. if (attr[0].equals("dbUsername")) user = attr[1].trim();
  103. if (attr[0].equals("dbPassword")) passwd = attr[1].trim();
  104. if (attr[0].equals("dbAlias")) {
  105. int i = attr[1].trim().lastIndexOf('/');
  106. int j = attr[1].trim().lastIndexOf('?');
  107. dbase = attr[1].trim().substring(i+1,j);
  108. }
  109. }
  110. scanner.close();
  111. } catch (IOException e) { e.printStackTrace(); }
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement