JAVAswing界面实现数据库增删改查(精)

更新时间:2024-06-13 13:18:01 阅读量: 综合文库 文档下载

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

数据库程序设计 大作业

班级:2012级软件外包服务一班 学号:201215140117 姓名: 。。。 时间:2013-6-19 1.功能描述 1.1 功能简介

用swing做出图形化界面形式,实现数据库的增删改查

把员工表跟部门表连接起来,实现数据的增加,删除,修改,查找。 1.2 实现步骤

(1)安装好虚拟机,并在其下确认oracle已安装成功(可以在dos下输入相关命令来检查是否安装成功)。

(2)在网络中心修改pc机上的VMware Network Adapter的IP、子网页码(默认)、网关。

(3)修改虚拟机的IP、网掩码(默认)、网关,确保PC机上的网关和虚拟机的IP一致。(在控制面板——>网络和共享中心——>本地连接3——>属性中,修改IP、网掩码) (4)在PC机的dos命令控制台ping虚拟机的IP,确保正常,能ping通(即将虚拟机内外ping通)。

(5)配置好虚拟机下的oracle的数据库和监听。

(6)在eclipse中编写相关代码,并用jtable实现图形化界面,用以实现与数据库的连接和操作数据库等功能。

(7)在eclipse中导入数据库的驱动。 (8)运行eclipse,查看运行结果。 2. 核心代码 2.1

1. 数据库连接

package org.lxh.info.dbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DatabaseConnection { public static void main(String[] args { Connection conn = null; Statement stmt = null; ResultSet rs = null; try {

Class.forName(\String url = \conn = DriverManager.getConnection(url, \stmt = conn.createStatement(;

String sql = \rs = stmt.executeQuery(sql;

while (rs.next( {

System.out.print(rs.getInt(\System.out.print(\

System.out.print(rs.getString(\System.out.print(\

System.out.print(rs.getInt(\System.out.print(\

System.out.print(rs.getInt(\System.out.println(; }

} catch (ClassNotFoundException e { e.printStackTrace(; } 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 {

// TODO Auto-generated catch block e.printStackTrace(; } } } }

2. 生成get set方法 package edu;

public class Country { private String department_id; private String department_name; private String area; private String population; public String getdepartment_id( { return department_id; }

public void setdepartment_id(String department_name { this.name = department_id; }

public String getdepartment_name( { return department_name; }

public void setdepartment_name(String department_name { this.department_name = department_name; }

public String getmanager_id( { return manager_id; }

public void setmanager_id(String manager_id { this.manager_id= manager_id; }

public String getlocation_id( { return location_id; }

public void setlocation_id(String location_idn { this.location_id = location_id; } } } 3实现方法

package tuxinghua; import java.awt.Color;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.Serializable; import java.sql.DriverManager; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame;

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

import javax.swing.table.DefaultTableModel; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class AppStudent extends JFrame{ private JTextField department_idField; private JTextField department_nameField; private JTextField manager_idField; private JTextField location_idField; private JTable table;

private DefaultTableModel model;

private String[] columns = { \\private List data; private Student tmp; public AppStudent( { data = new ArrayList(; getContentPane(.setLayout(null;

JLabel lblemployee = new JLabel(\lblemployee.setBounds(12, 10, 220, 13; getContentPane(.add(lblemployee; department_idField = new JTextField(; department_idField.setBounds(100, 7, 96, 19;

getContentPane(.add(department_idField; department_idField.setColumns(10;

JLabel lblAge = new JLabel(\lblAge.setBounds(252, 10, 220, 13; getContentPane(.add(lblAge;

department_nameField = new JTextField(; department_nameField.setBounds(365, 7, 96, 19; getContentPane(.add(department_nameField; department_nameField.setColumns(10; JLabel lblStuno = new JLabel(\lblStuno.setBounds(12, 36, 220, 13; getContentPane(.add(lblStuno; manager_idField = new JTextField(; manager_idField.setColumns(10;

manager_idField.setBounds(100, 33, 96, 19; getContentPane(.add(manager_idField; JLabel lblClass = new JLabel(\lblClass.setBounds(252, 36, 220, 13; getContentPane(.add(lblClass; location_idField = new JTextField(; location_idField.setColumns(10;

location_idField.setBounds(365, 33, 96, 19; getContentPane(.add(location_idField; JButton btnAdd = new JButton(\增加\

btnAdd.addActionListener(new ActionListener( {

public void actionPerformed(ActionEvent e { add(; } } ;

btnAdd.setBounds(75, 59, 77, 21; getContentPane(.add(btnAdd; JButton btnDel = new JButton(\删除\

btnDel.addActionListener(new ActionListener( { public void actionPerformed(ActionEvent e { del(; } } ;

btnDel.setBounds(180, 59, 77, 21; getContentPane(.add(btnDel;

JButton btnUpdate = new JButton(\更新\

btnUpdate.addActionListener(new ActionListener( { public void actionPerformed(ActionEvent e { update(; } } ;

btnUpdate.setBounds(280, 59, 77, 21; getContentPane(.add(btnUpdate;

JButton btnFind = new JButton(\查找\

btnFind.addActionListener(new ActionListener( { public void actionPerformed(ActionEvent e { find(; } } ;

btnFind.setBounds(380, 59, 77, 21; getContentPane(.add(btnFind;

model = new DefaultTableModel(columns, 0; table = new JTable(model;

table.addMouseListener(new MouseAdapter( {

public void mouseClicked(MouseEvent e {

int row = table.getSelectedRow(;

department_idField.setText((String table.getValueAt(row, 0; department_nameField.setText((String table.getValueAt(row, 2; manager_idField.setText((String table.getValueAt(row, 3; location_idField.setText((String table.getValueAt(row, 4; tmp = getInput(; } };

JScrollPane scrollPane = new JScrollPane(table; scrollPane.setBounds(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 stu = new Student(;

stu.department_id= department_idField.getText(; stu.department_name = department_nameField.getText(; stu.manager_id = manager_idField.getText(; stu.location_id = location_idField.getText(; return stu; }

private void add( { data.add(getInput(; showTable(; }

private void del( {

for (int i = 0; i < data.size(; i++ {

if (tmp.equals(data.get(i {

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

Top