Sunteți pe pagina 1din 12

CONEXIN DIRECTA SQL SERVER 2008 JAVA

CONEXIN SQL SERVER 2008 A UNA PAGINA WEB JSP

Para conectarse existen drivers de 4 tipos, los de tipo 3 y 4 sirven para conectarse directamente. El de tipo 1 viene incluido en las libreras de java y requiere crear DSN de usuario, y configurar el Orgenes de datos (ODBC).

Este es el que vamos a utilizar para crear nuestra conexin. Primero que todo debemos crear una base de datos en SQL server 2008

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

BASE DE DATOS
CREATE DATABASE Registro GO USE Registro GO CREATE TABLE Asignatura( idAsig int IDENTITY (100,1)PRIMARY KEY, nombre varchar(50), creditosTeoricos int NOT NULL, creditosPracticos int NOT NULL, tipo char(1) NOT NULL, cuatrimestre int NOT NULL )

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Ahora vamos a crear en origen de datos :

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Para agregar un nuevo origen de datos damos click en el botn agregar.

En esta ventana vamos a seleccionar el driver de SQL

Finalizamos y empezaremos con el asistente de creacin del nuevo origen de datos.

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Ingresamos un nombre con el cual identificaremos el origen de datos, una descripcin, y el nombre del servidor. Y damos siguiente.

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Le establecemos nuestra base de datos al origen de datos.

Y finalizamos el asistente.

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Vamos ahora ingresar a netbeans, tenga en cuenta que debe tener instalado el servidor Apache TomCat. Que el servidor en el cual se alojara y ejecutara nuestra pgina web. Creamos un nuevo proyecto de aplicacin web, siguiendo el asistente.

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Cdigo del ejemplo de pgina JSP: index.jsp


<%-Document : index Created on : 2/11/2011, 08:30:14 PM Author : Zheryio Pc --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <! DOCTYPE html> <%@page import= "java.sql.*"%> <% // Cadena de conexin. String connectionUrl = "jdbc:odbc:registro";//nombre del orgen de datos Connection con = null; Statement stmt = null; ResultSet rs = null; try { // Carga el driver sqljdbc Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch (Exception e) { e.printStackTrace();} %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Conexin Directa SQL Server 2008</title> <style type="text/css"> <!--.Estilo3 {color: #FFFFFF; font-weight: bold;}--></style> </head> <body> <h2 align="center">Tabla Asignatura </h2> <table width="80%" border="1" align="center" bordercolor="#000000"> <tr bgcolor="#0033CC"> <td> <div align="center" class="Estilo3">idAsig</div></td> <td><div align="center" class="Estilo3">nombre</div></td> <td><div align="center" class="Estilo3">creditosTeoricos</div></td> <td><div align="center" class="Estilo3">creditosPracticos</div></td> <td><div align="center" class="Estilo3">tipo </div></td> <td><div align="center" class="Estilo3">cuatrimestre</div></td> </tr>

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA


<% try{ con = DriverManager.getConnection(connectionUrl); //Crear y ejecutar una sentencia SQL que devuelve algunos datos. String SQL = "SELECT * FROM Asignatura"; //consulta a la base de datos stmt = con.createStatement();// objeto para el envo de sentencias SQL para la base de datos. rs = stmt.executeQuery(SQL); //ejecuta la sentencia de la base de datos while (rs.next()) { //carga la tabla de la base de datos out.print("<tr bordercolor=#000000 bgcolor=#FFFFFF>"); out.print("<td>"+ rs.getString("idAsig") +"</td>"); out.print("<td>"+ rs.getString("nombre") + "</td>"); out.print("<td>"+ rs.getString("creditosTeoricos") + "</td>"); out.print("<td>"+ rs.getString("creditosPracticos") + "</td>"); out.print("<td>"+ rs.getString("tipo") + "</td>"); out.print("<td>"+ rs.getString("cuatrimestre") + "</td>"); out.print("</tr>"); } } catch(Exception e) { e.printStackTrace(); } finally { if (rs != null) try {rs.close();} catch(Exception e){} if (stmt != null) try { stmt.close(); } catch(Exception e) {} if (con != null) try { con.close(); } catch(Exception e) {} } %> </body> </html>

Sergio Alejandro Prada

CONEXIN DIRECTA SQL SERVER 2008 JAVA

Pgina web:

SQL server 2008:

Sergio Alejandro Prada

S-ar putea să vă placă și