Advertisement
Guest User

Untitled

a guest
May 9th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. public class JDBC {
  8.    
  9.     //defibição de variaveis
  10.     static final  String JDBC_DRIVER = "com.mysql.jdbc.driver";
  11.     static final  String DB_URL = "jdbc:mysql://localhost";
  12.     static final  String USER = "root";
  13.     static final  String PWD = "root";
  14.    
  15.     public static void main(String[] args) {
  16.         Connection con = null;
  17.         Statement stmt = null;
  18.         ResultSet rs = null;
  19.        
  20.         try {
  21.             //Registro do drive JDBC para Mysql
  22.             Class.forName(JDBC_DRIVER);
  23.         }catch(ClassNotFoundException ex) {
  24.             ex.printStackTrace();
  25.         }
  26.        
  27.        
  28.         try {
  29.             //criando a minha conexao
  30.             con = DriverManager.getConnection(DB_URL, USER, PWD);
  31.             System.out.println("Conectando no banco...");
  32.            
  33.             stmt = con.createStatement();
  34.            
  35.             System.out.println("Excluindo o banco se existir...");
  36.             String SQLDeExclusao = "drop database if exists pessoas";
  37.             stmt.execute(SQLDeExclusao);
  38.            
  39.             System.out.println("Criando o banco pessoas...");
  40.             String SQLDeCriacao = "create database pessoas";
  41.             stmt.execute(SQLDeCriacao);
  42.            
  43.             String SQLUso = "use pessoas";
  44.             stmt.execute(SQLUso);
  45.             System.out.println("Usando o banco...");
  46.            
  47.             System.out.println("Criando a tabela pessoa...");
  48.             String SQLCriacaoTabela = "CREATE TABLE PESSOA " +
  49.             "(id INTEGER not NULL, " +
  50.             " nome VARCHAR(50), " +
  51.             " cpf CHAR(11), " +
  52.             " rg CHAR (7), " +
  53.             " PRIMARY KEY ( id ))";
  54.             stmt.executeUpdate(SQLCriacaoTabela);
  55.             System.out.println("Tabela pessoa criada!");
  56.            
  57.             System.out.println("Criando a tabela telefone...");
  58.             String SQLCriacaoTabelaTel = "CREATE TABLE TELEFONE " +
  59.             "(id INTEGER not NULL, " +
  60.             " pessoa_id  INTEGER not NULL, " +
  61.             " ddd CHAR(3), " +
  62.             " nr CHAR (9), " +
  63.             " PRIMARY KEY ( id )," +
  64.             " FOREIGN KEY(pessoa_id) REFERENCES PESSOA(id)" +
  65.             ")";
  66.             stmt.executeUpdate(SQLCriacaoTabelaTel);
  67.             System.out.println("Tabela telefone criada!");
  68.            
  69.             System.out.println("Inserir dados na tabela pessoa...");
  70.             String SQLInsercaoPessoa = "insert into pessoa values (1, 'joao', '12345678910', '1234567')";
  71.             stmt.executeUpdate(SQLInsercaoPessoa);
  72.             String SQLInsercaoPessoa2 = "insert into pessoa values (2, 'Maria', '98765432110', '7654321')";
  73.             stmt.executeUpdate(SQLInsercaoPessoa2);
  74.             String SQLInsercaoPessoa3 = "insert into pessoa values (3, 'Jose', '11122233344', '1236547')";
  75.             stmt.executeUpdate(SQLInsercaoPessoa3);
  76.             System.out.println("Dados inseridos em pessoa!");
  77.            
  78.             System.out.println("Inserir dados na tabela Telefone...");
  79.             String SQLInsercaotel = "insert into telefone values (1, 1, '083', '998762345')";
  80.             stmt.executeUpdate(SQLInsercaotel);
  81.             String SQLInsercaotel2 = "insert into telefone values (2, 3, '083', '987651234')";
  82.             stmt.executeUpdate(SQLInsercaotel2);
  83.             String SQLInsercaotel3 = "insert into telefone values (3, 2, '083', '999887766')";
  84.             stmt.executeUpdate(SQLInsercaotel3);
  85.             String SQLInsercaotel4 = "insert into telefone values (3, 2, '083', '33334567')";
  86.             stmt.executeUpdate(SQLInsercaotel4);
  87.             System.out.println("Dados inseridos em teelfone!");
  88.            
  89.         } catch (SQLException e) {         
  90.             e.printStackTrace();
  91.         }
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement