数据库课程设计(java版)

更新时间:2023-08-30 14:17:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

计算机科学与技术学院课程设计成绩单

优秀:90分~100分 良好:80分~89分 中等:70~79分 及格:60~69分 不及格0分~59分

计算机科学与技术学院

课 程

课程名称:专 业:班 级:学 号:姓 名:指导老师:设 计 报 告

数据库系统原理

java学生成绩管理系统课程设计报告以及代码共享

一、课程题目

设计一个简易的电子书店管理系统。用于记录书店书本的信息以供管理员管理。 (1) 记录书本的基本信息,如编号、名称、类型、作者等 (2) 记录各个读者的信息。

(3) 实现一些管理的基本功能,如:书本信息的查询、插入新的书本信息、更改 以及删除书本信息等功能。

(4) 能够连接数据库,从中获取信息。

二、题目分析与设计

1. 使用的开发环境。

我使用的开发环境是Eclipse,数据库是mysql。

2. 根据课题画出数据流程图。

电子书店管理,实现添加书本记录、修改书本记录,删除及更改记录的功能。

3. 根据课题画出E-R图。

4. 数据库中各个表的结构。

此次课题使用了三张表: 图书表(图书编号(主码),图书名,类型,作者,数目,价格); 读者表(读者编号(主码),读者名,年龄,性别,电话,地址); 订单表(订单编号(主码),图书编号,读者编号,数目,需付金额)。 5. 界面设计过程和组件的布局策略。

由多个界面组成,通过点击界面的按钮来响应下一个事务,并对操作的 失误有友好的提醒界面功能。 6. 论述程序逻辑的实现

用户输入数据后,整理数据,通过JDBC连接数据库进行各种操作。

三、测试分析

1. 登陆界面

进入登陆界面输入用户名:何耀武 密码:1234 点击确定就可以进入主界 面然后根据自己的需要点击界面上的操作按钮,从而进入下一个界面来实 现要完成的操作

主要代码如下:

jButton1.addActionListener(new ActionListener() {//确定按钮所产生的事务

public void actionPerformed(ActionEvent e) { try { if(e.getSource()==jButton1){ String S1="***",S2="1234";

if((jTextField1.getText()).equals(S1)&&jPasswordField1用户名密码比

{

dispose();

NewJFrame1 main_ui=new NewJFrame1(); main_ui.setVisible(true);

} else { jLabel4.setText("你输入的密码或用户名有误,请再输入" ); jLabel4.setVisible(true);

jPasswordField1.setText(null);//输入错误提示

}

}

} catch (Exception f) { f.printStackTrace(); } }

});

jButton1.addActionListener(new ActionListener() {//进入下个界面 public void actionPerformed(ActionEvent e) { if(e.getSource()==jButton1){ dispose(); NewJFrame2 N2=new NewJFrame2(); N2.setVisible(true);//出现主界面

} }

});

jButton3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub

if(e.getSource()==jButton3){ dispose(); NewJFrame6 N6=new NewJFrame6();

N6.setVisible(true);//出现主界面

});

}

}

2. 测试结果 (1)查询书本信息

进入查询界面后点击要查询的对象,然后在方框内输入编号点击确 定按钮从而得到所要查询的信息。

主要查询代码:

jButton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { String S=null;

if(jRadioButton1.isSelected()) { jTextField2.setText(null); if(e.getSource()==jButton1) S=jTextField1.getText();

String driver="com.mysql.jdbc.Driver";//驱动程序 String url = "jdbc:mysql://localhost:3306/sellbook"; String user="root";//登录名 String password="118157439";//密码 Connection Conn = null;//l连接数据库 try{

Class.forName(driver); Conn=DriverManager.getConnection(url,user,password); String query = "select * from books where Bno='"+S+"';"; PreparedStatement ps=Conn.prepareStatement(query); ResultSet result = ps.executeQuery(); NewJFrame3 n3=new NewJFrame3(); while (result.next()) { n3.jTextField1.setText(result.getString("Bno")); n3.jTextField2.setText(result.getString("Bname")); n3.jTextField3.setText(result.getString("Btype")); n3.jTextField4.setText(result.getString("Bauthor")); n3.jTextField5.setText(result.getString("Bprice")); n3.jTextField6.setText(result.getString("Bnumber")); ");

}//查询所得数据

if(n3.jTextField1.getText().length()==0)

{ IOS_ron N1=new IOS_ron(null); N1.setVisible(true); }//查询编号不在,计算机报错

else {

//dispose(); n3.setVisible(true); Conn.close(); } } catch(SQLException f) {

//JOptionPane.showMessageDialog(null, "输入有误

}

f.printStackTrace(); }

catch(Exception g){ g.printStackTrace(); }

if(jRadioButton2.isSelected())//点击第二个按钮的操作实现

{ jTextField1.setText(null); if(e.getSource()==jButton1) S=jTextField2.getText();

String driver="com.mysql.jdbc.Driver";

String url = "jdbc:mysql://localhost:3306/sellbook"; String user="root";

String password="528157439"; Connection Conn = null; try{

Class.forName(driver);//驱动加载

Conn=DriverManager.getConnection(url,user,password);

");

String query = "select * from orders where Ono='"+S+"';"; PreparedStatement ps=Conn.prepareStatement(query); ResultSet result = ps.executeQuery(); NewJFrame4 n4=new NewJFrame4();//选择数据 while (result.next()) { n4.jTextField1.setText(result.getString("Ono")); n4.jTextField2.setText(result.getString("Bno")); n4.jTextField3.setText(result.getString("Rno")); n4.jTextField4.setText(result.getString("Onumber")); n4.jTextField5.setText(result.getString("Opay"));

}//查询所得数据

if(n4.jTextField1.getText().length()==0)

{ IOS_ron N1=new IOS_ron(null); N1.setVisible(true);

}//报错处理

else {

n4.setVisible(true); Conn.close(); }} catch(SQLException f) {

JOptionPane.showMessageDialog(null, "输入有误

} catch (Exception f) {

f.printStackTrace(); } } }});

