Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class JDBC {
- //defibição de variaveis
- static final String JDBC_DRIVER = "com.mysql.jdbc.driver";
- static final String DB_URL = "jdbc:mysql://localhost";
- static final String USER = "root";
- static final String PWD = "root";
- public static void main(String[] args) {
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- //Registro do drive JDBC para Mysql
- Class.forName(JDBC_DRIVER);
- }catch(ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- try {
- //criando a minha conexao
- con = DriverManager.getConnection(DB_URL, USER, PWD);
- System.out.println("Conectando no banco...");
- stmt = con.createStatement();
- System.out.println("Excluindo o banco se existir...");
- String SQLDeExclusao = "drop database if exists pessoas";
- stmt.execute(SQLDeExclusao);
- System.out.println("Criando o banco pessoas...");
- String SQLDeCriacao = "create database pessoas";
- stmt.execute(SQLDeCriacao);
- String SQLUso = "use pessoas";
- stmt.execute(SQLUso);
- System.out.println("Usando o banco...");
- System.out.println("Criando a tabela pessoa...");
- String SQLCriacaoTabela = "CREATE TABLE PESSOA " +
- "(id INTEGER not NULL, " +
- " nome VARCHAR(50), " +
- " cpf CHAR(11), " +
- " rg CHAR (7), " +
- " PRIMARY KEY ( id ))";
- stmt.executeUpdate(SQLCriacaoTabela);
- System.out.println("Tabela pessoa criada!");
- System.out.println("Criando a tabela telefone...");
- String SQLCriacaoTabelaTel = "CREATE TABLE TELEFONE " +
- "(id INTEGER not NULL, " +
- " pessoa_id INTEGER not NULL, " +
- " ddd CHAR(3), " +
- " nr CHAR (9), " +
- " PRIMARY KEY ( id )," +
- " FOREIGN KEY(pessoa_id) REFERENCES PESSOA(id)" +
- ")";
- stmt.executeUpdate(SQLCriacaoTabelaTel);
- System.out.println("Tabela telefone criada!");
- System.out.println("Inserir dados na tabela pessoa...");
- String SQLInsercaoPessoa = "insert into pessoa values (1, 'joao', '12345678910', '1234567')";
- stmt.executeUpdate(SQLInsercaoPessoa);
- String SQLInsercaoPessoa2 = "insert into pessoa values (2, 'Maria', '98765432110', '7654321')";
- stmt.executeUpdate(SQLInsercaoPessoa2);
- String SQLInsercaoPessoa3 = "insert into pessoa values (3, 'Jose', '11122233344', '1236547')";
- stmt.executeUpdate(SQLInsercaoPessoa3);
- System.out.println("Dados inseridos em pessoa!");
- System.out.println("Inserir dados na tabela Telefone...");
- String SQLInsercaotel = "insert into telefone values (1, 1, '083', '998762345')";
- stmt.executeUpdate(SQLInsercaotel);
- String SQLInsercaotel2 = "insert into telefone values (2, 3, '083', '987651234')";
- stmt.executeUpdate(SQLInsercaotel2);
- String SQLInsercaotel3 = "insert into telefone values (3, 2, '083', '999887766')";
- stmt.executeUpdate(SQLInsercaotel3);
- String SQLInsercaotel4 = "insert into telefone values (4, 2, '083', '33334567')";
- stmt.executeUpdate(SQLInsercaotel4);
- System.out.println("Dados inseridos em teelfone!");
- // Selecionando dados
- System.out.println("Selecionando dados...");
- String SQLSelect = "select p.id, p.nome, t.ddd, t.nr from pessoa p, telefone t where p.id=t.pessoa_id";
- rs = stmt.executeQuery(SQLSelect);
- // Varrendo o resultSet
- while (rs.next()) {
- // Retrieve by column name
- int id = rs.getInt("id");
- String nome = rs.getString("nome");
- String ddd = rs.getString("ddd");
- String nr = rs.getString("nr");
- // Display values
- System.out.print("ID: " + id +
- ", Nome: " + nome +
- ", ddd: " + ddd +
- ", nr: " + nr + "\n");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- rs.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.println("RS nao fechou!");
- }
- try {
- stmt.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.println("Stmt nao fechou!");
- }
- try {
- con.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- System.out.println("Con nao fechou!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement