JSPʵÏÖ¼òÒ×µÄSQL±¨±í
ÊäÈë: select ID as ±àºÅ, NAME as ÐÕÃû, AGE as ÄêÁä from XXX
Êä³ö:
񅧏
ÐÕÃû
ÄêÁä
ÒªÇóÊÇÈç¹ûSQL±ä¶¯, ÈÔȻҪÏÔʾ³öÀ´ËùÓеıðÃû×Ö¶ÎÐÅÏ¢ºÍÊý¾Ý.
ÒòΪÏÖÔÚHibernateÓõıȽϹ㷺, ËùÒÔÓÅÏÈ¿¼ÂÇÓÃHibernateÀ´ÊµÏÖ, ½á¹û·¢ÏÖÈç¹ûÊÇʵÌåÓ³Éä²éѯÓï¾ä, ¿ÉÒÔ·½±ãµÄÓÃ:List<String> Query.getReturnAliases() »ñµÃ±ðÃû, È»¶øÎÒÃÇÖªµÀ²éѯʱÓÐʱºòÓï¾äÊǺܸ´ÔÓµÄ, ²»È«ÊÇHQL, ÕâʱºòÓÃSQLQueryµÄʱºò, ¾ªÑȵÄÌáʾÕâ¸ö·½·¨ÉÐδʵÏÖ(×îаæµÄHibernate 3.3 ÊÇ·ñʵÏÖÉÐδ²âÊÔ), Óõİ汾ÊÇHibernate 3.2, ¶ÔÓ¦µÄ´úÂëÊÇ:
DAO
/**
* ¸ù¾Ý²éѯÓï¾ä·µ»Ø½á¹û, ²¢°üº¬½á¹ûµÄÁÐÃû
* @param hql
* @param args
* @return
*/
public List queryAllForReport( final String hql, final Object... args) {
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
for(int i =0; i < args.length; i++) {
query.setParameter(i, args[i]);
}
List list = query.list();
list.add(0, query.getReturnAliases());
return list;
}
});
// Hibernate×öcount¼ÆËã·µ»ØÒ»°ã¶¼ÊǶÔÏó
return list;
}
²âÊÔ´úÂë:
List<Object[]> list = dao.queryAllForReport("select id as ±àºÅ, name as µÇ¼Ãû, address as µØÖ·, realName from User");
System.out.println(list.size());
for(Object[] row : list) {
for(Object v : row) {
System.out.print(v + "\t");
}
System.out.println();
}
×îºó²»µÃ²»»Øµ½JDBC, Óà ResultSetºÍResultSetMetaDataʵÏÖÁËÕâ¸ö¹¦ÄÜ, Ïêϸ´úÂë(×Ô¼º½øÐÐÐ޸ļ´¿ÉʵÏÖ)ÈçÏÂ:
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SQL±¨±í&l
Ïà¹ØÎĵµ£º
ÓÃJavaÁ¬½ÓSQL Server2000Êý¾Ý¿âÓжàÖÖ·½·¨£¬ÏÂÃæ½éÉÜÆäÖÐ×î³£ÓõÄÁ½ÖÖ£¨Í¨¹ýJDBCÇý¶¯Á¬½ÓÊý¾Ý¿â£©¡£
1. ͨ¹ýMicrosoftµÄJDBCÇý¶¯Á¬½Ó¡£´ËJDBCÇý¶¯¹²ÓÐÈý¸öÎļþ£¬·Ö±ðÊÇmssqlserver.jar¡¢msutil.jarºÍmsbase.jar£¬¿ÉÒÔµ½Î¢ÈíµÄÍøÕ¾È¥ÏÂÔØ£¨http://www.microsoft.com/downloads/details.aspx?FamilyId=07287B11-0502-461A- ......
/*
Ô±í£º
thid other
a 1
a 1
b 0
b 0
......
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Color getRandColor(int fc,int bc){//¸ø¶¨·¶Î§»ñµÃËæ»úÑÕÉ«
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+ran ......
Ô¶³ÌÁ¬½Ósql server 2000·þÎñÆ÷µÄ½â¾ö·½°¸
----ת×Ô£º·ÉºüСÎÝ
ÓÉÓ ......
CREATE proc page
@RecordCount int output,
@QueryStr nvarchar(100)='table1',--±íÃû¡¢ÊÓͼÃû¡¢²éѯÓï¾ä
@PageSize int=20, --ÿҳµÄ´óС(ÐÐÊý)
@PageCurrent int=2, --ÒªÏÔʾµÄÒ³ ´Ó0¿ªÊ¼
@FdShow nvarchar (1000)='*', --ÒªÏÔʾµÄ×Ö¶ÎÁбí
@IdentityStr nvarchar (100)='id', --Ö÷¼ü
@WhereStr nvarchar ......