Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * DbaseClean
- * Drop a database, if exists, and create a new empty one.
- * Collect database info from a resource file.
- *
- * When using embeded in an application must be called outside a transaction
- * (@Atomic) or connection to the same database server!
- *
- * javac DbaseClean.java
- * java -cp mysql-connector-java-5.1.38.jar:. DbaseClean
- *
- * Maven (do not use in the fenix-framework):
- * <dependency>
- * <groupId>mysql</groupId>
- * <artifactId>mysql-connector-java</artifactId>
- * <version>5.1.38</version>
- * </dependency>
- */
- import java.io.*;
- import java.sql.*;
- import java.util.Scanner;
- public class DbaseClean {
- static final String driver = "com.mysql.jdbc.Driver";
- static final String url = "jdbc:mysql://localhost/";
- private String dbase, user, passwd; // dbase params from resource file
- // resource file in current-dir ou src/main/resources maven-dir
- private static String res = "fenix-framework-jvstm-ojb.properties";
- private Connection conn = null;
- public DbaseClean() { getDbase(res); }
- public DbaseClean(String resource) { getDbase(res = resource); }
- public DbaseClean(String db, String user, String pw) {
- dbase = db;
- this.user = user;
- passwd = pw;
- }
- public void connect() throws SQLException {
- conn = DriverManager.getConnection(url, user, passwd);
- }
- public void disconnect() throws SQLException {
- if (conn != null) conn.close();
- conn = null;
- }
- public String name() { return dbase; }
- public String user() { return user; }
- protected String passwd() { return passwd; }
- public void name(String s) { dbase = s; }
- public void user(String s) { user = s; }
- public void passwd(String s) { passwd = s; }
- public static void main(String[] args) {
- try {
- DbaseClean db;
- if (args.length == 3) db = new DbaseClean(args[0], args[1], args[2]);
- else {
- if (args.length > 3) {
- System.err.println("DbaseClean [dbase [user [passwd]]]");
- return;
- }
- db = new DbaseClean();
- if (args.length > 0) db.name(args[0]);
- if (args.length > 1) db.user(args[1]);
- if (args.length > 2) db.passwd(args[2]);
- }
- db.cleanup();
- System.out.println("Database created successfully...");
- } catch(Exception e) { e.printStackTrace(); }
- }
- public void cleanup() throws Exception {
- Class.forName(driver);
- System.err.println(user()+"@"+url+user()); // debug
- connect();
- System.err.println("Connected..."); // debug
- try { sql("DROP DATABASE " + name());
- } catch (SQLException e) { System.err.println(e); }
- sql("CREATE DATABASE " + name());
- disconnect();
- }
- public void sql(String com) throws SQLException {
- Statement stmt = conn.createStatement();
- stmt.executeUpdate(com);
- stmt.close();
- }
- /* read database information from resource file */
- private void getDbase(String fileName) {
- ClassLoader classLoader = getClass().getClassLoader();
- File file = new File(classLoader.getResource(fileName).getFile());
- try (Scanner scanner = new Scanner(file)) {
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine().trim();
- if (line.length() == 0) continue;
- if (line.charAt(0) == '#') continue;
- String[] attr = line.split("=");
- if (attr[0].equals("dbUsername")) user = attr[1].trim();
- if (attr[0].equals("dbPassword")) passwd = attr[1].trim();
- if (attr[0].equals("dbAlias")) {
- int i = attr[1].trim().lastIndexOf('/');
- int j = attr[1].trim().lastIndexOf('?');
- dbase = attr[1].trim().substring(i+1,j);
- }
- }
- scanner.close();
- } catch (IOException e) { e.printStackTrace(); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement