ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
Eclipse + WindowBuilder(Swing) : 簡単な MySQL アクセス
日時: 2014/04/29 14:19
名前: lightbox






前提条件
ソースコード : UTF-8
JDBC ドライバ : mysql-connector-java-5.1.30-bin.jar

拡張子:
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private final Action action = new SwingAction();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textField = new JTextField();
		textField.setBounds(51, 29, 96, 19);
		contentPane.add(textField);
		textField.setColumns(10);

		JButton btnNewButton = new JButton("New button");
		btnNewButton.setAction(action);
		btnNewButton.addFocusListener(new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent arg0) {
				System.out.println("focusGained");
			}

			@Override
			public void focusLost(FocusEvent e) {
				// TODO 自動生成されたメソッド・スタブ
				super.focusLost(e);
				System.out.println("focusLost");
			}
		});
		btnNewButton.setBounds(190, 28, 91, 21);
		contentPane.add(btnNewButton);

		textField_1 = new JTextField();
		textField_1.setBounds(51, 81, 326, 19);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		JButton btnNewButton_1 = new JButton("\u66F4\u65B0");
		btnNewButton_1.setBounds(286, 206, 91, 21);
		contentPane.add(btnNewButton_1);
	}

	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "確認");
			putValue(SHORT_DESCRIPTION, "社員コードを入力してください");
		}

		public void actionPerformed(ActionEvent e) {
			
			Connection con;
			Statement stmt;
			ResultSet rset;

			try {
				con = DriverManager.getConnection("jdbc:mysql://localhost/lightbox?" +
				                           "user=root&password=パスワード");			
				stmt = con.createStatement();
				String scode = textField.getText();
				rset = stmt.executeQuery ( "select * from `社員マスタ` where `社員コード` = '" + scode + "'" );
				if ( rset.next() ) {
					textField_1.setText(rset.getString( "氏名" ));
				}
				else {
					JOptionPane.showMessageDialog(contentPane, "入力した社員コードは存在しません");
				}
				
			} catch (Exception e1) {
				// TODO 自動生成された catch ブロック
				e1.printStackTrace();
			}
			
		}
	}
}
メンテナンス


日時: 2014/04/29 14:19
名前: lightbox