Advertisement
Guest User

Untitled

a guest
May 5th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.70 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package pkgPrincipal;
  7.  
  8. /**
  9.  *
  10.  * @author root
  11.  */
  12.  
  13. import org.jdesktop.swingx.plaf.nimbus.NimbusLookAndFeel;
  14.  
  15. import javax.swing.UIManager;
  16. import javax.swing.JOptionPane;
  17.  
  18. import java.sql.Connection;
  19. import java.sql.Statement;
  20. import java.sql.ResultSet;
  21. import java.sql.DriverManager;
  22. import java.sql.SQLException;
  23.  
  24. import com.mysql.jdbc.Driver;
  25. import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;
  26.  
  27.  
  28. public class Principal {
  29.    
  30.     //private
  31.     private Connection conn = null;
  32.     private Statement stmnt = null;
  33.     //public
  34.     public String host = null;
  35.     public ResultSet rs = null;
  36.  
  37.     public void Principal() throws SQLException{
  38.         host = JOptionPane.showInputDialog(null,"Qual sera o endereco do Banco de Dados?","Projeto11102008",JOptionPane.INFORMATION_MESSAGE);
  39.         if(host.toString().equals("") | host.toString().equals(" ") | host.toString().equals(null)){
  40.            
  41.         }
  42.         ConectaBanco();      
  43.     }
  44.    
  45.     private void ConectaBanco() throws SQLException{
  46.         try{
  47.             Driver drv = new Driver();
  48.             conn = DriverManager.getConnection("jdbc:mysql://"+host.toString()+":3306/sistema","root","root@mysql@pedrohms");
  49.             if(conn.isValid(5000)){
  50.                 stmnt = conn.createStatement();
  51.                 rs = stmnt.executeQuery("Select * from log_principal");
  52.                
  53.             }
  54.         }catch(SQLException e){
  55.             if(e.getMessage().toString().equals("Table \'sistema.log_principal\' doesn\'t exist")){
  56.                 JOptionPane.showMessageDialog(null,"Tabela nao existe","erro",JOptionPane.ERROR_MESSAGE);
  57.                 if(JOptionPane.showConfirmDialog(null, "Voce gostaria que o sistema crie a tabela?","Projeto11102008",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){
  58.                     stmnt.execute("create table log_principal(id_log BIGINT(18) NOT NULL AUTO_INCREMENT,log VARCHAR(100),user VARCHAR(255),PRIMARY KEY(id_log))");
  59.                 }
  60.             }else if(e.getClass() == CommunicationsException.class){
  61.                 JOptionPane.showMessageDialog(null,"Host nao encontrado !","Projeto11102008",JOptionPane.ERROR_MESSAGE);
  62.             }
  63.         }
  64.     }
  65.    
  66.     public static void main(String args[]){
  67.         try{
  68.             UIManager.setLookAndFeel(new NimbusLookAndFeel());
  69.             Principal principal = new Principal();
  70.             principal.Principal();
  71.         }catch(Exception e){
  72.             JOptionPane.showMessageDialog(null,e.getMessage(),"erro",JOptionPane.ERROR_MESSAGE);
  73.         }        
  74.     }
  75.    
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement