/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.derby.impl.drda.NetworkServerControlImpl;
/**
*
* @author charleston anjos
*/
public class Conexao {
public static ResultSet res;
public static Connection conn;
public static PreparedStatement pst;
public static Statement sta;
public static String sSQL;
private final String usuario = "seu usuario";
private final String senha = "sua senha";
private final String db = "seu banco";
private final String driver = "org.apache.derby.jdbc.ClientDriver";
private final String servidor = "localhost:1527";
private final String url = "jdbc:derby://" + servidor + "/"+ db;
//conexao embarcada
public void conectar(){
try{
System.setProperty("derby.system.home","/home/lionel/.netbeans-derby");
NetworkServerControlImpl networkServer = new NetworkServerControlImpl();
networkServer.start(new PrintWriter(System.out));
Class.forName(driver).newInstance();
this.conn = java.sql.DriverManager.getConnection(url + ";create=true",usuario,senha);
this.sta = conn.createStatement();
System.out.println("Banco de dados conectado");
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Erro ao conectar banco de dados \\n\\n" + e.getMessage());
System.exit(0);
}
}
public void desconectar(){
if(conn != null){
try{
conn.close();
System.out.println("Banco de dados desconectado");
}catch(Exception e){
e.printStackTrace();
} finally{
conn = null;
try {
DriverManager.getConnection(url + ";shutdown=true");
} catch (SQLException ex) {
Logger.getLogger(this.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}