java可视化界面操作数据库编程

更新时间:2023-12-21 16:30:01 阅读量: 教育文库 文档下载

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

.

天津理工大学实验报告

学院(系)名称:计算机与通信工程学院 姓名 班级 晴小天 学号 201356xxxx 专业 计算机科学与技术 2013级2班 实验项目 实验五:小型信息管理系统设计开发 课程代码 实验地点 成绩 0667056 7-219 课程名称 实验时间 批改意见 Java语言程序设计 2015年11月12日、20日、25日第3、4节 教师签字: 实验内容: 1. 参考实验资料,自拟题目,设计制作一个小型信息管理系统。 实验目的: 1.理解并掌握Java数据库访问技术。 2.掌握对数据进行增加、删除、修改、查询。 实验要求: 1.按照要求编制程序; 3.为增加程序可读性,请在程序中对类成员进行适当注释说明; 4.整理上机步骤,总结经验和体会; 5.认真完成并按时提交实验报告。 【实验过程记录(源程序、测试用例、测试结果及心得体会等)】

.

.

一、 MIS系统

系统名称:学生信息管理系统 操作系统: Window8.1

Java环境; jdk 1.7.0_21 、jre 1.7.0_21 集成开发环境:MyEclipse Professional 2013

数据库软件版本:SQL Server 2010 连接驱动:jdbc-odbc

二、 系统主要功能

本项目主要实现了java界面的登录功能,以及登陆后的增删改查功能,主要是对student表实现增加一条学生信息、删除一条学生信息记录、更新学生信息表,以及查询学生信息表的数据

学生信息管理系统 登陆模块 功能模块

添加 删除 更新 查找 三、数据库结构

1. 数据库名称:SchoolDB

2. 数据表:student表具体字段信息如下表: 字段名 类型 长度 小数点 不是null sname(主键) varchar 20 0 √ sname varchar 20 0 √ sdept varchar 20 0 √ sgrade varchar 20 0 √ saddress varchar 20 0 √ sname, sname, sdept, sgrade、saddress分别保存学生的姓名、专业、学院、成绩、家庭住址等学生基本信息,其中sname为主键且不为空。

四、关键功能界面截图

1. 登陆界面

用户输入数据库中存在的用户名和密码后,便可登陆成功,否则提示密码错误

① 正确的用户名,错误的密码

.

.

正确的用户名正确的密码后进入学生信息管理系统:

2. 添加功能:输入界面中的所有数据之后,点击添加,即可向数据库中加入一条学生记录

.

.

3.删除功能:输入姓名,即可把数据库中对应姓名的一条学生信息删除

4.更新功能:输入要修改学生的信息,即可修改该学生在数据库中的记录

.

.

5.查找功能:点击查找可以查询数据库中的所有学生信息,每页9条记录,其余的学生信息的点击下一页,可以查看,还可以点击上一页,来切换数据

点击下一页:

.

.

五、关键代码分析

DBConnetion用于连接数据库: package edu;

import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;

public class DBConnection {

public static Connection getConnection(){ Connection conn = null;

String driver = \ String url = \ String username = \ String password = \

try {

Class.forName(driver);

.

.

conn = DriverManager.getConnection(url,username,password); } catch (ClassNotFoundException e) { e.printStackTrace();

} catch (SQLException e) { e.printStackTrace(); }

return conn; } }

登陆的窗体类LoginFrame、JFrame: package edu;

import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.SQLException;

import javax.swing.JButton; import javax.swing.JLabel;

import javax.swing.JOptionPane; import javax.swing.JPanel;

import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingUtilities;

