DBによるマスタメンテサンプル(参照と新規のみ)

データベースは Oracle を使ってテストしていますが、ODBC 経由なのでJava からは DSN さえあれば RDBMS は何でも良いです。
( ※ Class.forName( DriverName ); は無くても動きます )

update と delete の処理を実装していませんが、executeUpdate メソッドでSQL を実行するだけなので実装の追加は容易です。

※ キャンセル処理が実装されていません
( キャンセルボタンを追加して、クリックしたら社員コード入力に画面遷移させる )


記述を簡略化する為に、JPanel と JButton と JText をユーザークラスとして実装し、以前のソースコード を書き換えてあります。
con = DriverManager.getConnection(
	"jdbc:odbc:Oracle",		// Oracle
	"lightbox",
	"lightbox"
);

con = DriverManager.getConnection(
	"jdbc:odbc:MySQL;database=lightbox",	// MySQL
	"root",
	""
);

con = DriverManager.getConnection(
	"jdbc:odbc:SQS;database=lightbox",	// SQLServer
	"sa",
	"passwordpassword"
);

---------------------------------------------------------
キャラクタセットを指定する接続は以下のようになります
// 接続情報
Properties prop = new java.util.Properties();
prop.put("charSet", "shift_jis");
prop.put("user", "lightbox");
prop.put("password", "lightbox");

// 接続( Oracle は、DSN )
cn = DriverManager.getConnection("jdbc:odbc:Oracle", prop);
java.sun.com のドキュメント(IEで化ける場合は右クリックでエンコードでUnicode)



第一会話と第二会話のコントロール


