親フォルダ
001.package syain;
002. 
003.import java.sql.DriverManager;
004.import java.sql.ResultSet;
005.import java.sql.SQLException;
006. 
007.import org.eclipse.jface.action.MenuManager;
008.import org.eclipse.jface.action.StatusLineManager;
009.import org.eclipse.jface.action.ToolBarManager;
010.import org.eclipse.jface.window.ApplicationWindow;
011.import org.eclipse.swt.SWT;
012.import org.eclipse.swt.events.SelectionAdapter;
013.import org.eclipse.swt.events.SelectionEvent;
014.import org.eclipse.swt.graphics.Point;
015.import org.eclipse.swt.widgets.Button;
016.import org.eclipse.swt.widgets.Composite;
017.import org.eclipse.swt.widgets.Control;
018.import org.eclipse.swt.widgets.Display;
019.import org.eclipse.swt.widgets.Shell;
020.import org.eclipse.swt.widgets.Text;
021. 
022.import com.mysql.jdbc.Connection;
023.import com.mysql.jdbc.Statement;
024. 
025.public class Main extends ApplicationWindow {
026.    private Text text;
027.     
028.    private Connection conn = null;
029.    private Statement stmt = null;
030.    private ResultSet rs = null;   
031.    private String connectionString = "jdbc:mysql://localhost/lightbox?user=root&password=";
032.    private Text fld_sname;
033. 
034.    /**
035.     * Create the application window.
036.     */
037.    public Main() {
038.        super(null);
039.        createActions();
040.        addToolBar(SWT.FLAT | SWT.WRAP);
041.        addMenuBar();
042.        addStatusLine();
043.    }
044. 
045.    /**
046.     * Create contents of the application window.
047.     * @param parent
048.     */
049.    @Override
050.    protected Control createContents(Composite parent) {
051.        Composite container = new Composite(parent, SWT.NONE);
052.         
053.        text = new Text(container, SWT.BORDER);
054.        text.setBounds(10, 10, 73, 21);
055.         
056.        Button btnNewButton = new Button(container, SWT.NONE);
057.        btnNewButton.addSelectionListener(new SelectionAdapter() {
058.            @Override
059.            public void widgetSelected(SelectionEvent e) {
060.                 
061.                String scode = text.getText();
062.                System.out.println(scode);
063. 
064.                 
065.                try {
066.                    // 接続文字列で接続
067.                    conn = (Connection) DriverManager.getConnection(connectionString);
068.                     
069.                    String query_base = "select * from 社員マスタ where 社員コード = '%s'";
070.                    String query = String.format(query_base, scode);
071.                    System.out.println(query);
072.                     
073.                    stmt = (Statement) conn.createStatement();
074.                    rs = stmt.executeQuery(query);
075.                     
076.                    String sname;
077.                    if( rs.next() ) {
078.                         
079.                        sname = rs.getString("氏名");
080.                        System.out.println(sname);
081.                         
082.                        fld_sname.setText(sname);
083.                         
084.                    }
085. 
086.                     
087.                    rs.close();
088.                    stmt.close();
089.                    // 接続を閉じる
090.                    conn.close();
091.                     
092.                } catch (SQLException ex) {
093.                    // TODO 自動生成された catch ブロック
094.                    ex.printStackTrace();
095.                }
096.                 
097.            }
098.        });
099.        btnNewButton.setBounds(89, 8, 75, 25);
100.        btnNewButton.setText("New Button");
101.         
102.        fld_sname = new Text(container, SWT.BORDER);
103.        fld_sname.setBounds(10, 49, 324, 21);
104. 
105.        return container;
106.    }
107. 
108.    /**
109.     * Create the actions.
110.     */
111.    private void createActions() {
112.        // Create the actions
113.    }
114. 
115.    /**
116.     * Create the menu manager.
117.     * @return the menu manager
118.     */
119.    @Override
120.    protected MenuManager createMenuManager() {
121.        MenuManager menuManager = new MenuManager("menu");
122.        return menuManager;
123.    }
124. 
125.    /**
126.     * Create the toolbar manager.
127.     * @return the toolbar manager
128.     */
129.    @Override
130.    protected ToolBarManager createToolBarManager(int style) {
131.        ToolBarManager toolBarManager = new ToolBarManager(style);
132.        return toolBarManager;
133.    }
134. 
135.    /**
136.     * Create the status line manager.
137.     * @return the status line manager
138.     */
139.    @Override
140.    protected StatusLineManager createStatusLineManager() {
141.        StatusLineManager statusLineManager = new StatusLineManager();
142.        return statusLineManager;
143.    }
144. 
145.    /**
146.     * Launch the application.
147.     * @param args
148.     */
149.    public static void main(String args[]) {
150.        try {
151.            Main window = new Main();
152.            window.setBlockOnOpen(true);
153.            window.open();
154.            Display.getCurrent().dispose();
155.        } catch (Exception e) {
156.            e.printStackTrace();
157.        }
158.    }
159. 
160.    /**
161.     * Configure the shell.
162.     * @param newShell
163.     */
164.    @Override
165.    protected void configureShell(Shell newShell) {
166.        super.configureShell(newShell);
167.        newShell.setText("New Application");
168.    }
169. 
170.    /**
171.     * Return the initial size of the window.
172.     */
173.    @Override
174.    protected Point getInitialSize() {
175.        return new Point(450, 300);
176.    }
177.}