Olá
Sou um pouco novo em programação e preciso da seguinte ajuda, se alguem puder me ajudar.
Quero criar um programa que cria bando de dados em mysql.
Aperto o Botão [Instalar] e ja cria o banco com as tabelas e tudo.
Obrigado
Rebelatto
Olá
Sou um pouco novo em programação e preciso da seguinte ajuda, se alguem puder me ajudar.
Quero criar um programa que cria bando de dados em mysql.
Aperto o Botão [Instalar] e ja cria o banco com as tabelas e tudo.
Obrigado
Rebelatto
Acredito que assim funcionaria, mas não.
public class frame1 extends javax.swing.JFrame {
private Connection conexão = null;
private ResultSet rs = null;
public frame1() throws SQLException {
initComponents();
String nomeDoDriver = "com.mysql.jdbc.Driver";
try {
Class.forName(nomeDoDriver).newInstance();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}
String localBancoDeDados = "jdbc:mysql://localhost:3306";
String usuario = "root";
String senha = "root";
try {
conexão = (Connection) DriverManager.getConnection(localBancoDeDados, usuario, senha);
} catch (SQLException ex) {
ex.printStackTrace();
}
Statement st = conexão.createStatement();
rs = st.executeQuery("show databases");
while (rs.next()) {
if (rs.getString(1).equals("nutri")) {
break;
}
if (rs.isLast()) {
String Command = "create database nutri";
st.executeQuery(Command);
Command = "use nutri";
st.executeQuery(Command);
System.exit(0);
Command = "create table escolas(cod int primary key, nome varchar(30) not null, rua varchar(20) not null," +
" telefone int not null)";
st.executeQuery(Command);
Command = "create table criancas(cod int primary key, codEscola int foreign key references escolas(cod), nome varchar(30)" +
" not null, dataNasc date not null, sexo char(9) not null)";
st.executeQuery(Command);
}
}
}
Acredito que assim funcionaria, mas não.public class frame1 extends javax.swing.JFrame {
private Connection conexão = null; private ResultSet rs = null; public frame1() throws SQLException { initComponents(); String nomeDoDriver = "com.mysql.jdbc.Driver"; try { Class.forName(nomeDoDriver).newInstance(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } String localBancoDeDados = "jdbc:mysql://localhost:3306"; String usuario = "root"; String senha = "root"; try { conexão = (Connection) DriverManager.getConnection(localBancoDeDados, usuario, senha); } catch (SQLException ex) { ex.printStackTrace(); } Statement st = conexão.createStatement(); rs = st.executeQuery("show databases"); while (rs.next()) { if (rs.getString(1).equals("nutri")) { break; } if (rs.isLast()) { String Command = "create database nutri"; st.executeQuery(Command); Command = "use nutri"; st.executeQuery(Command); System.exit(0); Command = "create table escolas(cod int primary key, nome varchar(30) not null, rua varchar(20) not null," + " telefone int not null)"; st.executeQuery(Command); Command = "create table criancas(cod int primary key, codEscola int foreign key references escolas(cod), nome varchar(30)" + " not null, dataNasc date not null, sexo char(9) not null)"; st.executeQuery(Command); } } }
Não esquece de sempre que postar um código coloca-lo entre tags, caso contrário, a legibilidade do mesmo torna-se complicada ou as vezes incompreensível. Em relação ao seu código, se você não possuir nenhuma tabela criada, não vai acessar o seu while. Nunca tentei criar uma database diretamente do JAVA. Tente simplesmente enviar os comandos SQL diretamente para o banco.
Abraços.