package syain; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import org.eclipse.jface.action.MenuManager; import org.eclipse.jface.action.StatusLineManager; import org.eclipse.jface.action.ToolBarManager; import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; public class Main extends ApplicationWindow { private Text text; private Connection conn = null; private Statement stmt = null; private ResultSet rs = null; private String connectionString = "jdbc:mysql://localhost/lightbox?user=root&password="; private Text fld_sname; /** * Create the application window. */ public Main() { super(null); createActions(); addToolBar(SWT.FLAT | SWT.WRAP); addMenuBar(); addStatusLine(); } /** * Create contents of the application window. * @param parent */ @Override protected Control createContents(Composite parent) { Composite container = new Composite(parent, SWT.NONE); text = new Text(container, SWT.BORDER); text.setBounds(10, 10, 73, 21); Button btnNewButton = new Button(container, SWT.NONE); btnNewButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { String scode = text.getText(); System.out.println(scode); try { // 接続文字列で接続 conn = (Connection) DriverManager.getConnection(connectionString); String query_base = "select * from 社員マスタ where 社員コード = '%s'"; String query = String.format(query_base, scode); System.out.println(query); stmt = (Statement) conn.createStatement(); rs = stmt.executeQuery(query); String sname; if( rs.next() ) { sname = rs.getString("氏名"); System.out.println(sname); fld_sname.setText(sname); } rs.close(); stmt.close(); // 接続を閉じる conn.close(); } catch (SQLException ex) { // TODO 自動生成された catch ブロック ex.printStackTrace(); } } }); btnNewButton.setBounds(89, 8, 75, 25); btnNewButton.setText("New Button"); fld_sname = new Text(container, SWT.BORDER); fld_sname.setBounds(10, 49, 324, 21); return container; } /** * Create the actions. */ private void createActions() { // Create the actions } /** * Create the menu manager. * @return the menu manager */ @Override protected MenuManager createMenuManager() { MenuManager menuManager = new MenuManager("menu"); return menuManager; } /** * Create the toolbar manager. * @return the toolbar manager */ @Override protected ToolBarManager createToolBarManager(int style) { ToolBarManager toolBarManager = new ToolBarManager(style); return toolBarManager; } /** * Create the status line manager. * @return the status line manager */ @Override protected StatusLineManager createStatusLineManager() { StatusLineManager statusLineManager = new StatusLineManager(); return statusLineManager; } /** * Launch the application. * @param args */ public static void main(String args[]) { try { Main window = new Main(); window.setBlockOnOpen(true); window.open(); Display.getCurrent().dispose(); } catch (Exception e) { e.printStackTrace(); } } /** * Configure the shell. * @param newShell */ @Override protected void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText("New Application"); } /** * Return the initial size of the window. */ @Override protected Point getInitialSize() { return new Point(450, 300); } }