学生可以自由选择要报考的科目。
请问怎样联系这两个表呢?
写了一个例子...自己参考一下..
import java.sql.*;
public class JoinDemo{
public static void main(String[] args){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); //载入sql server驱动
String conUrl="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Northwind";
Connection dbConn=DriverManager.getConnection(conUrl,"sa","admin"); //创建Connection对象
//通过Inner Join语句.连接两个表.使用Orders的主键EmployeeID和Employees的外键EmployeeID来建立关系
String sql="Select Orders.OrderID ,Orders.ShipAddress,Employees.lastname," +
"Employees.firstname From Orders Inner Join Employees On Orders.EmployeeID=Employees.EmployeeID";
Statement stmt=dbConn.createStatement();
ResultSet rs=stmt.executeQuery(sql); //执行sql查询
while(rs.next()){ //当存在下一条,则输出它
String orderid=rs.getString(1);
String shipaddress=rs.getString(2);
String lastname=rs.getString(3);
String firstname=rs.getString(4);
System.out.println(orderid+"\t"+shipaddress+"\t"+lastname+"\t"+firstname);
}
rs.close();
stmt.close();
dbConn.close();
}catch(Exception sqlexc){
sqlexc.printStackTrace();
}
}
}