質問編集履歴

1

追記

2017/06/11 02:07

投稿

ryo-dev
ryo-dev

スコア437

test CHANGED
File without changes
test CHANGED
@@ -109,3 +109,119 @@
109
109
  ```
110
110
 
111
111
  一応PHPから接続する時みたいに、IP書いたのですが、どう書くのが正解なのですか?
112
+
113
+ ###追記
114
+
115
+ ```Java
116
+
117
+ package Test;
118
+
119
+
120
+
121
+ import java.sql.DriverManager;
122
+
123
+ import java.sql.SQLException;
124
+
125
+
126
+
127
+ import com.mysql.jdbc.Connection;
128
+
129
+ import com.mysql.jdbc.Statement;
130
+
131
+
132
+
133
+ public class Test {
134
+
135
+ public static void main(String[] args) throws SQLException {
136
+
137
+ // (1) 接続用のURIを用意する(必要に応じて認証指示user/passwordを付ける)
138
+
139
+ String uri = "jdbc:mysql:memory:192.168.12.15;create=true";
140
+
141
+
142
+
143
+ // (2) DriverManagerクラスのメソッドで接続する
144
+
145
+ Connection conn = null;
146
+
147
+ String url = "jdbc:mysql://192.168.12.15/******";
148
+
149
+ String user = "******";
150
+
151
+ String password = "******";
152
+
153
+
154
+
155
+ try{
156
+
157
+ conn = (Connection) DriverManager.getConnection(url, user, password);
158
+
159
+
160
+
161
+ // データベースに対する処理
162
+
163
+
164
+
165
+ }catch (SQLException e){
166
+
167
+ }
168
+
169
+
170
+
171
+ // (3) SQL送信用インスタンスの作成
172
+
173
+ Statement st = (Statement) conn.createStatement();
174
+
175
+
176
+
177
+ // (4) SQL送信
178
+
179
+ st.executeUpdate("create table sample(id integer primary key)");
180
+
181
+ Long start = System.currentTimeMillis();
182
+
183
+ Long delta = start;
184
+
185
+
186
+
187
+ // とりあえず1万回のループ
188
+
189
+ for (int i = 0; i < 10000; i++) {
190
+
191
+ st.executeUpdate("insert into sample values(" + i + ")");
192
+
193
+
194
+
195
+ // スプリットの計算
196
+
197
+ if (i % 1000 == 0) {
198
+
199
+ Long now = System.currentTimeMillis();
200
+
201
+ Long split = now - delta;
202
+
203
+ System.out.println("" + i + ": " + split + "ms.");
204
+
205
+ delta = now;
206
+
207
+ }
208
+
209
+ }
210
+
211
+ Long end = System.currentTimeMillis();
212
+
213
+ System.out.println("所要時間: " + (end - start) + "ms.");
214
+
215
+
216
+
217
+ // (5) 後始末(インスタンスの正常クローズ)
218
+
219
+ st.close();
220
+
221
+ conn.close();
222
+
223
+ }
224
+
225
+ }
226
+
227
+ ```