要求:
要求:
要求:输入学生的学号,列出学生所修课程的成绩和加权平均分。或者输入all,列出所有学生的加权平均分。
test
package test8;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class stu {
public static void main(String[] args) {
DBConnection db=new DBConnection();
System.out.println("How many students you want input?");
int i=0;
Scanner input= new Scanner(System.in);
i = input.nextInt();
System.out.println("Input student infomation(Format:2016110201 A 1 1998-01-01)");
for(int j=0;j<i;j++)
{
String str = input.nextLine();
str = input.nextLine();
String[] stustr = str.split(" ");
db.add_student(stustr[0],stustr[1],stustr[2],stustr[3]);
}
//插入学生信息
System.out.println("How many course you want input?");
i = input.nextInt();
System.out.println("Input course infomation(Format:001 Math 4)");
for(int j=0;j<i;j++)
{
String str = input.nextLine();
str = input.nextLine();
String[] stustr = str.split(" ");
db.add_course(stustr[0],stustr[1],stustr[2]);
}
//插入课程信息
System.out.println("Whose grade you want add?");
String stuNo = input.next();
while(!db.select_stuno(stuNo))
{
System.out.println("please input student number again?");
stuNo = input.next();
}
//输入查找学生的学号
System.out.println("which course you want to input grade ?");
db.select_couname();
System.out.println();
String couName = input.next();
//列出可选课程
String couno = null;
String grade = null;
if(!db.select_grade(stuNo, couName))
{
couno=db.couname_to_couno(couName);
System.out.println("please input grade (0~100)");
grade = input.nextLine();
db.add_scg(stuNo, couno, grade);
}
{
System.out.println("the grade is exist,are you iuput again?(y/n)");
if(input.next().equals("y"))
{
System.out.println("please input grade (0~100)");
grade = input.nextLine();
db.update_scg(stuNo, couno, grade);
}
else;
}
//输入学生成绩
System.out.println("input student number to clac the grade or all");
String choice =input.next();
if(choice.equals("all"))
{
String[] stunos = db.avrg_all();
for(int j =0; j<stunos.length;j++)
{
System.out.println("stuNo"+stunos[j]+" avrg_grade is "+db.avrg_grade(stunos[j]));
}
}
else
{
System.out.println("stuNo"+choice+" avrg_grade is "+db.avrg_grade(choice));
}
}
}
功能实现
package test8;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class DBConnection {
String driver = "com.mysql.jdbc.Driver";
String url= "jdbc:mysql://localhost:3306/scores";
String user = "root";
String password = "123456";
public Connection coon;
public DBConnection() {
try {
Class.forName(driver);// 加载驱动程序
coon = (Connection) DriverManager.getConnection(url, user, password);// 连续数据库
if(!coon.isClosed())
System.out.println("Succeeded connecting to the Database!");
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
this.coon.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void add_student(String stuNo,String stuName,String stuSex, String stuBirthdate){
String sql="insert into student (stuNo,stuName,stuSex,stuBirthdate) values (?,?,?,?)";
try{
PreparedStatement preStmt=(PreparedStatement)this.coon.prepareStatement(sql);
preStmt.setString(1, stuNo);
preStmt.setString(2, stuName);
preStmt.setString(3, stuSex);
preStmt.setString(4, stuBirthdate);
preStmt.executeUpdate();
System.out.println("插入数据成功!");
preStmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void add_course(String CouNo,String CouName,String CouGrade){
String sql="insert into course (couNo,couName,couGrade) values (?,?,?)";
try{
PreparedStatement preStmt=(PreparedStatement)this.coon.prepareStatement(sql);
preStmt.setString(1, CouNo);
preStmt.setString(2, CouName);
preStmt.setString(3, CouGrade);
preStmt.executeUpdate();
System.out.println("插入数据成功!");
preStmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void add_scg(String stuNo,String CouNo,String grade){
String sql="insert into scg (stuNo,couNo,grade) values (?,?,?)";
try{
PreparedStatement preStmt=(PreparedStatement)this.coon.prepareStatement(sql);
preStmt.setString(1, stuNo);
preStmt.setString(2, CouNo);
preStmt.setString(3, grade);
preStmt.executeUpdate();
System.out.println("插入数据成功!");
preStmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void select_course(){
String sql="select * from course";
try{
Statement stmt=(Statement)this.coon.createStatement();
ResultSet rs=(ResultSet)stmt.executeQuery(sql);
String couNo = null;
String couName = null;
String couGrade = null;
boolean isFirst =true;
while(rs.next()){
couNo = rs.getString("couNo");
couName = rs.getString("couName");
couGrade = rs.getString("couGrade");
if(isFirst)
System.out.println(couNo + "\t" + couName+ "\t" +"\t" + couGrade);
else
System.out.println(couNo + "\t" + couName+ "\t" + couGrade);
isFirst = false;
}
System.out.println("--------------------------------");
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void select_student(){
String sql="select * from student";
try{
Statement stmt=(Statement)this.coon.createStatement();
ResultSet rs=(ResultSet)stmt.executeQuery(sql);
String stuNo = null;
String stuName = null;
String stuSex = null;
String stuBirthDate = null;
System.out.println("--------------------------------");
while(rs.next()){
stuNo = rs.getString("stuNo");
stuName = rs.getString("stuName");
stuSex = rs.getString("stuSex");
stuBirthDate = rs.getString("stuBirthDate");
System.out.println(stuNo + "\t" + stuName+ "\t"+stuSex+ "\t"+ stuBirthDate);
}
System.out.println("--------------------------------");
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
public boolean select_stuno(String stuNo){
String sql="select * from student where stuNo = "+ stuNo;
//String sql="select * from student where stuNo = 2016110201";
boolean flag;
try{
Statement stmt=(Statement)this.coon.createStatement();
ResultSet rs=(ResultSet)stmt.executeQuery(sql);
flag = rs.next() ;
stmt.close();
return flag ;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public void select_couname(){
String sql="select couName from course ";
//String sql="select * from student where stuNo = 2016110201";
try{
Statement stmt=(Statement)this.coon.createStatement();
ResultSet rs=(ResultSet)stmt.executeQuery(sql);
while(rs.next()){
System.out.print(rs.getString(1)+" ");
}
stmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
public boolean select_grade(String stuNo,String couName){
String sql="select * from select_all where stuNo = "+ stuNo +" and couName = "+"\'"+couName+"\'";
boolean flag;
try{
Statement stmt=(Statement)this.coon.createStatement();
ResultSet rs=(ResultSet)stmt.executeQuery(sql);
flag = rs.next() ;
stmt.close();
return flag ;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public String couname_to_couno(String couName){
String couNo = null;
String sql="select * from course";
try{
Statement stmt=(Statement)this.coon.createStatement();
ResultSet rs=(ResultSet)stmt.executeQuery(sql);
while(rs.next()){
if(rs.getString("couName").equals(couName))
couNo = rs.getString("couNo");
}
stmt.close();
return couNo;
}catch(Exception e){
e.printStackTrace();
return couNo;
}
}
// 更改数据
public void update_scg(String stuNo,String couNo,String grade){
String sql="update scg set grade=? where stuNo=? and couNo=?";
try{
PreparedStatement prestmt=(PreparedStatement)this.coon.prepareStatement(sql);
prestmt.setString(1, grade);
prestmt.setString(2,stuNo);
prestmt.setString(3,couNo);
prestmt.executeUpdate();
System.out.println("更改数据成功!");
prestmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
public float avrg_grade(String stuNo)
{
String sql="select * from select_all";
int grade = 0;
int cougrade =0;
try{
Statement stmt=(Statement)this.coon.createStatement();
ResultSet rs=(ResultSet)stmt.executeQuery(sql);
while(rs.next()){
if(rs.getString("stuNo").equals(stuNo))
{
grade += rs.getInt(3)*rs.getInt(4);
cougrade += rs.getInt(4);
}
}
stmt.close();
return (float) (grade*1.0/cougrade*1.0);
}catch(Exception e){
e.printStackTrace();
return (float) 0;
}
}
public String[] avrg_all( )
{
int i =0;
String[] stuNos = null;
String sql="select distinct stuNo from select_all";
try{
Statement stmt=(Statement)this.coon.createStatement();
ResultSet rs=(ResultSet)stmt.executeQuery(sql);
rs.last();
stuNos = new String[rs.getRow()];
rs.beforeFirst();;
while(rs.next()){
stuNos[i] = rs.getString(1);
i++;
}
stmt.close();
return stuNos;
}catch(Exception e){
e.printStackTrace();
return stuNos;
}
}
// 删除数据
public void del(String name){
String sql="delete from usrInfo where username=?";
try{
PreparedStatement prestmt=(PreparedStatement)this.coon.prepareStatement(sql);
prestmt.setString(1, name);
prestmt.executeUpdate();
System.out.println("删除数据成功!");
prestmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
关于视图这个概念有了一个更直观的认识,首先它是一个“虚表”,做数据查询可以提高效率,且安全性也会有所保障,合理的使用视图可以减少代码中sql的的使用。但是仍然还是有许多问题,例如当我将三个表连接起来之后我想查找某个具体的人的信息,在代码中使用SQL的where语句就不太方便。