document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. import java.io.PrintWriter;
  7. import java.sql.*;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import javax.swing.JOptionPane;
  11. import org.apache.derby.impl.drda.NetworkServerControlImpl;
  12.  
  13.  
  14. /**
  15.  *
  16.  * @author charleston anjos
  17.  */
  18. public class Conexao {
  19.    
  20.     public static ResultSet res;
  21.     public static Connection conn;
  22.     public static PreparedStatement pst;
  23.     public static Statement sta;
  24.     public static String sSQL;  
  25.    
  26.     private final String usuario = "seu usuario";
  27.     private final String senha = "sua senha";
  28.  
  29.     private final String db = "seu  banco";
  30.     private final String driver = "org.apache.derby.jdbc.ClientDriver";
  31.     private final String servidor = "localhost:1527";
  32.     private final String url = "jdbc:derby://" + servidor + "/"+ db;
  33.  
  34.     //conexao embarcada
  35.     public void conectar(){
  36.         try{
  37.            
  38.             System.setProperty("derby.system.home","/home/lionel/.netbeans-derby");
  39.            
  40.             NetworkServerControlImpl networkServer = new NetworkServerControlImpl();
  41.             networkServer.start(new PrintWriter(System.out));
  42.            
  43.             Class.forName(driver).newInstance();
  44.  
  45.             this.conn = java.sql.DriverManager.getConnection(url + ";create=true",usuario,senha);
  46.  
  47.             this.sta = conn.createStatement();
  48.  
  49.             System.out.println("Banco de dados conectado");
  50.  
  51.         }catch(Exception e){
  52.             e.printStackTrace();
  53.             JOptionPane.showMessageDialog(null, "Erro ao conectar banco de dados \\n\\n" + e.getMessage());
  54.             System.exit(0);
  55.         }
  56.     }
  57.  
  58.  
  59.    
  60.     public void desconectar(){
  61.        
  62.         if(conn != null){
  63.             try{
  64.                 conn.close();
  65.            
  66.                 System.out.println("Banco de dados desconectado");
  67.             }catch(Exception e){
  68.                 e.printStackTrace();
  69.             } finally{
  70.                 conn = null;
  71.                 try {  
  72.                     DriverManager.getConnection(url + ";shutdown=true");
  73.                 } catch (SQLException ex) {
  74.                     Logger.getLogger(this.class.getName()).log(Level.SEVERE, null, ex);
  75.                 }
  76.             }
  77.         }
  78.        
  79.     }
  80.    
  81.    
  82. }
');