001.
package
mysql;
002.
003.
import
java.sql.DriverManager;
004.
import
java.sql.ResultSet;
005.
import
java.sql.SQLException;
006.
import
java.text.SimpleDateFormat;
007.
008.
import
org.eclipse.jface.window.ApplicationWindow;
009.
import
org.eclipse.swt.SWT;
010.
import
org.eclipse.swt.events.SelectionAdapter;
011.
import
org.eclipse.swt.events.SelectionEvent;
012.
import
org.eclipse.swt.graphics.Point;
013.
import
org.eclipse.swt.widgets.Button;
014.
import
org.eclipse.swt.widgets.Composite;
015.
import
org.eclipse.swt.widgets.Control;
016.
import
org.eclipse.swt.widgets.Display;
017.
import
org.eclipse.swt.widgets.Shell;
018.
import
org.eclipse.swt.widgets.Table;
019.
import
org.eclipse.swt.widgets.TableColumn;
020.
import
org.eclipse.swt.widgets.TableItem;
021.
022.
import
com.mysql.jdbc.Connection;
023.
import
com.mysql.jdbc.ResultSetMetaData;
024.
import
com.mysql.jdbc.Statement;
025.
026.
public
class
Main
extends
ApplicationWindow {
027.
028.
029.
private
Table table;
030.
031.
032.
033.
private
Connection conn =
null
;
034.
private
Statement stmt =
null
;
035.
private
ResultSet rs =
null
;
036.
private
int
maxRows =
20
;
037.
038.
public
Main() {
039.
super
(
null
);
040.
createActions();
041.
}
042.
043.
@Override
044.
protected
Control createContents(Composite parent) {
045.
Composite container =
new
Composite(parent, SWT.NONE);
046.
047.
table =
new
Table(container, SWT.BORDER | SWT.FULL_SELECTION);
048.
table.setBounds(
10
,
41
,
764
,
338
);
049.
table.setHeaderVisible(
true
);
050.
table.setLinesVisible(
true
);
051.
052.
Button button =
new
Button(container, SWT.NONE);
053.
button.addSelectionListener(
new
SelectionAdapter() {
054.
@Override
055.
public
void
widgetSelected(SelectionEvent e) {
056.
057.
table.removeAll();
058.
loadMySQL(
"select * from 社員マスタ"
);
059.
}
060.
061.
});
062.
button.setBounds(
10
,
10
,
75
,
25
);
063.
button.setText(
"実行"
);
064.
065.
return
container;
066.
}
067.
068.
private
void
createActions() {
069.
070.
}
071.
072.
public
static
void
main(String args[]) {
073.
try
{
074.
Main window =
new
Main();
075.
window.setBlockOnOpen(
true
);
076.
window.open();
077.
Display.getCurrent().dispose();
078.
}
catch
(Exception e) {
079.
e.printStackTrace();
080.
}
081.
}
082.
083.
@Override
084.
protected
void
configureShell(Shell newShell) {
085.
super
.configureShell(newShell);
086.
newShell.setText(
"MySQL Connector/J"
);
087.
}
088.
089.
@Override
090.
protected
Point getInitialSize() {
091.
return
new
Point(
800
,
443
);
092.
}
093.
094.
095.
private
void
loadMySQL( String sql ) {
096.
097.
try
{
098.
099.
conn = (Connection) DriverManager.getConnection(
100.
"jdbc:mysql://localhost/lightbox?user=root&password="
101.
);
102.
103.
stmt = (Statement) conn.createStatement();
104.
rs = stmt.executeQuery(sql);
105.
106.
107.
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
108.
109.
110.
int
columnCount = rsmd.getColumnCount();
111.
112.
113.
int
tableColumnCount = table.getColumnCount();
114.
TableColumn tableColumnWork =
null
;
115.
for
(
int
i = tableColumnCount -
1
; i >=
0
; i--) {
116.
tableColumnWork = table.getColumn(i);
117.
tableColumnWork.dispose();
118.
}
119.
120.
121.
TableColumn tableColumn =
null
;
122.
for
(
int
i =
1
; i <= columnCount; i++) {
123.
tableColumn =
new
TableColumn(table, SWT.NONE);
124.
tableColumn.setWidth(
100
);
125.
tableColumn.setText(rsmd.getColumnName(i));
126.
}
127.
128.
TableItem tableItem =
null
;
129.
SimpleDateFormat sdf =
new
SimpleDateFormat(
"yyyy/MM/dd"
);
130.
int
countRow =
0
;
131.
while
( rs.next() && countRow < maxRows ) {
132.
133.
countRow++;
134.
135.
String[] columnData =
new
String[columnCount];
136.
137.
for
(
int
i =
1
; i <= columnCount; i++) {
138.
139.
if
( rsmd.getColumnTypeName(i).equals(
"DATETIME"
) ) {
140.
columnData[i-
1
] = sdf.format(rs.getDate(i));
141.
}
142.
else
{
143.
columnData[i-
1
] = rs.getString(i);
144.
}
145.
146.
}
147.
tableItem =
new
TableItem(table, SWT.NONE);
148.
tableItem.setText(columnData);
149.
}
150.
151.
rs.close();
152.
stmt.close();
153.
conn.close();
154.
155.
}
catch
(SQLException ex) {
156.
157.
System.out.println(
"SQLException: "
+ ex.getMessage());
158.
System.out.println(
"SQLState: "
+ ex.getSQLState());
159.
System.out.println(
"VendorError: "
+ ex.getErrorCode());
160.
}
161.
162.
}
163.
}