连接数据库在JTable中显示

本文介绍了一个使用Java Swing创建的简单应用程序,该程序能够从数据库查询数据并将其显示在一个表格中。通过实例展示了如何利用Swing组件构建用户界面,并通过SQL语句与数据库进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//刚学习的写的一些笔记和大家分享一下有什么不改进的地方希望大家能指正

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

public class MainHome {
	public static void main(String[] args) {
		Window win = new Window();
		win.setTitle("小学期作业");
	}

}

class Window extends JFrame implements ActionListener {
	JMenuBar bar;
	JMenu menuQuery, menuDelete, menuView;
	JMenuItem delete, qurey, itemClose;
	JTextField text;
	JButton button;
	JLabel label;
	JPanel panel;
	JScrollPane jscrollpane;
	JTable table;
	DefaultTableModel dtm;
	Statement stmt;
	ResultSet rs;
	ResultSetMetaData rsmd;
	DataBaseConnection dbc = new DataBaseConnection();

	Window() {
		init();
		setBounds(300, 100, 850, 550);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void init() {
		setLayout(new BorderLayout());
		// 菜单条和菜单
		bar = new JMenuBar();
		menuQuery = new JMenu("查询");
		menuDelete = new JMenu("删除");
		menuView = new JMenu("更新");

		bar.add(menuQuery);
		bar.add(menuDelete);
		bar.add(menuView);
		setJMenuBar(bar);

		// 文件菜单
		qurey = new JMenuItem("查询");
		qurey.addActionListener(this);
		menuQuery.add(qurey);
		delete = new JMenuItem("删除");
		delete.addActionListener(this);
		menuDelete.add(delete);

		// 插入数据
		text = new JTextField(16);
		button = new JButton("删除");
		label = new JLabel("删除数据");
		panel = new JPanel();
		panel.add(label);
		panel.add(text);
		panel.add(button);
		button.addActionListener(this);
		add(panel, BorderLayout.SOUTH);

	}

	@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
	@Override
	public void actionPerformed(ActionEvent e) {
		Vector colum = new Vector();
		Vector rows = new Vector();

		// 查询
		if (e.getSource() == qurey) {
			rs = dbc.executeQuery("SELECT * FROM employee");
			try {
				rsmd = rs.getMetaData();
				for (int i = 1; i <= rsmd.getColumnCount(); ++i)
					colum.addElement(rsmd.getColumnName(i));
				while (rs.next()) {
					Vector currow = new Vector();
					for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
						currow.addElement(rs.getString(i));
					}
					rows.addElement(currow);
				}
				table = new JTable(rows, colum);
				add(table, BorderLayout.CENTER);
				table.setVisible(true);
				table.setRowHeight(50);
				add(new JScrollPane(table), BorderLayout.CENTER);
				// table.setFillsViewportHeight(true);

			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			show();

		}

	}

}

数据库连接部分

//数据库的连接
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DataBaseConnection {
	
	Connection con = null;
	Statement sql = null;
	ResultSet rs = null;

	public DataBaseConnection() {

		// shujuku
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 加载驱动
		} catch (ClassNotFoundException e) {
			System.out.println(e);
		}
		try {
			con = DriverManager.getConnection("jdbc:odbc:star", "", ""); // 连接数据库

		} catch (SQLException e) {
			System.out.println(e);
		}
	}

	// 查询
	public ResultSet executeQuery(String sql_s) {
		try {
			sql = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			rs = sql.executeQuery(sql_s);
		} catch (SQLException e) {
			System.out.println(e);
		}
		return rs;
	}

	// update操作
	public int executeUpdate(String sql_s) {

		int rs = 0;
		try {
			sql = con.createStatement();
			rs = sql.executeUpdate(sql_s);
		} catch (SQLException e) {
			System.out.println(e);
		}

		return rs;
	}

	// 关闭数据库
	public void close() {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
//还有很多功能没有实现,后面有时间再补上!!Ps:正在学习中~~~



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值