实验目的
了解数据库系统、关系模型、客户-服务器模式等基本概念,掌握SQL的数据定义、数据操纵和数据查询等语句的语法;了解JDBC各种数据库驱动程序类型,熟悉JDBC提供的接口和类,掌握指定驱动类型、连接数据库、执行SQL语句、处理结果集等操作方法。
实验内容
数据库应用程序
1)首先在数据库应用程序中创建数据库Studentinfo,按照下表的结构在数据库中建立"student"表。
字段名 |Java数据类型 |宽度 |SQL数据类型
字段名 | Java数据类型 | 宽度 | SQL数据类型 |
---|---|---|---|
Name | String | 10 | Char(10) |
Sex | String | 2 | Char(2) |
Age | Int | 3 | Integer |
2)编写程序,a)向"student"表中填入若干数据记录;b)在"student"表中分别查询所有记录以及满足条件"age>18"的记录。c)编程创建学生成绩表,并进行数据插入、修改、删除、查询和成绩统计等操作。
注:不限定数据库类型,
package sql;
import java.sql.*;
import java.util.Scanner;
public class tt {
public static Statement statement=null;
public static String input=null;
public static ResultSetMetaData rsm;//只用于输出列表名
public static ResultSet rs;
public static String getscore_for_studentscore="select %s(%s) from studentscore;";
public static String insert_for_studentscore="insert into studentscore values(%s,'%s',%d,%d,%d);";
public static String delete_for_studentscore="delete from studentscore where 学号='%s';";//学号统一为数字,输入时不需要''
public static String check_for_studentscore_count="select count(*) from studentscore where %s%s%s;";//要满足任意输入,所以对于姓名部分,要自带''
public static String check_for_studentscore="select * from studentscore where %s%s%s;";//要满足任意输入,所以对于姓名部分,要自带''
public static Scanner scan=new Scanner(System.in);
public static int data_Exist(String type,String symbol,String number) throws SQLException//依次为属性、符号、参数
{
rs=statement.executeQuery(String.format(check_for_studentscore_count,type,symbol,number));
rs.next();
return rs.getInt(1);
}
public static void count_Three(String labelString,String type) throws SQLException//依次为标签,类型(max、avg、min)
{
System.out.print(labelString);
for(int i=3;i<=5;i++)
{
rs=statement.executeQuery(String.format(getscore_for_studentscore,type,rsm.getColumnName(i)));
rs.next();
System.out.print(String.format("%9d", rs.getInt(1))+" ");
}
System.out.println();
}
public static void show_columnName(int begin) throws SQLException//输出表头
{
for (int i = begin; i <= rsm.getColumnCount(); i++) //从begin开始
{
System.out.print(String.format("%-10s", rsm.getColumnName(i))+"\t");
}
System.out.println();
}
public static void show() throws SQLException
{
show_columnName(1);
int count=0