1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/*
La table plus simple il n'y a pas :
USE [jspTest]
GO
/****** Objet : Table [dbo].[test1] Date de génération du script : 09/25/2006 23:23:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[test1](
[id] [int] IDENTITY(1,1) NOT NULL,
[prenom] [nchar](50) COLLATE French_CI_AS NOT NULL,
[nom] [nchar](50) COLLATE French_CI_AS NOT NULL,
CONSTRAINT [PK_test1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
*/
// 1 - la classe permettant d'accéder aux données :
/*
* SqlConnection.java
*
* Created on 23 septembre 2006, 12:26
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package sqlserver.mydb.common;
/**
*
* @author david
*/
import java.io.*;
import java.sql.*;
public class SqlConnection {
/**
* Creates a new instance of SqlConnection
*/
public SqlConnection() {
}
private String driver;
private String url;
private Connection cnx;
private Statement st;
public boolean ConnectToSQL()
{
try
{
Class.forName(getDriver());
cnx=DriverManager.getConnection(getUrl());
st=cnx.createStatement();
return true;
}
catch(SQLException ex)
{
System.out.println(ex.getMessage().toString());
return false;
}
catch(ClassNotFoundException ex)
{
System.out.println(ex.getMessage().toString());
return false;
}
}
public boolean CloseSql()
{
try
{
this.cnx.close();
return true;
}
catch(SQLException ex)
{
System.out.println(ex.getMessage().toString());
return false;
}
}
public ResultSet Requete(String req)
{
ResultSet rs=null;
try{
rs=st.executeQuery(req);
return rs;
}
catch(SQLException ex)
{
rs=null;
return rs;
}
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url)
{
this.url=url;
}
}
// 2 - La page jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>JSP Page</h1>
<%
boolean isOK=false;
sqlserver.mydb.common.SqlConnection myConn=new sqlserver.mydb.common.SqlConnection();
myConn.setDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver");
myConn.setUrl("jdbc:sqlserver://localhost:1433;databaseName=jspTest;user=*****;password=******");
isOK=myConn.ConnectToSQL();
if(!isOK)
{
out.println("Erreur");
}
else
{
out.println("OK");
ResultSet rs1=myConn.Requete("select * from test1");
int i=0;
while(rs1.next()){
i++;
out.println("<li>" + i + " " + rs1.getString("Prenom") + "-" + rs1.getString("nom"));
}
myConn.CloseSql();
}
%>
</body>
</html> |
Partager