Monday, August 31, 2009

CONFIGURACION JDBC PARA SQL SERVER 2005 EXPRESS

En esta oportunidad mostrae la configuración para crear una conexión JDBC entre una aplicación JAVA con el respectivo servidor SQL Server 2005 Express.

Los requerimientos previos y el software necesario son los siguientes:

Requerimientos Previos:>

- NetFramework v4.0.exe (DESCARGAR.)
- Windows Installer v4.5.exe (DESCARGAR.)

Herramienta SQL Sever (DESCARGAR AMBOS.):

- SQL Server 2005 Express.exe.
- SQL Server Manager Studio 2005 Express.msi

Para acceder a este motor de datos vía una aplicación JAVA JDBC, se debe de realizar lo siguiente:

Descargar los drivers de conexión desde AQUI.:

- sqljdbc.jar
- sqljdbc4.jar
- mssqlserver.jar
- msbase.jar
- msutil.jar
- jtds-1.2.2.jar

Acceder a tu herramienta de desarrollo ECLIPSE y crear un proyecto Dinamic Web y en la carpeta lib ingresar los .Jars de conexión descargados. Luego, creamos una clase .java para la conexión con los motores de datos SQL Server 2000 y SQL Server 2005, aquí comparto mi clase que desarrolle:

package org.java;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;

public class ConexionSQLServeJDBC{

private Connection conexion = null;
private final String url = "jdbc:microsoft:sqlserver://";
private final String servidor = "localhost\\SQLEXPRESS";
//"192.168.1.33"; //"LOCALHOST"; //"LOCALHOST\\SQLEXPRESS SQLSERVER2000";
private final String puerto = "1433";
private final String baseDatos = "DEMODB";
private final String usuario = "sa";
private final String password = "sql";
private final String metodo = "cursor"; //direct - cursor;

//Constructor.
public ConexionSQLServeJDBC(){
}

//Levanta la Aplicacion.
public static void main( String[] args ) throws Exception{
ConexionSQLServeJDBC conexionJDBC = new ConexionSQLServeJDBC();
conexionJDBC.propiedadesDataBase();
}
/**
* getConnectionUrl
* @return String
*/
private String getConnectionUrl(){

/*
* El driver de Microsoft NO funciona con "servidor\instancia". Para solucionar el
* tema, debes indicar la direccion IP del servidor + el puerto de la instancia.
*/

//String SQL = ( this.url + this.servidor + ":" + this.puerto + ";databaseName=" + this.baseDatos + ";" + "selectMethod=" + this.metodo + ";" );

String SQL = ( this.url + this.servidor + ":" + this.puerto + ";databaseName=" + this.baseDatos + ";" );

System.out.println( "Cadena CONEXION JDBC: " + SQL );

return SQL;
}
/**
* getConnection
* @return Connection
*/
private Connection getConnection(){
try{
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver" );
this.conexion = java.sql.DriverManager.getConnection( this.getConnectionUrl(), this.usuario, this.password );

if( this.conexion != null ){
System.out.println( "Conexión .... OK" );
}
else{
System.out.println( "Conexión .... NOK" );
}
}
catch( Exception e ){
e.printStackTrace();
System.out.println( "Error de seguimiento en 'getConnection()': " + e.getMessage() );
}
return this.conexion;
}

/**
* propiedadesDataBase
*/
public void propiedadesDataBase(){

DatabaseMetaData metaData = null;
ResultSet rs = null;
int contador = 0;

try{
this.conexion = this.getConnection();

if( this.conexion != null ){
metaData = this.conexion.getMetaData();

System.out.println( "" );
System.out.println( "****** INFORMACION DEL CONTROLADOR JDBC ******" );
System.out.println( " - NOMBRE: " + metaData.getDriverName() );
System.out.println( " - VERSION: " + metaData.getDriverVersion() );
System.out.println( "" );
System.out.println( "****** INFORMACION DE BASE DE DATOS ******" );
System.out.println( " - NOMBRE: " + metaData.getDatabaseProductName() );
System.out.println( " - VERSION: "+ metaData.getDatabaseProductVersion() );
System.out.println( "" );
System.out.println( " - BASES DE DATOS ACTIVAS: " );

rs = metaData.getCatalogs();

while( rs.next() ){
contador += 1;
System.out.println( " BD #" + contador + ": " + rs.getString( 1 )
);
}
this.cerrarConexiones( this.conexion, rs );
}
else{
System.out.println( "Error: No hay ninguna Conexión SQL Activa" );
}
}
catch( Exception e ){
e.printStackTrace();
}
}
/**
* cerrarConexiones
* @param conexion
* @param rs
*/
private void cerrarConexiones( Connection conexion, ResultSet rs ){
try{
if( conexion != null ){
conexion.close();
}
if( rs != null ){
rs.close();
}
}
catch( Exception e ){
e.printStackTrace();
}
}
}

2 comments:

Java Men said...

Hola JavaMan, intenté bajar los enlaces del programa de conexion jdbc y el doc de explicación y no existen archivos para descargarlos en el repositorio.

Podrías enviármelos y/o colocarlos nuevamente para su descarga por favor. digitanpro@gmail.com

Gracias!!!

JAVAMAN said...

Listo doc ya hice UPDATE al LINK, el problema es el roche con MEGAUPLOAD que con el, he perdido mas de 100 fuentes.