確認ボタンで明細部分の入力に画面が遷移し、
更新ボタンで更新後、社員コードの入力へ画面が遷移します。
	/**
	 * This method initializes jButton
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new LboxButton();
			jButton.setBounds(new Rectangle(306, 23, 116, 31));
			jButton.setGroup(1);
			jButton.setText("確認");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					// ***********************************************
					// 社員コードの入力チェック
					// ***********************************************
					Object param[] = {lboxText1,lboxText2,lboxText3};
					appCheck.targetExistDB(lboxText.getText(),param);

					jContentPane.groupDisable(1);
					jContentPane.groupEnable(2);

				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new LboxButton();
			jButton1.setBounds(new Rectangle(30, 238, 392, 31));
			jButton1.setText("更新");
			jButton1.setGroup(2);
			jButton1.setEnabled(false);
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()");

					// 社員データの更新処理
					Object param[] = {lboxText,lboxText1,lboxText2,lboxText3};
					if ( !appUpdate.insertDataDB(param) ) {
						JOptionPane.showMessageDialog(
								Main.this,
								"書き込みエラーが発生しました",
								"エラー",
								JOptionPane.ERROR_MESSAGE);
						return;
					}
					JOptionPane.showMessageDialog(
							Main.this,
							"更新が終了しました");
					lboxText.setEditable(true);

					jContentPane.groupDisable(2);
					jContentPane.groupEnable(1);

					jContentPane.groupClear(2);

				}
			});
		}
		return jButton1;
	}
Check.java
入力した社員コードが存在する場合は DB よりデータを表示します
( ※ テキストバージョンも残してあります )
	//*************************************************
	// 社員コード存在チェック( DB バージョン )
	//*************************************************
	public boolean targetExistDB(String code,Object param[]) {

		boolean ret = true;

		Connection con = null;
		Statement stmt = null;
		ResultSet rset = null;
		String DriverName = "sun.jdbc.odbc.JdbcOdbcDriver";

		// 接続
		try {
			Class.forName( DriverName );
			con = DriverManager.getConnection(
				"jdbc:odbc:Oracle",	// ODBC DSN
				"lightbox",
				"lightbox"
			);
			stmt = con.createStatement();
		}
		catch( Exception e  ) {
			System.out.println(e.getMessage());
			ret = false;
		}
		// 接続失敗
		if ( !ret ) {
			return ret;
		}

		// SQL 作成
		String Query = "select * from 社員マスタ where 社員コード = ";
		Query += "'";
		Query += code;
		Query += "'";
		try {
			rset = stmt.executeQuery ( Query );
			ret = rset.next();
		}
		catch( SQLException e ) {
			System.out.println(e.getMessage());
			ret = false;
		}

		// データが存在しないので処理終了
		if ( !ret ) {
			return ret;
		}

		// 存在した場合の処理
		LboxText field1 = (LboxText)param[0];
		LboxText field2 = (LboxText)param[1];
		LboxText field3 = (LboxText)param[2];

		try {
			field1.setText( rset.getString( "氏名" ) );
			field2.setText( rset.getString( "フリガナ" ) );
			field3.setText( rset.getString( "給与" ) );
		}
		catch( SQLException e ) {
			System.out.println(e.getMessage());
			ret = false;
		}

		try {
			stmt.close();
			con.close();
		}
		catch( SQLException e ) {
			System.out.println(e.getMessage());
		}

		return ret;
	}
Update.java
データベースへの書き込み処理です
( ※ テキストバージョンも残してあります )
	public boolean insertDataDB(Object param[]) {

		boolean ret = true;

		Connection con = null;
		Statement stmt = null;
		ResultSet rset = null;
		String DriverName = "sun.jdbc.odbc.JdbcOdbcDriver";

		// 接続
		try {
			Class.forName( DriverName );
			con = DriverManager.getConnection(
				"jdbc:odbc:Oracle",	// ODBC DSN
				"lightbox",
				"lightbox"
			);
			stmt = con.createStatement();
		}
		catch( Exception e  ) {
			System.out.println(e.getMessage());
			ret = false;
		}
		// 接続失敗
		if ( !ret ) {
			return ret;
		}

		// 主キーデータ
		String code = ((LboxText)param[0]).getText();
		// データ
		String field1 = ((LboxText)param[1]).getText();
		String field2 = ((LboxText)param[2]).getText();
		String field3 = ((LboxText)param[3]).getText();

		// SQL 作成
		String Query = "insert into 社員マスタ (社員コード,氏名,フリガナ,給与)";
		Query += " values('";
		Query += code;
		Query += "','";
		Query += field1;
		Query += "','";
		Query += field2;
		Query += "',";
		Query += field3;
		Query += ")";
		try {
			stmt.executeUpdate( Query );
		}
		catch( SQLException e ) {
			System.out.println(Query);
			System.out.println(e.getMessage());
			ret = false;
		}

		try {
			stmt.close();
			con.close();
		}
		catch( SQLException e ) {
			System.out.println(e.getMessage());
		}

		return ret;
	}
ユーザークラス
画面遷移をコントロールする為のプロパティとメソッドを実装しています
LboxPanel
package myswing;

import javax.swing.JPanel;

public class LboxPanel extends JPanel {
	
	public void groupEnable( int target ) {
		
		int cnt = this.getComponentCount();
		for( int i = 0; i < cnt; i++) {
			java.awt.Component child = this.getComponent(i);
			String targetClass = child.getClass().toString();
			try {
				if ( targetClass.equals("class myswing.LboxText")) {
					if ( ((LboxText)child).getGroup() == target ) {
						child.setEnabled(true);
					}
				}
			}
			catch( Exception ex ) {
			}
			try {
				if ( targetClass.equals("class myswing.LboxButton")) {
					if ( ((LboxButton)child).getGroup() == target ) {
						child.setEnabled(true);
					}
				}
			}
			catch( Exception ex ) {
			}
		}
	}

	public void groupDisable( int target ) {

		int cnt = this.getComponentCount();
		for( int i = 0; i < cnt; i++) {
			java.awt.Component child = this.getComponent(i);
			String targetClass = child.getClass().toString();
			try {
				if ( targetClass.equals("class myswing.LboxText")) {
					if ( ((LboxText)child).getGroup() == target ) {
						child.setEnabled(false);
					}
				}
			}
			catch( Exception ex ) {
			}
			try {
				if ( targetClass.equals("class myswing.LboxButton")) {
					if ( ((LboxButton)child).getGroup() == target ) {
						child.setEnabled(false);
					}
				}
			}
			catch( Exception ex ) {
			}
		}
	}

	public void groupClear( int target ) {

		int cnt = this.getComponentCount();
		for( int i = 0; i < cnt; i++) {
			java.awt.Component child = this.getComponent(i);
			try {
				String targetClass = child.getClass().toString();
				if ( targetClass.equals("class myswing.LboxText")) {
					if ( ((LboxText)child).getGroup() == target ) {
						((LboxText)child).setText("");
					}
				}
			}
			catch( Exception ex ) {
			}
		}
	}
}
LboxButton
package myswing;

import javax.swing.JButton;

public class LboxButton extends JButton {

	private int _group = 0;

	public int getGroup() {

		return this._group;

	}

	public void setGroup(int arg) {

		this._group = arg;

	}
	
}
LboxText
package myswing;

import javax.swing.JTextField;

public class LboxText extends JTextField {

	private int _group = 0;

	public int getGroup() {

		return this._group;

	}

	public void setGroup(int arg) {

		this._group = arg;

	}

}