建立一个stu学生表,建立一个te老师表,在stu表设置外键tid,对两张表进行相关查询。
1:根据外键tid查询老师名字(Navicat)
```
Connection connection = JdbcUtil.getConnection();
String sql="select * from stu s ,te t where s.tid=t.id";
```
查询成功,最后一列为te表老师名字。
![M`{CCK]2A$0WSPO`YWQF%HM.jpg](https://upload-images.jianshu.io/upload_images/7428976-3c157462886dcb8a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2:查询学生名字及所对应的老师名字(Navicat)
Connection connection = JdbcUtil.getConnection();
String sql="select s.`name`,t.`name` from stu s ,te t where s.tid=t.id";
查询结果为两列。
3:查询学生id,名字,性别,老师名字
A: IDEA代码查询(笨方法)
```
public class QueryDemos {
public static void main(String[] args) throws SQLException {
Connection connection = JdbcUtil.getConnection();
String sql="select s.id,s.name,s.sex,t.tname from stu s,te t where s.tid=t.id";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String sex = resultSet.getString("sex");
String tname = resultSet.getString("tname");
System.out.println(id+"\t"+name+"\t"+sex+"\t"+tname);
}
JdbcUtil.closeResouce(connection,statement,resultSet);
}
}
```
查询结果
```
1 王芝洋 女 陈勇
2 汉斯 男 曹阳
4 ewdwe 女 陈勇
5 ewdw 女 陈勇
6 张得到 女 曹阳
7 张给他 女 曹阳
8 放入福 女 曹阳
9 一湖光塔影 男 陈勇
11 汉斯 男 曹阳
```
B:代码改造后:
Student类:
```
package com.pp.Bean;
public class Student {
private int id;
private String name;
private int age;
private String sex;
private String tname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTname() {
return tname;
}
@Override
public String toString() {
return "Student{" +
"id=" + id + ",\n " +
"name='" + name+ '\'' + ",\n" +
" age=" + age+ ", \n" +
"sex='" + sex+ + '\'' + ",\n " +
"tname='" + tname + '\'' +"\n"+
'}'+"\n";
}
public void setTname(String tname) {
this.tname = tname;
}
}
```
QueryDemos类:
```
package com.pp.Bean;
import com.pp.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class QueryDemos {
public static void main(String[] args) throws SQLException {
Connection connection = JdbcUtil.getConnection();
String sql="select s.id,s.name,s.age,s.sex,t.tname from stu s,te t where s.tid=t.id";
ArrayList students = new ArrayList();
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()){
Student student = new Student();
student.setId(resultSet.getInt("id"));
student.setName(resultSet.getString("name"));
student.setAge(resultSet.getInt("age"));
student.setSex(resultSet.getString("sex"));
student.setTname( resultSet.getString("tname"));
students.add(student);
}
System.out.println(students);
JdbcUtil.closeResouce(connection,statement,resultSet);
}
}
```
运行结果:
```
[Student{id=1,
name='王芝洋',
age=90,
sex='女39,
tname='陈勇'
}
, Student{id=2,
name='汉斯',
age=30,
sex='男39,
tname='曹阳'
}
, Student{id=4,
name='ewdwe',
age=66,
sex='女39,
tname='陈勇'
}
, Student{id=5,
name='ewdw',
age=77,
sex='女39,
tname='陈勇'
}
, Student{id=6,
name='张得到',
age=23,
sex='女39,
tname='曹阳'
}
, Student{id=7,
name='张给他',
age=23,
sex='女39,
tname='曹阳'
}
, Student{id=8,
name='放入福',
age=23,
sex='女39,
tname='曹阳'
}
, Student{id=9,
name='一湖光塔影',
age=33,
sex='男39,
tname='陈勇'
}
, Student{id=11,
name='汉斯',
age=30,
sex='男39,
tname='曹阳'
}
]
```
查询一条语句:(ID为2的学生的老师的ID,名字,年龄,性别,名字)
sql语句为:
String sql="select s.id,s.name,s.age,s.sex,t.tname from stu s,te t where s.tid=t.id and s.id=?";
再加入一条语句:
statement.setInt(1,2);//第二个人的老师
结果为:
```
[Student{id=2,
name='汉斯',
age=30,
sex='男,
tname='曹阳'
}
]
```