/** 文件:InfoModel.java
说明:存储学生信息的类 (数据库版)
**/
import java.io.*;
import java.util.*;
import java.text.*;
import java.sql.*;
public class InfoModelDB
{
String sno=null;
String name=null;
String sex=null;
String department=null;
String birthday=null;
AccessDB_Bean myData;
public InfoModelDB() throws SQLException,ClassNotFoundException
{ this.myData=new AccessDB_Bean(); //建立访问数据库的类实例
}
public InfoModelDB(String sno,String name,String sex,String birthday,String department) //构造方法
throws SQLException,ClassNotFoundException
{
this.sno=sno;
this.name=name;
this.sex=sex;
this.birthday=birthday;
this.department=department;
this.myData=new AccessDB_Bean();
}
public void Query() throws SQLException //显示学生记录的方法
{
String sqlst="select * from student";
ResultSet rc=myData.executeSelect(sqlst);
int count=0;
while(rc.next())
{ count++;
System.out.println("------记录号:"+(count)+"------");
System.out.println("学 号:"+rc.getString("sno"));
System.out.println("姓 名:"+rc.getString("name"));
System.out.println("性 别:"+rc.getString("sex"));
System.out.println("出生年月:"+rc.getDate("birthday"));
System.out.println("院系名称:"+rc.getString("department"));
}
if(count==0) System.out.println("目前没有记录");
}
public void Insert(BufferedReader readStream) throws SQLException //插入新的学生记录(键盘版本)
{
System.out.println("请输入学生信息");
try
{System.out.print("请输入学号:");
sno=readStream.readLine();
System.out.print("请输入姓名:");
name=readStream.readLine();
System.out.print("请输入性别:");
sex=readStream.readLine();
System.out.print("请输入出生日期(年-月-日):");
birthday=readStream.readLine();
System.out.print("请输入院系名称:");
department=readStream.readLine();
}
catch(IOException e)
{e.printStackTrace();}
String sqlst="insert into student values('"+sno+"','"+name+
"','"+sex+"',"+birthday+",'"+department+"')";
myData.executeDML(sqlst);
System.out.println("成功输入");
}
public void Delete(BufferedReader readStream) //删除一条学生记录(键盘版本)
throws SQLException,IOException
{
System.out.println("请输入想删除的学号:");
String sno=readStream.readLine();
String sqlst="delete from student where sno='"+sno+"'";
myData.executeDML(sqlst);
System.out.println("删除成功");
}
public void Update(BufferedReader readStream) throws SQLException //修改学生记录(键盘版本)
{ System.out.println("请输入想修改的学号:");
try{
sno = readStream.readLine();
}
catch(Exception e)
{
e.printStackTrace();
}
String sqlst="SELECT * from student where sno='"+sno+"'";
ResultSet rc=myData.executeSelect(sqlst);
if(rc.next())
{
System.out.println("请输入想修改的学生信息");
try
{System.out.println("请输入新的数据:");
System.out.print("请输入姓名:");
name=readStream.readLine();
System.out.print("请输入性别:");
sex=readStream.readLine();
System.out.print("请输入出生日期(年-月-日):");
birthday=readStream.readLine();
System.out.print("请输入院系名称:");
department=readStream.readLine();
}
catch(IOException e)
{e.printStackTrace();}
sqlst="update student set name='"+name+"',"+
"sex='"+sex+"',birthday='"+birthday+"',"+
"department='"+department+"' where sno='"+sno+"'";
myData.executeDML(sqlst);
System.out.println("数据更新成功");
}
else {System.out.println("找不到此学号!"); }
}
public void Load(String filename) //装入学生记录的方法
throws IOException,SQLException,FileNotFoundException
{ String in;
BufferedReader readStream=new BufferedReader(
new FileReader(filename));
while ((in=readStream.readLine())!=null)
{
StringTokenizer st=new StringTokenizer(in,"|");
sno=st.nextToken();
name=st.nextToken();
sex=st.nextToken();
birthday=st.nextToken();
department=st.nextToken();
String sqlst="insert into student values('"+sno+"','"+name+
"','"+sex+"',"+birthday+",'"+department+"')";
myData.executeDML(sqlst);
}
System.out.println("载入成功!");
}
public void SaveTo(String filename) //保存处理结果
throws IOException,SQLException,FileNotFoundException
{
BufferedWriter writeStream=new BufferedWriter(
new FileWriter(filename));
String sqlst="select * from student";
ResultSet rc=myData.executeSelect(sqlst);
while(rc.next())
{
writeStream.write(rc.getString("Sno"));
writeStream.write("|");
writeStream.write(rc.getString("name"));
writeStream.write("|");
writeStream.write(rc.getString("Sex"));
writeStream.write("|");
SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd");
String myDate=fmt.format(rc.getDate("birthday"));
writeStream.write(myDate);
writeStream.write("|");
writeStream.write(rc.getString("department"));
writeStream.newLine(); //即写入一个回车换行符
}
writeStream.close();
System.out.println("数据导出成功");
}
}
- 1
- 2
前往页