Entradas

Mostrando entradas de octubre 31, 2015

Conexion Java con SQL Server

package com.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Conexion {
    //var
    private Connection cone=null;
    //constructor
    public Conexion(){
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url="jdbc:sqlserver://localhost:1433;databaseName=BD_Supermercado;";
            cone=DriverManager.getConnection(url, "sa", "xxxxx");
        }catch(SQLException ex){
            JOptionPane.showMessageDialog(null, "Error Servidor:"
                    +ex.getMessage());
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Error Driver:"
                    +e.getMessage());
        }
    }
    //metodo
    public Connection getConnection(){
        return cone;
    }
}