public class Jswing implements ActionListener{

private LoginFrame frame; private JLabel lname; private JLabel lpassword; private JTextField tname;

private JPasswordField tpassword; private JButton byes; private JButton bno; private JPanel p1; private JPanel p2; private JPanel p3;

private String userName,password; /**

* 初始化窗口数据

.

.

*/

public void initData() {

frame=new LoginFrame(\登陆\ frame.setResizable(false);

frame.setLayout(new GridLayout(3, 2)); Container c=frame.getContentPane(); lname=new JLabel(\用户名:\

lpassword=new JLabel(\密 码: \ tname=new JTextField(8);

tpassword=new JPasswordField(8); byes=new JButton(\确定\ bno=new JButton(\取消\ p1=new JPanel(); p2=new JPanel(); p3=new JPanel(); p1.add(lname); p1.add(tname); c.add(p1);

p2.add(lpassword); p2.add(tpassword); c.add(p2); p3.add(byes); p3.add(bno); c.add(p3);

frame.setVisible(true);

byes.addActionListener(this); bno.addActionListener(this); userName=null; password=null; }

@Override

public void actionPerformed(ActionEvent e) { if(e.getSource()==bno){ System.exit(1); }else{

if(e.getSource()==byes){

userName=tname.getText().trim();

password=new String(tpassword.getPassword()); boolean result=false;

result=SchoolOperate.login(userName, password); if(result){

frame.setVisible(false);

SwingUtilities.invokeLater(new Runnable() { public void run() {

.

.

SchoolInfoGui application = new SchoolInfoGui();

application.getJFrame().setVisible(true); } }); }else{

String error=null; if(result==false){

error=\用户名或密码错误,请重试!\ }

JOptionPane.showMessageDialog(null, error); } } } }

public static void main(String[] args) { new Jswing().initData(); } }

package edu;

import java.awt.HeadlessException;

import javax.swing.JFrame;

public class LoginFrame extends JFrame{

public LoginFrame(String title) throws HeadlessException { super();

setTitle(title);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(540,200, 280, 200); } }

学生信息管理系统主界面的SchoolInfoGui: package edu;

import java.awt.Rectangle;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List;

.

.

import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel;

import javax.swing.JOptionPane; import javax.swing.JPanel;

import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.SwingUtilities;

public class SchoolInfoGui implements ActionListener {

private JFrame jFrame;

private JPanel jContentPane; private JLabel lblName = null; private JTextField tfName = null; private JLabel lblRegion = null; private JComboBox cbRegion = null; private JLabel lblArea = null; private JTextField tfArea = null; private JLabel lblPopu = null; private JTextField tfPopu = null; private JLabel lblGdp = null; private JTextField tfGDP = null; private JButton btnAdd = null; private JButton btnDel = null; private JButton btnModify = null; private JButton btnQuery = null; private JButton btnUpPage = null; private JButton btnDownPage = null; private JTable table = null; TableModel tm = null; JScrollPane jsp = null;

private JTextField getTfName() { if (tfName == null) {

tfName = new JTextField();

tfName.setBounds(new Rectangle(89, 17, 173, 27)); }

return tfName; }

private JComboBox getCbRegion() { if (cbRegion == null) {

.

.

String[] str = { \请选择\计算机科学与技术\信息安全\网络工程\信息与计算科学\物流工程\软件工程\ cbRegion = new JComboBox(str);

cbRegion.setBounds(new Rectangle(360, 17, 229, 28)); }

return cbRegion; }

private JTextField getTfArea() { if (tfArea == null) {

tfArea = new JTextField();

tfArea.setBounds(new Rectangle(89, 61, 173, 27)); }

return tfArea; }

private JTextField getTfPopu() { if (tfPopu == null) {

tfPopu = new JTextField();

tfPopu.setBounds(new Rectangle(360, 61, 229, 28)); }

return tfPopu; }

private JTextField getTfGDP() { if (tfGDP == null) {

tfGDP = new JTextField();

tfGDP.setBounds(new Rectangle(89, 105, 173, 27)); }

return tfGDP; }

private JButton getBtnAdd() { if (btnAdd == null) { btnAdd = new JButton();

btnAdd.setBounds(new Rectangle(46, 152, 71, 30)); btnAdd.addActionListener(this); btnAdd.setText(\添加\ }

return btnAdd; }

private JButton getBtnUpPage() { if (btnUpPage == null) {

btnUpPage = new JButton(\上一页\

.

.

btnUpPage.addActionListener(this); btnUpPage.setBounds(200, 390, 80, 30); }

return btnUpPage; }

private JButton getBtnDownPage() { if (btnDownPage == null) {

btnDownPage = new JButton(\下一页\ btnDownPage.addActionListener(this); btnDownPage.setBounds(300, 390, 80, 30); }

return btnDownPage; }

private JButton getBtnDel() { if (btnDel == null) { btnDel = new JButton();

btnDel.setBounds(new Rectangle(189, 152, 71, 30)); btnDel.addActionListener(this); btnDel.setText(\删除\ }

return btnDel; }

private JButton getBtnModify() { if (btnModify == null) { btnModify = new JButton();

btnModify.setBounds(new Rectangle(342, 152, 71, 30)); btnModify.addActionListener(this); btnModify.setText(\更新\ }

return btnModify; }

private JButton getBtnQuery() { if (btnQuery == null) { btnQuery = new JButton();

btnQuery.setBounds(new Rectangle(484, 152, 71, 30)); btnQuery.addActionListener(this); btnQuery.setText(\查找\ }

return btnQuery; }

.

.

JFrame getJFrame() { if (jFrame == null) { jFrame = new JFrame();

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setBounds(new Rectangle(450, 250, 610, 454)); jFrame.setContentPane(getJContentPane()); jFrame.setTitle(\学生信息管理系统\ }

return jFrame; }

private JPanel getJContentPane() { if (jContentPane == null) { lblGdp = new JLabel();

lblGdp.setBounds(new Rectangle(15, 106, 63, 27)); lblGdp.setText(\家庭住址\ lblPopu = new JLabel();

lblPopu.setBounds(new Rectangle(285, 61, 63, 27)); lblPopu.setText(\成绩\ lblArea = new JLabel();

lblArea.setBounds(new Rectangle(15, 61, 63, 27)); lblArea.setText(\学院\ lblRegion = new JLabel();

lblRegion.setBounds(new Rectangle(285, 17, 63, 27)); lblRegion.setText(\专业\ lblName = new JLabel();

lblName.setBounds(new Rectangle(15, 17, 63, 27)); lblName.setText(\姓名\ jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(lblName, null);

jContentPane.add(getTfName(), null); jContentPane.add(lblRegion, null); jContentPane.add(getCbRegion(), null); jContentPane.add(lblArea, null);

jContentPane.add(getTfArea(), null); jContentPane.add(lblPopu, null);

jContentPane.add(getTfPopu(), null); jContentPane.add(lblGdp, null);

jContentPane.add(getTfGDP(), null); jContentPane.add(getBtnAdd(), null); jContentPane.add(getBtnDel(), null); jContentPane.add(getBtnModify(), null); jContentPane.add(getBtnQuery(), null); jContentPane.add(getBtnUpPage(), null);

.

.

jContentPane.add(getBtnDownPage(), null); }

return jContentPane; }

public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(\查找\ selectAll();

} else if (e.getActionCommand().equals(\下一页\ selectDownPage();

} else if (e.getActionCommand().equals(\上一页\ selectUpPage();

} else if (e.getActionCommand().equals(\添加\ if (tfName.getText().equals(\

|| cbRegion.getSelectedItem().equals(\请选择\ || tfArea.getText().equals(\ || tfPopu.getText().equals(\ || tfGDP.getText().equals(\

JOptionPane.showMessageDialog(jFrame, \请填写相关信息\ }else {

Student co = new Student(); co.setSname(tfName.getText());

co.setSpro((String) cbRegion.getSelectedItem()); co.setSdept(tfArea.getText()); co.setSgrade(tfPopu.getText()); co.setSaddress(tfGDP.getText());

boolean flag = SchoolOperate.add(co); if (flag) {

JOptionPane.showMessageDialog(jFrame, \添加成功\ } else {

JOptionPane.showMessageDialog(jFrame, \添加失败\ } }

} else if (e.getActionCommand().equals(\删除\ if(!tfName.getText().equals(\ String key = tfName.getText();

boolean flag = SchoolOperate.del(key); if (flag) {

JOptionPane.showMessageDialog(jFrame, \删除成功\ } else {

JOptionPane.showMessageDialog(jFrame, \删除失败\ }

}else {

JOptionPane.showMessageDialog(jFrame, \请填写一个国家名字\ }

.

.

} else if (e.getActionCommand().equals(\更新\ if (!tfName.getText().equals(\

&& !cbRegion.getSelectedItem().equals(\请选择\ && !tfArea.getText().equals(\ && !tfPopu.getText().equals(\ && !tfGDP.getText().equals(\ Student cou = new Student(); cou.setSname(tfName.getText());

cou.setSpro((String)cbRegion.getSelectedItem()); cou.setSdept(tfPopu.getText()); cou.setSgrade(tfArea.getText()); cou.setSaddress(tfGDP.getText());

boolean flag = SchoolOperate.modify(cou); if(flag){

JOptionPane.showMessageDialog(jFrame, \更新成功\ }else {

JOptionPane.showMessageDialog(jFrame, \更新失败\ }

} else {

JOptionPane.showMessageDialog(jFrame, \请填写相应的信息\ } } }

public void setValues(Object[][] obj) { this.tm.setDatavalues(obj); }

public void selectAll() {

String spro=cbRegion.getSelectedItem().toString(); List list = SchoolOperate.queryFirst(); Object[][] datavalues = new Object[list.size()][5]; for (int i = 0; i < list.size(); i++) { Student co = list.get(i);

datavalues[i][0] = co.getSname(); datavalues[i][1] = co.getSpro(); datavalues[i][2] = co.getSdept(); datavalues[i][3] = co.getSgrade(); datavalues[i][4] = co.getSaddress(); }

tm = new TableModel();

this.setValues(datavalues); table = new JTable(tm); table.updateUI();

JScrollPane jsp = new JScrollPane(table);

.

.

jsp.setBounds(0, 200, 600, 182); jContentPane.add(jsp); }

public void selectDownPage() {

List list = SchoolOperate.downPage(); if (list != null) {

Object[][] datavalues = new Object[list.size()][5]; for (int i = 0; i < list.size(); i++) { Student co = list.get(i);

datavalues[i][0] = co.getSname(); datavalues[i][1] = co.getSpro(); datavalues[i][2] = co.getSdept(); datavalues[i][3] = co.getSgrade(); datavalues[i][4] = co.getSaddress(); }

this.setValues(datavalues); table.updateUI(); } else {

JOptionPane.showMessageDialog(jFrame, \已经是最后一页\ } }

public void selectUpPage() {

List list = SchoolOperate.upPage(); if (list != null) {

Object[][] datavalues = new Object[list.size()][5]; for (int i = 0; i < list.size(); i++) { Student co = list.get(i);

datavalues[i][0] = co.getSname(); datavalues[i][1] = co.getSpro(); datavalues[i][2] = co.getSdept(); datavalues[i][3] = co.getSgrade(); datavalues[i][4] = co.getSaddress(); }

this.setValues(datavalues); table.updateUI(); } else {

JOptionPane.showMessageDialog(jFrame, \已经是第一页\ } } }

针对数据库的增删改查操作方法类; package edu;

.

.

import java.sql.Connection;

import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List;

public class SchoolOperate { static int i = 1; static int x = 0;

public static List queryFirst() {

String sql = \ List list = query(sql); return list; }

public static List downPage() { int y = queryCount();

List list = null; int count = 0; // 总页数 if (y % 5 == 0) { count = y / 5; } else {

count = y / 5 + 1; }

if (i < count) {

x = ((++i) - 1) * 5;

String sql = \ + x

+ \\

+ x + \ list = query(sql);

} else if (i >= count) { list = null; }

return list; }

public static boolean login(String userName,String password){ boolean result=false;

.

.

Connection con = DBConnection.getConnection();

String sql=\ PreparedStatement ps = null; ResultSet rs = null; try{

ps=con.prepareStatement(sql); ps.setObject(1, userName); rs=ps.executeQuery(); while(rs.next()){

if(password.equals(rs.getString(\ result=true; } }

}catch (SQLException e) {

// TODO Auto-generated catch block e.printStackTrace(); }finally{ try{

if(rs!=null){ rs.close(); }

}catch(SQLException e){ e.printStackTrace(); }

try {

if(ps!=null){ ps.close(); }

} catch (SQLException e) { e.printStackTrace(); } } try{

if(con!=null){ con.close(); }

}catch(SQLException e){ e.printStackTrace(); }

return result; }

public static int queryCount() { int count = 0;

Connection conn = DBConnection.getConnection();

.

.

String sql = \ Statement stmt = null; ResultSet rs = null; try {

stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { count = rs.getInt(1); }

} catch (SQLException e) { e.printStackTrace(); } finally { try {

if (rs != null) rs.close();

if (stmt != null) stmt.close(); if (conn != null) conn.close();

} catch (SQLException e) { e.printStackTrace(); } }

return count; }

public static List query(String sql) { Connection conn = DBConnection.getConnection(); List list = new ArrayList(); Statement stmt = null; ResultSet rs = null; try {

stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) {

Student cou = new Student(); cou.setSname(rs.getString(1)); cou.setSpro(rs.getString(2)); cou.setSdept(rs.getString(3)); cou.setSgrade(rs.getString(4)); cou.setSaddress(rs.getString(5)); list.add(cou); }

} catch (SQLException e) {

.

.

e.printStackTrace(); } finally { try {

if (rs != null) rs.close();

if (stmt != null) stmt.close(); if (conn != null) conn.close();

} catch (SQLException e) { e.printStackTrace(); } }

return list; }

public static List upPage() { List list = null; if (i > 2) {

x = ((--i) - 1) * 5;

String sql = \ + x

+ \\

+ x + \ list = query(sql); } else if (i == 2) { list = queryFirst(); i--;

} else if (i < 2) { list = null; }

return list; }

public static boolean add(Student cou) {

Connection conn = DBConnection.getConnection(); PreparedStatement pstmt = null;

String sql = \ int x = 0; try {

pstmt = conn.prepareStatement(sql); pstmt.setString(1, cou.getSname()); pstmt.setString(2, cou.getSpro()); pstmt.setString(3, cou.getSdept());

.

.

pstmt.setString(4, cou.getSgrade()); pstmt.setString(5, cou.getSaddress()); x = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try {

if (pstmt != null) pstmt.close(); if (conn != null) { conn.close(); }

} catch (SQLException e) { e.printStackTrace(); } }

if (x == 1) { return true; } else {

return false; } }

public static boolean del(String key) {

Connection conn = DBConnection.getConnection(); PreparedStatement stmt = null;

String sql = \ int x = 0; try {

stmt = conn.prepareStatement(sql); stmt.setString(1, key); x = stmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try {

if (stmt != null) stmt.close(); if (conn != null) conn.close();

} catch (SQLException e) { e.printStackTrace(); } }

if (x == 1) {

.

.

return true; } else {

return false; } }

public static boolean modify(Student cou) {

Connection conn = DBConnection.getConnection(); PreparedStatement pstmt = null;

String sql = \student set spro=?,sdept=?,sgrade=?,saddress=? where sname =?\ int x = 0; try {

pstmt = conn.prepareStatement(sql); pstmt.setString(1, cou.getSpro()); pstmt.setString(2, cou.getSdept()); pstmt.setString(3, cou.getSaddress()); pstmt.setString(4, cou.getSgrade()); pstmt.setString(5, cou.getSname()); x = pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }

if(x==1){

return true; }else {

return false; } } }

Student实体类: package edu;

public class Student { private String sname; private String spro; private String sdept; private String sgrade; private String saddress; public String getSname() { return sname; }

public void setSname(String sname) { this.sname = sname; }

.

.

public String getSpro() { return spro; }

public void setSpro(String spro) { this.spro = spro; }

public String getSdept() { return sdept; }

public void setSdept(String sdept) { this.sdept = sdept; }

public String getSgrade() { return sgrade; }

public void setSgrade(String sgrade) { this.sgrade = sgrade; }

public String getSaddress() { return saddress; }

public void setSaddress(String saddress) { this.saddress = saddress; } }

表格类: package edu;

import javax.swing.table.AbstractTableModel;

public class TableModel extends AbstractTableModel {

public final static String[] columnHeaders = { \姓名\,\专业\,\学院\,\成绩\,\家庭住址\ };

private Object[][] datavalues = { {\,\,\,\,\} };

public int getColumnCount() { return datavalues[0].length; }

.

.

public int getRowCount() { return datavalues.length; }

public Object getValueAt(int rowIndex, int columnIndex) { return datavalues[rowIndex][columnIndex]; }

public String getColumnName(int column) { return columnHeaders[column]; }

public void setDatavalues(Object[][] datavalues) { this.datavalues = datavalues; } }

六、编程心得

这次实验老师让我们做一个Java界面与数据库相连,实现数据动态变化的小项目,于是,我就选择做一个学生信息管理系统,实现学生信息在数据库中的简单处理,自我感觉,方法是实现比较简单,但是Java界面的规划布局比较麻烦,稍微弄不好就会跑偏,而且步骤很繁琐,稍微不注意就会出错,尤其是那个表格中动态数据的处理,做了很长时间才搞定,经过这次实验,让我对Java又有了新的认识,真切的感受到java语言的强大之处,我会继续努力的,把Java学好!

.

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

Top