数据库三张表查询,一对多 一个主表 二个附表。

查询“计算机”专业学生在“2017-12-15”至“2018-1-8”时间段内借书的学生编号、 学生名称、图书编号、图书名称、借出日期;
SELECT
student.stuID,student.stuName,book.BID,book.title,borrow.T_time
from
book,borrow,student
WHERE
student.stuID=borrow.stuID
and book.BID=borrow.BID
and student.major=“计算机”
and borrow.T_time BETWEEN “2017-12-15” and “2018-1-8”

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现多张的复制,可以使用Java中的JDBC技术,通过建立两个数据库的连接,然后使用SQL语句来将数据从源数据库中的复制到目标数据库中的。 以下是一个示例代码,可供参考: ```java import java.sql.*; public class TableCopy { public static void main(String[] args) { String sourceDBUrl = "jdbc:mysql://localhost:3306/source_db"; String sourceDBUser = "root"; String sourceDBPass = "password"; String targetDBUrl = "jdbc:mysql://localhost:3306/target_db"; String targetDBUser = "root"; String targetDBPass = "password"; String[] tablesToCopy = { "table1", "table2", "table3" }; try { // Connect to source database Connection sourceConn = DriverManager.getConnection(sourceDBUrl, sourceDBUser, sourceDBPass); // Connect to target database Connection targetConn = DriverManager.getConnection(targetDBUrl, targetDBUser, targetDBPass); // Loop through tables and copy data for (String table : tablesToCopy) { Statement sourceStmt = sourceConn.createStatement(); ResultSet rs = sourceStmt.executeQuery("SELECT * FROM " + table); PreparedStatement targetStmt = targetConn.prepareStatement("INSERT INTO " + table + " VALUES (?, ?, ?)"); while (rs.next()) { // Copy data from source to target targetStmt.setInt(1, rs.getInt(1)); targetStmt.setString(2, rs.getString(2)); targetStmt.setDouble(3, rs.getDouble(3)); targetStmt.executeUpdate(); } // Close statements and result sets rs.close(); sourceStmt.close(); targetStmt.close(); } // Close connections sourceConn.close(); targetConn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,我们首先定义了源数据库和目标数据库的连接信息以及要复制的。然后,我们通过循环遍历要复制的每个,并使用SELECT语句从源数据库中检索数据。接下来,我们使用INSERT语句将检索到的数据插入到目标数据库的相应中。最后,我们关闭所有的数据库连接和语句。 需要注意的是,在实际使用中,我们还需要考虑一些其他问题,如事务处理、异常处理等。此外,我们还可以使用一些开源的Java库,如Apache Commons DBUtils或Spring JDBC等,来简化数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值