用 JDBC 查询学生成绩单, 把主要代码写出来

 SC表

/*
Navicat MySQL Data Transfer

Source Server         : mysql
Source Server Version : 50549
Source Host           : localhost:3306
Source Database       : ooxx

Target Server Type    : MYSQL
Target Server Version : 50549
File Encoding         : 65001

Date: 2019-08-19 16:10:39
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for sc
-- ----------------------------
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (
  `sno` int(11) NOT NULL,
  `cno` int(11) NOT NULL,
  `score` int(11) DEFAULT NULL,
  PRIMARY KEY (`sno`,`cno`),
  KEY `cno` (`cno`),
  CONSTRAINT `sc_ibfk_1` FOREIGN KEY (`sno`) REFERENCES `student` (`sno`),
  CONSTRAINT `sc_ibfk_2` FOREIGN KEY (`cno`) REFERENCES `course` (`cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of sc
-- ----------------------------
INSERT INTO `sc` VALUES ('1', '1', '90');
INSERT INTO `sc` VALUES ('1', '2', '50');
INSERT INTO `sc` VALUES ('1', '3', '66');
INSERT INTO `sc` VALUES ('2', '1', '90');
INSERT INTO `sc` VALUES ('2', '2', '100');
INSERT INTO `sc` VALUES ('2', '3', '86');
INSERT INTO `sc` VALUES ('3', '1', '90');
INSERT INTO `sc` VALUES ('3', '2', '77');
INSERT INTO `sc` VALUES ('3', '3', '57');
INSERT INTO `sc` VALUES ('4', '1', '90');
INSERT INTO `sc` VALUES ('4', '2', '100');
INSERT INTO `sc` VALUES ('4', '3', '78');
INSERT INTO `sc` VALUES ('5', '2', '45');

student表

/*
Navicat MySQL Data Transfer

Source Server         : mysql
Source Server Version : 50549
Source Host           : localhost:3306
Source Database       : ooxx

Target Server Type    : MYSQL
Target Server Version : 50549
File Encoding         : 65001

Date: 2019-08-19 16:10:32
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `sno` int(11) NOT NULL,
  `sname` varchar(50) DEFAULT NULL,
  `sage` int(11) DEFAULT NULL,
  `ssex` varchar(2) DEFAULT NULL,
  PRIMARY KEY (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', 'lmd', '21', '男');
INSERT INTO `student` VALUES ('2', '刘蕾', '23', '女');
INSERT INTO `student` VALUES ('3', '李斌', '21', '男');
INSERT INTO `student` VALUES ('4', '天使', '18', '女');
INSERT INTO `student` VALUES ('5', '魔鬼', '99', '男');
import java.sql.*;

public class Test02 {
    public static void main(String[] args) {
        Connection cn = null;
        PreparedStatement ps =null;
        ResultSet rs = null;
        try
        {
            cn = C3p0Util.getConnection();
            ps = cn.prepareStatement("select sc.* from sc left join student stu on sc.sno=stu.sno where sname=?");
            ps.setString(1,"lmd");
            rs = ps.executeQuery();
            while(rs.next())
            {
                System.out.println(rs.getInt("cno") + " " +
                        rs.getInt("score"));
            }
        }catch(Exception e){e.printStackTrace();}
        finally
        {
            try {
                cn.close();
                ps.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

你可能感兴趣的:(【JAVA】JavaWeb)