java数据库连接及crud操作

上传人:第*** 文档编号:55890224 上传时间:2018-10-07 格式:PDF 页数:16 大小:425.61KB
返回 下载 相关 举报
java数据库连接及crud操作_第1页
第1页 / 共16页
java数据库连接及crud操作_第2页
第2页 / 共16页
java数据库连接及crud操作_第3页
第3页 / 共16页
java数据库连接及crud操作_第4页
第4页 / 共16页
java数据库连接及crud操作_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《java数据库连接及crud操作》由会员分享,可在线阅读,更多相关《java数据库连接及crud操作(16页珍藏版)》请在金锄头文库上搜索。

1、一、学习内容1.字符串的处理2.流的概念,Java 的字节流与字符流3.文件的读写4.字符集与编码 (UTF-8, GB2312/GBK, BIG5, ASCII)5.数据库的连接及 CRUD 操作二、任务1.创建学生类 Student2.请从抽象类 AbstractStudentRepository(下载:AbstractStudentRepository 和IStudentDB 源代码)中继承一个类 StudentRepository,实现方法 loadStudents()和saveStudents(), 要求能够支持对 students_UTF8.txt(下载:UTF-8 编码的学生数据

2、文件)和students_GB2312.txt(下载:GB2312编码的学生数据文件)等不同编码的文件的操作 (提示:参考 FileInputStream/FileOutputStream,InputStreamReader/OutputStreamWriter, BufferedReader/BufferedWriter 等类的 API 文档):3.请使用接口 IStudentDB(下载:AbstractStudentRepository 和 IStudentDB 源代码)实现类StudentDBMysql, 用于处理 Student 类在 Mysql 数据库的操作,在类中添加方法creat

3、eStudentTable()用于创建 students 表格:数据库类型: mysql (下载 Mysql 驱动:Mysql JDBC 驱动)主机地址:210.32.80.105数据库名:本人学号用户名:本人学号密码:身份证后 6 位数据库管理地址:http:/210.32.80.105/phpmyadmin4.创建类 TestStudents:测试从文件中读取学生数据; 向文件写入学生数据; 连接数据库;创建 student 表;向 student 中插入文件中的全部学生数据;在数据库中按自己的学号查询数据;从数据库中删除自己学号的数据。5.请仿照已实现的 StudentDBMysql 使

4、用接口 IStudentDB 实现类 StudentDBAccess,实现 Access 数据库连接的操作。(下载 access 数据库:Access 数据库文件用户名、密码均为空)三、内容与要求1.学生数据文件中的学生数据格式如下:尹克平;0012031102;英语(师范);外国语学院谢惠良;0112023302;市场营销;经贸管理学院胡康康;0112032306;初等教育(英语方向)(师范);教师教育学院程双喜;0112033105;外贸英语;外国语学院何勇;0112041114;数学与应用数学(师范);数学与信息工程学院即每个学生数据占一行,数据中每个字段以“;”分隔,对应字段为姓名(x

5、m)、学号(xh)、专业(zy)、学院(xy),其中学号是学生的惟一标识,不存在重复学号的两位学生。请按字段要求实现学生类 Student。由于学号作为学生的惟一标识,请在 Student 类中重写方法hashcode()和 equals(Object obj)这两个方法。(为了实现的需要,只要学号相同的两个Student 实例将会视为相等)2.转换学生数据字段,将专业名与是否师范分开,即英语(师范)转换为英语;true,市场营销转换为市场营销;false3.修改学生类 Student:增加变量“是否师范(sfsf)”;根据我们实验的要求请实现一个构造方法 Student(String str

6、),它可以根据提供的文件中每行学生的数据来创建对应学生实例。(如:“尹克平;0012031102;英语(师范);外国语学院“)。4.对于 saveStudents()方法,需要实现一个 Comparator接口。然后在该方法中对所有学生以学号进行排序。提示:查看 Set 和 Arrays 的 API 文档。将所有学生数据写回文件,内容如下:尹克平;0003121102;英语;true;外国语学院谢惠良;0102123302;市场营销;false;经贸管理学院胡康康;0103122306;初等教育(英语方向);true;教师教育学院程双喜;0103123105;外贸英语;false;外国语学院何