(2)插入书本信息

点击插入按钮进入插入界面,向对话框中输入信息,如: 009 大学音乐 音乐 30 李红 10

将这一组数据插入表中,然后点击确定按钮,会弹出对话框插入成功, 表明数据插入成功。 主要插入代码:

jButton2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(jTextField1.getText().length()==0){

jLabel8.setText("编号不能为空" );//判断插入编号不能为空 jLabel8.setVisible(true); }

if(jTextField1.getText().length()!=0){ String driver="com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/sellbook"; String user="root"; String password="118157439";

Connection Conn = null;//连接数据库

try{

Class.forName(driver);//加载驱动

Conn=DriverManager.getConnection(url,user,password);

String query = "insert into books values(?,?,?,?,?,?)"; //连接数据库,向其中插入数据

PreparedStatement ps=Conn.prepareStatement(query); ps.setString(1, jTextField1.getText()); ps.setString(2, jTextField2.getText()); ps.setString(3, jTextField3.getText()); ps.setString(4, jTextField4.getText()); ps.setString(5, jTextField5.getText()); ps.setString(6, jTextField6.getText());

ps.executeUpdate();//插入成功 Conn.close();//关闭连接

}

catch(ClassNotFoundException f)

});

}

}

{ System.out.println(f.getMessage()); }

catch(SQLException g){ System.out.println(g.getMessage()); }

add_suc a=new add_suc(null); a.setVisible(true);

(3)修改书本信息

在主界面点击要修改图书信息,输入要修改的图书信息,进入图书修改界面, 在对话框中修改所要修改的信息,如: 价格:由30修改为46,数目:由10修改为4.

之后点击确定按钮,弹出对话框更改成功,即完成更改。

修改代码为:

jButton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { if(e.getSource()==jButton1){

String driver="com.mysql.jdbc.Driver";//加载驱动 String url = "jdbc:mysql://localhost:3306/sellbook"; String user="root";//用户名 String password="118157439";//密码 Connection Conn = null;//连接数据库

try{

Class.forName(driver);

Conn=DriverManager.getConnection(url,user,password); String query = "delete from books where Bno='"+jTextField1.getText()+"'"; PreparedStatement ps=Conn.prepareStatement(query); ps.executeUpdate();

String query1 = "insert into books values(?,?,?,?,?,?)";//插入 PreparedStatement ps2=Conn.prepareStatement(query1); ps2.setString(1, jTextField1.getText()); ps2.setString(2, jTextField2.getText()); ps2.setString(3, jTextField3.getText()); ps2.setString(4, jTextField5.getText()); ps2.setString(5, jTextField4.getText()); ps2.setString(6, jTextField6.getText());

});

} }

ps2.executeUpdate();//更改成功 Conn.close();//关闭连接

}

catch(ClassNotFoundException f) { System.out.println(f.getMessage()); }

catch(SQLException g){ System.out.println(g.getMessage()); }

alter_suc a=new alter_suc(null); a.setVisible(true);//弹出更改成功窗口

(4)删除图书信息

进入删除图书界面,输入图书编号,点击确定按钮,当弹出删除成功 对话窗口,即成功删除,这样实现删除要删除的图书信息。

删除代码为:

jButton1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { String S=jTextField1.getText(); try { String driver="com.mysql.jdbc.Driver";//加载驱动 String url = "jdbc:mysql://localhost:3306/sellbook"; String user="root";//用户名

String password="118157439";//密码 Connection Conn = null;//连接数据库 Class.forName(driver); Conn=DriverManager.getConnection(url,user,password); String query = "delete from books where Bno='"+S+"'"; //删除图书信息 PreparedStatement ps=Conn.prepareStatement(query); ps.executeUpdate();//删除成功 Conn.close(); delete_suc d=new delete_suc(null); d.setVisible(true);//弹出删除成功的对话框 }catch(ClassNotFoundException f) {

System.out.println("Message="+f.getMessage()); } catch(SQLException g){ g.printStackTrace(); }

}});

四、总结

不足之处:

(1)对于这个课题的实践,界面做的过多,应该设计为一个界面上完成各个操 作,弹出窗口过多,不易观察,过于复杂。

(2)所做的操作由于技术有限,能够实现的功能太少,要加强软件的实用性, 操作不能太单一。

(3)程序写的不是很完善,存在许多漏洞,需要通过以后的学习不断的加一 巩固和改善。 可取之处:

(1)基本上完成了题目的要求,实现了查询,插入,更新,删除数据的操作, 可以满足要求。

(2)完成了对数据完整性约束的要求,但用户输入错误时会有一个比较好的的 弹出框提醒用户,比较人性化。 实践心得:

通过此次为期三周的数据库实践课题,确实学到了不少的东西,通过连接 数据库,从中获得数据,体现数据库数据结构化,数据独立,易于扩充 的特点,对JAVA这门以面向对象为主体的操作语言有了进一步的加深 加熟了对这门语言的认知。这次的实践对自己以后的学习有很积极的指导 作用。

本文来源:https://www.bwwdw.com/article/lsti.html

Top