1、javaswing界面实现数据库增删改查数据库程序设计大作业班级:2012级软件外包服务一班学号:0117姓名: 。 时间:2013-6-19 1功能描述 功能简介用swing做出图形化界面形式,实现数据库的增删改查把员工表跟部门表连接起来,实现数据的增加,删除,修改,查找。 实现步骤(1)安装好虚拟机,并在其下确认oracle已安装成功(可以在dos下输入相关命令来检查是否安装成功)。(2)在网络中心修改pc机上的VMware Network Adapter的IP、子网页码(默认)、网关。(3)修改虚拟机的IP、网掩码(默认)、网关,确保PC机上的网关和虚拟机的IP一致。(在控制面板网络和共
2、享中心本地连接3属性中,修改IP、网掩码)(4)在PC机的dos命令控制台ping虚拟机的IP,确保正常,能ping通(即将虚拟机内外ping通)。(5)配置好虚拟机下的oracle的数据库和监听。(6)在eclipse中编写相关代码,并用jtable实现图形化界面,用以实现与数据库的连接和操作数据库等功能。(7)在eclipse中导入数据库的驱动。(8)运行eclipse,查看运行结果。2. 核心代码 1. 数据库连接package class DatabaseConnection public static void main(String args) Connection conn =
3、null; Statement stmt = null; ResultSet rs = null; try (); String url = :1521/WFJ; conn = (url, hr, hr); stmt = (); String sql = select * from departments; rs = (sql); while () department_id); t); department_name); t); manager_id); t); location_id); catch (ClassNotFoundException e) (); catch (SQLExce
4、ption e) (); finally try if (rs != null) (); if (stmt != null) (); if (conn != null) (); catch (SQLException e) etLayout(null); JLabel lblemployee = new JLabel(department_id); (12, 10, 220, 13); getContentPane().add(lblemployee); department_idField = new JTextField(); (100, 7, 96, 19); getContentPan
5、e().add(department_idField); (10); JLabel lblAge = new JLabel(department_name); (252, 10, 220, 13); getContentPane().add(lblAge); department_nameField = new JTextField(); (365, 7, 96, 19); getContentPane().add(department_nameField); (10); JLabel lblStuno = new JLabel(manager_id); (12, 36, 220, 13);
6、getContentPane().add(lblStuno); manager_idField = new JTextField(); (10); (100, 33, 96, 19); getContentPane().add(manager_idField); JLabel lblClass = new JLabel(location_id); (252, 36, 220, 13); getContentPane().add(lblClass); location_idField = new JTextField(); (10); (365, 33, 96, 19); getContentP
7、ane().add(location_idField); JButton btnAdd = new JButton(增加); (new ActionListener() public void actionPerformed(ActionEvent e) add(); ); (75, 59, 77, 21); getContentPane().add(btnAdd); JButton btnDel = new JButton(删除); (new ActionListener() public void actionPerformed(ActionEvent e) del(); ); (180,
8、 59, 77, 21); getContentPane().add(btnDel); JButton btnUpdate = new JButton(更新); (new ActionListener() public void actionPerformed(ActionEvent e) update(); ); (280, 59, 77, 21); getContentPane().add(btnUpdate); JButton btnFind = new JButton(查找); (new ActionListener() public void actionPerformed(Acti
9、onEvent e) find(); ); (380, 59, 77, 21); getContentPane().add(btnFind); model = new DefaultTableModel(columns, 0); table = new JTable(model); (new MouseAdapter() public void mouseClicked(MouseEvent e) int row = (); (String) (row, 0); (String) (row, 2); (String) (row, 3); (String) (row, 4); tmp = get
10、Input(); ); JScrollPane scrollPane = new JScrollPane(table); (12, 100, 571, 248); getContentPane().add(scrollPane); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); setLocation(350,200); setSize(601, 380); setResizable(false); setVisible(true); private Student getInput() Student
11、 stu = new Student(); = (); = (); = (); = (); return stu; private void add() (getInput(); showTable(); private void del() for (int i = 0; i (); i+) if (i) (i);break; showTable(); private void update() Student stu = getInput(); for (int i = 0; i (); i+) if (i) (i); (i, stu); break; showTable(); priva
12、te void find() removeRows(); Student stu = getInput(); for (int i = 0; i (); i+) Student tmp = (Student) (i); if (stu) (); break; private void showTable() removeRows(); for (int i = 0; i 0) (0); public static void main(String args) new AppStudent(); class Studentimplements Serializable public String
13、 department_id; public String department_name; public String manager_id; public String location_id; public boolean equals(Object obj) return equals(Student) obj); public boolean equals(Student obj) boolean isdepartment_id = true; if != null & !.equals) isdepartment_id = ; boolean isdepartment_name =
14、 true; if != null & !.equals) isdepartment_name = ; boolean ismanager_id = true; if != null & !.equals) ismanager_id = ; boolean islocation_id = true; if != null & !.equals) islocation_id = ; return isdepartment_id& isdepartment_name & ismanager_id & islocation_id; public String toArray() return new
15、 String department_id, department_name, manager_id, location_id; public void setVisible(boolean b) 实训总结通过这次实训我发现了自己许多的缺点和不足,我一定会好好找出不足,尽最大可能去改正,不断进步发展。对于本次实训,我表示很遗憾,未能达到预期的效果,归根到底还是关于java的知识没学好。我认为基础知识是很重要的,图形化界面要在eclipse中实现必须要熟悉jframe的功能,只有熟知jframe的所有功能才能对图形化界面做出来,但是我对之一部分的了解是少之又少,不足以通过jframe来实现图形化界面,并且在里面连接数据库,实现oracle中hr用户中department 和employees这两张表的增删改查。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1