7、勇;0104121114;数学与应用数学;true;数学与信息工程学院5.在上一步实现的基础上,请修改 Student 类的构造方法,要求其能根据参数 str 中的”;”字符的个数来创建正确的 Student 对象。即:a.若str=”尹克平;0012031102;英语(师范);外国语学院”则 Student 实例中的 xm=”尹克平”, xh=”0012031102”, zy=”英语”, sfsf=true, xy=”外国语学院”b.若str=”尹克平;0012031102;英语;false外国语学院”则 Student 实例中的 xm=”尹克平”, xh=”0012031102”, zy=

8、”英语”, sfsf=true, xy=”外国语学院”c.若str=”0012031102”则 Student 实例中的 xh=”0012031102”, 其它变量全为 Java 的对应默认值6.在数据库中创建一个 student 表,包含字段:姓名(xm)、学号(xh)、专业(zy)、是否师范(sfsf)、学院(xy),sql 语句如下:Mysql 数据库:CREATE TABLE student (xm VARCHAR( 24 ),xh VARCHAR( 24 ),zyVARCHAR( 24 ),sfsf TINYINT( 1 ),xy VARCHAR( 24 ),PRIMARY KEY

9、( xh)CHARACTER SET utf8Access 数据库:CREATETABLEstudent(xmVARCHAR(24),xhVARCHAR(24),zyVARCHAR(24),sfsf BIT,xy VARCHAR(24),primary key(xh)7.在表中查找出学号为自己学号的数据的 sql 语句:SELECT * FROM student WHERE xh=学号8.将自己学号的数据删除的 sql 语句:DELETE FROM student WHERE xh=学号9.在数据库中添加的数据的 sql 语句:INSERT INTO student (xm, xh, zy,

10、sfsf, xy) VALUES (“尹克平”, “0003121102”,“英语”, “true”, “外国语学院”)10. 更新学生数据的代码:UPDATE student SET zy = 新专业名, sfsf = 0 或 1 WHERE xh = 我的学号四、实验要求1.使用 Eclipse 动手编程,需要相关的代码截图和运行结果截图2.整理上机结果和体会3.完成实验报告五、实验过程 1.创建 student 类packagepackage streamJdbcPac;publicpublic classclass Student privateprivate String xm,xh

11、,zy,xy;privateprivate booleanboolean ifSF=falsefalse;publicpublic booleanboolean isIfSF() returnreturn ifSF;publicpublic voidvoid setIfSF(booleanboolean ifSF) thisthis.ifSF = ifSF;publicpublic String getXh() returnreturn xh;publicpublic voidvoid setXh(String xh) thisthis.xh = xh;publicpublic String

12、getXm() returnreturn xm;publicpublic voidvoid setXm(String xm) thisthis.xm = xm;publicpublic String getXy() returnreturn xy;publicpublic voidvoid setXy(String xy) thisthis.xy = xy;publicpublic String getZy() returnreturn zy;publicpublic voidvoid setZy(String zy) thisthis.zy = zy;publicpublic Student

13、(String line)String info=line.split(“;“);/用;分割字符串ifif(info.length=4)thisthis.xm=info0;thisthis.xh=info1;thisthis.zy=info2;thisthis.xy=info3;ifif(thisthis.zy.endsWith(“(师范)“)thisthis.ifSF=truetrue;intint index=thisthis.zy.indexOf(“(师范)“);thisthis.zy.substring(0, index);elseelsethrowthrow newnew Illeg

14、alArgumentException();publicpublic booleanboolean equals(Object obj) Student s=(Student)obj;ifif (pareTo(s.xh)=0)returnreturn truetrue;returnreturn falsefalse;publicpublic intint hashCode() returnreturn thisthis.xh.hashCode();publicpublic String toString() returnreturn thisthis.xm+“ “+thisthis.xh+“

15、“+thisthis.zy+“ “+thisthis.ifSF+“ “+thisthis.xy;2. 实现类 StudentRepositorypackagepackage streamJdbcPac;importimport java.io.*;importimport java.util.*;publicpublic classclass StudentRepository extendsextends AbstractStudentRepository publicpublic StudentRepository(String dataFilePath, String charset)

16、supersuper(dataFilePath, charset);/ TODOTODO 自动生成构造函数存根Overridepublicpublic voidvoid loadStudents() trytryFileInputStream fis=newnew FileInputStream(thisthis.dataFilePath);/文件字节流InputStreamReader isr=newnew InputStreamReader(fis,charset);/指定编码的字符流BufferedReader br=newnew BufferedReader(isr);/缓冲流String line;ifif(set=nullnull)set=newnew HashSet();elseelseset.clear();/清空数

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号