I'm developing a Java application and I need this application to run check that the database exists, if it does not exist, it creates the base and its tables so that the application can enter the data.
Can anyone help me?
I'm developing a Java application and I need this application to run check that the database exists, if it does not exist, it creates the base and its tables so that the application can enter the data.
Can anyone help me?
Based on responses Checking if a table exists in MySQL and another on SOen .
// abre a conexão com o banco de dados
Connection conexao = DriverManager.getConnection(URL, USUÁRIO, SENHA);
// acessa os metadados do banco de dados
DatabaseMetaData metadados = conexao.getMetaData();
// erificar se a tabela existe
ResultSet tabela = metadados.getTables(null, null, "MinhaTabela", null);
// condição, caso a tabela exista
if (tabela.next()) {
// faça algo se a tabela existir
} else {
// faça algo se a não tabela existir
criarTabela();
}
Function to create the table
private void criarTabela() throws SQLException {
String sqlCreate = "CREATE TABLE IF NOT EXISTS MinhaTabela"
+ " (id INTEGER(10),"
+ " nome VARCHAR(80),"
+ " email VARCHAR(80))";
Statement stmt = conexao.createStatement();
stmt.execute(sqlCreate);
}
Obs because I'm on a totally unprepared computer, testing can not be performed.