2011年11月23日

(转贴)数据库连接(内连接,外连接,交叉连接)

数据库连接分为:内连接,外连接(左、右连接,全连接),交叉连接
文章地址 : http://www.zxbc.cn/html/20080527/51189.html
转载 
内连接:把两个表中数据对应的数据查出来 
外连接:以某个表为基础把对应数据查出来(全连接是以多个表为基础) 
student表 
no name 
1     a 
2     b 
3     c 
4     d 
grade表 
no grade 
1     90 
2     98 
3     95 
内连接 inner join(查找条件中对应的数据,no4没有数据不列出来) 
语法:select * from student inner join grade on student.no = grade.no 
结果 
student.no name grade.no grade 
1             a             1         90 
2             b             2         98 
3             c             3         95 
左连接(左表中所有数据,右表中对应数据) 
语法:select * from student left join grade on student.no = grade.no 
结果: 
student.no name grade.no grade 
1                 a         1         90 
2                 b         2         98 
3                 c         3         95 
4                 d     
右连接(右表中所有数据,左表中对应数据) 
语法:select * from student right join grade on student.no = grade.no 
结果: 
student.no name grade.no grade 
1                 a         1         90 
2                 b         2         98 
3                 c         3         95 
全连接 
语法:select * from student full join grade on student.no = grade.no 
结果: 
no name grade 
1     a     90 
2     b     98 
3     c     95 
4     d 
1     a     90 
2     b     98 
3     c     95 
注:access 中不能直接使用full join ,需要使用union all 将左连接和右连接合并后才可以

交叉连接
将两个表所有行组合,连接后的行数为两个表行数的乘积(笛卡尔积)
语法,借用上面的例子应该是
select * from student cross join grade

行数应该为12行 :
no name grade 
1     a     90 
2     b     98 
3     c     95 
4     d  
1     a     90 
2     b     98 
3     c     95 
4     d 
1     a     90 
2     b     98 
3     c     95 
4     d 

posted @ 2011-11-30 17:24 AK47 阅读(475) | 评论 (0)编辑 收藏

JAXB向Xml非根节点添加一个或多个属性

JAXB 向Xml非根节点添加一个或多个属性,直接上代码,关于JAXB的相关注解可查阅JAVA API。

原创文章,转载请注明出处。http://www.blogjava.net/kangdy/archive/2011/11/23/364635.html

code1: colors类  根节点
code1
package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Colors")
@XmlAccessorType(XmlAccessType.FIELD)
public class Colors {
    
    @XmlElement(name = "red",nillable=true)
    private Red red;
    
    @XmlElement(name = "blue",nillable=true)
    private Blue blue;

    public Red getRed() {
        return red;
    }

    public Blue getBlue() {
        return blue;
    }

    public void setRed(Red red) {
        this.red = red;
    }

    public void setBlue(Blue blue) {
        this.blue = blue;
    }
}

code2:  Red类  子节点
code2package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "red")
@XmlAccessorType(XmlAccessType.FIELD)
public class Red {
    
    private String value;
    
    @XmlAttribute(name = "att1")
    private String att;
    
    public String getValue() {
        return value;
    }
    
    public void setValue(String value) {
        this.value = value;
    }

    public String getAtt() {
        return att;
    }

    public void setAtt(String att) {
        this.att = att;
    }
    
}


code3:  类 Blue 子节点
code3
package com.kangdy.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "blue")
@XmlAccessorType(XmlAccessType.FIELD)
public class Blue {
    private String value;
    
    @XmlAttribute(name = "att2")
    private String att2;
    
    @XmlAttribute(name = "att1")
    private String att;
    
    public String getAtt() {
        return att;
    }

    public void setAtt(String att) {
        this.att = att;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getAtt2() {
        return att2;
    }

    public void setAtt2(String att2) {
        this.att2 = att2;
    }
}

code4: main类
code4
package com.kangdy.test;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Jaxbtest {
    public static void main(String[] args) throws Exception {

        StringWriter writer = new StringWriter();
        JAXBContext jc = JAXBContext.newInstance(Colors.class);
        Marshaller ma = jc.createMarshaller();
        ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        
        Colors colors = new Colors();
        Red red = new Red();
        red.setAtt("att-red");
        red.setValue("red");
        Blue blue = new Blue();
        blue.setValue("blue");
        blue.setAtt("att-blue");
        blue.setAtt2("blue-att2");
        colors.setRed(red);
        colors.setBlue(blue);
        
        ma.marshal(colors, writer);
        System.out.println(writer.toString());

    }
}

运行结果:
结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Colors>
    <red att1="att-red">
        <value>red</value>
    </red>
    <blue att1="att-blue" att2="blue-att2">
        <value>blue</value>
    </blue>
</Colors>

posted @ 2011-11-23 14:33 AK47 阅读(10080) | 评论 (4)编辑 收藏

<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