Java GUI和mysql小系統是一種常用的數據庫管理工具,此系統在Java語言基礎上運用了GUI編程技術,將操作數據庫的過程圖形化以便操作。
Java GUI和mysql小系統主要實現功能包括:數據庫表的增、刪、改、查、增量備份等。其操作界面簡潔明了,便于用戶進行操作,同時還支持數據導入、導出等高級操作。
下面是此系統的源代碼實例:
//導入java相關包 import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; public class JavaGuiAndMysqlSmallSystem { private JFrame frame; private JTextField textField; private JTable table; private DefaultTableModel model; private Connection conn; private Statement stmt; private ResultSet rs; private String url = "jdbc:mysql://localhost:3306/student"; private String username = "root"; private String password = "root"; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JavaGuiAndMysqlSmallSystem window = new JavaGuiAndMysqlSmallSystem(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public JavaGuiAndMysqlSmallSystem() { initialize(); } private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 680, 420); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); textField = new JTextField(); textField.setBounds(20, 20, 250, 30); frame.getContentPane().add(textField); textField.setColumns(10); JButton button = new JButton("查詢"); button.setBounds(300, 20, 100, 30); frame.getContentPane().add(button); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(20, 70, 630, 280); frame.getContentPane().add(scrollPane); table = new JTable(); model = new DefaultTableModel( new Object[][] {}, new String[] { "學號", "姓名", "性別", "年齡" } ); table.setModel(model); scrollPane.setViewportView(table); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { if (conn == null) { conn = DriverManager.getConnection(url, username, password); } stmt = conn.createStatement(); rs = stmt.executeQuery("SELECT * FROM stu WHERE name like '%" + textField.getText() + "%'"); while (model.getRowCount() >0) { model.removeRow(0); } while (rs.next()) { Object[] rowData = { rs.getString("id"), rs.getString("name"), rs.getString("gender"), rs.getString("age") }; model.addRow(rowData); } } 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(); } } } }); } }
以上是Java GUI和mysql小系統的一個簡單實例源代碼,開發人員可以根據實際需求進行開發和改進,以滿足不同的數據庫管理需求。