ChestShop-3/com/Acrobot/Breeze/Database/Database.java
2012-11-02 20:48:14 +01:00

73 lines
1.8 KiB
Java

package com.Acrobot.Breeze.Database;
import javax.persistence.Entity;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Database class, which can be used to connect to JDBC
*
* @author Acrobot
*/
public class Database {
private String uri;
private String username;
private String password;
public Database(String uri, String username, String password) {
this.uri = uri;
this.username = username;
this.password = password;
}
/**
* Gets the table with given name, even if it doesn't exist in the database
*
* @param name Table's name
* @return Table
*/
public Table getTable(String name) {
return new Table(this, name);
}
/**
* @Deprecated - Not ready yet
*
* Creates a table from a given class
*
* @param clazz Class with fields
* @return If table was succesfully created
*/
public boolean createFromClass(Class clazz) {
if (!clazz.isAnnotationPresent(Entity.class) || !clazz.isAnnotationPresent(javax.persistence.Table.class)) {
return false;
}
String tableName = ((javax.persistence.Table) clazz.getAnnotation(javax.persistence.Table.class)).name();
Table table = getTable(tableName);
EntityParser parser = new EntityParser(clazz);
String fields = parser.parseToString();
try {
table.create(fields);
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* @return Connection to the database
* @throws SQLException exception
*/
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(uri, username, password);
}
}