###該当のソースコード
package employee_management_system;
import java.awt.Cursor;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.Font;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ListSelectionModel;
public class employee_list extends JFrame {
private static final long serialVersionUID = 1L; // ソースコード保護
private final Action Sign_action = new Sign_up();
private final Action Edit_action = new Edit();
private final Action Deleate_action = new Deleate();
private JPanel contentPane;
private JTable table;
private JButton Sign_button;
private JButton Edit_button;
private JButton Deleate_button;
public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { employee_list frame = new employee_list(); frame.setTitle("社員一覧"); // タイトル frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public employee_list() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ×ボタンを押すとシステム終了 setBounds(100, 100, 600, 450); // ウィンドウ contentPane = new JPanel(); // JPanel配置 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JScrollPane scrollPane = new JScrollPane(); // テーブル情報がウィンドウサイズを超えるとき、スクロールして表示する。 scrollPane.setBounds(10, 40, 560, 360); contentPane.add(scrollPane); table = new JTable(); // テーブルのカラム表示 table.setFont(new Font("MS Pゴシック", Font.PLAIN, 12)); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setModel(new DefaultTableModel( new Object[][] { }, new String[] { "社員No", "名前", "生年月日", "年齢","性別", "メールアドレス" } )); this.setCursor(new Cursor(Cursor.WAIT_CURSOR)); // データベース接続 try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee_list","root","password"); Statement stm = conn.createStatement(); String sql = "select * from employee_list"; ResultSet rs = stm.executeQuery(sql); DefaultTableModel display = (DefaultTableModel)table.getModel(); // テーブル情報表示 display.setRowCount(0); while(rs.next()){display.addRow(new String[]{rs.getString(1), rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6)}); } rs.close(); // データベース切断 stm.close(); conn.close(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); table.getColumnModel().getColumn(5).setPreferredWidth(250); scrollPane.setViewportView(table); table.setDefaultEditor(Object.class, null); Sign_button = new JButton("新規登録"); // 新規登録ボタン Sign_button.setBounds(20, 5, 120, 30); Sign_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); Sign_button.setAction(Sign_action); Sign_button.setFont(new Font("MS Pゴシック", Font.PLAIN, 14)); contentPane.add(Sign_button); Edit_button = new JButton("編集"); // 編集ボタン Edit_button.setFont(new Font("MS Pゴシック", Font.PLAIN, 14)); Edit_button.setBounds(180, 5, 70, 30); Edit_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {} }); Edit_button.setAction(Edit_action); contentPane.add(Edit_button); Deleate_button = new JButton("削除"); // 削除ボタン Deleate_button.setFont(new Font("MS Pゴシック", Font.PLAIN, 14)); Deleate_button.setBounds(290, 5, 70, 30); Deleate_button.setAction(Deleate_action); Deleate_button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { } }); contentPane.add(Deleate_button); } private class Sign_up extends AbstractAction { // 新規登録ボタンを押した際の挙動 private static final long serialVersionUID = 1L; public Sign_up() { putValue(NAME, "新規登録"); putValue(SHORT_DESCRIPTION, "新しく社員の情報を登録"); // カーソルを合わせた際に説明文を表示 } public void actionPerformed(ActionEvent e) { employee_sign_up sign = new employee_sign_up(); sign.sign_up(); } } private class Edit extends AbstractAction { // 編集ボタンを押した際の挙動 private static final long serialVersionUID = 1L; public Edit() { putValue(NAME, "編集"); putValue(SHORT_DESCRIPTION, "選択している社員の情報を編集"); // カーソルを合わせた際に説明文を表示 } public void actionPerformed(ActionEvent e) { employee_edit ed = new employee_edit(); ed.edit(); } } private class Deleate extends AbstractAction { // 削除ボタンを押した際の挙動 private static final long serialVersionUID = 1L; public Deleate() { putValue(NAME, "削除"); putValue(SHORT_DESCRIPTION, "選択している社員の情報を削除"); // カーソルを合わせた際に説明文を表示 } public void actionPerformed(ActionEvent e) { employee_deleate del = new employee_deleate(); del.deleate(); } }
}
###試したこと
ネットで調べる。調査力がないのかもう2週間ほど調べる日が続いています。。。
###補足情報(言語/FW/ツール等のバージョンなど)
JAVA、Eclipse、JavaSwing