Java程序设计 实验七 数据库应用

实验目的

	了解数据库系统、关系模型、客户-服务器模式等基本概念,掌握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
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值