Javaでデータベースに接続したいです。
データベースは192.168.12.15のサーバで立ち上がっています。
実際に接続したいPCのIPは192.168.12.10です。データベース側に、このIPとユーザを登録してあるので、接続はできると思います。
あとは、コードの書き方なんですが、このサイトをみてコピペして少し書き換えたんですが、
Java
1package Test; 2 3import java.sql.DriverManager; 4import java.sql.SQLException; 5 6import com.mysql.jdbc.Connection; 7import com.mysql.jdbc.Statement; 8 9public class Test { 10 public static void main(String[] args) throws SQLException { 11 // (1) 接続用のURIを用意する(必要に応じて認証指示user/passwordを付ける) 12 String uri = "jdbc:mysql:memory:192.168.12.15;create=true"; 13 14 // (2) DriverManagerクラスのメソッドで接続する 15 Connection conn = (Connection) DriverManager.getConnection(uri); 16 17 // (3) SQL送信用インスタンスの作成 18 Statement st = (Statement) conn.createStatement(); 19 20 // (4) SQL送信 21 st.executeUpdate("create table sample(id integer primary key)"); 22 Long start = System.currentTimeMillis(); 23 Long delta = start; 24 25 // とりあえず1万回のループ 26 for (int i = 0; i < 10000; i++) { 27 st.executeUpdate("insert into sample values(" + i + ")"); 28 29 // スプリットの計算 30 if (i % 1000 == 0) { 31 Long now = System.currentTimeMillis(); 32 Long split = now - delta; 33 System.out.println("" + i + ": " + split + "ms."); 34 delta = now; 35 } 36 } 37 Long end = System.currentTimeMillis(); 38 System.out.println("所要時間: " + (end - start) + "ms."); 39 40 // (5) 後始末(インスタンスの正常クローズ) 41 st.close(); 42 conn.close(); 43 } 44}
この箇所をどう書いたらいいんですか?
// (1) 接続用のURIを用意する(必要に応じて認証指示user/passwordを付ける) String uri = "jdbc:mysql:memory:192.168.12.15;create=true";
一応PHPから接続する時みたいに、IP書いたのですが、どう書くのが正解なのですか?
###追記
Java
1package Test; 2 3import java.sql.DriverManager; 4import java.sql.SQLException; 5 6import com.mysql.jdbc.Connection; 7import com.mysql.jdbc.Statement; 8 9public class Test { 10 public static void main(String[] args) throws SQLException { 11 // (1) 接続用のURIを用意する(必要に応じて認証指示user/passwordを付ける) 12 String uri = "jdbc:mysql:memory:192.168.12.15;create=true"; 13 14 // (2) DriverManagerクラスのメソッドで接続する 15 Connection conn = null; 16 String url = "jdbc:mysql://192.168.12.15/******"; 17 String user = "******"; 18 String password = "******"; 19 20 try{ 21 conn = (Connection) DriverManager.getConnection(url, user, password); 22 23 // データベースに対する処理 24 25 }catch (SQLException e){ 26 } 27 28 // (3) SQL送信用インスタンスの作成 29 Statement st = (Statement) conn.createStatement(); 30 31 // (4) SQL送信 32 st.executeUpdate("create table sample(id integer primary key)"); 33 Long start = System.currentTimeMillis(); 34 Long delta = start; 35 36 // とりあえず1万回のループ 37 for (int i = 0; i < 10000; i++) { 38 st.executeUpdate("insert into sample values(" + i + ")"); 39 40 // スプリットの計算 41 if (i % 1000 == 0) { 42 Long now = System.currentTimeMillis(); 43 Long split = now - delta; 44 System.out.println("" + i + ": " + split + "ms."); 45 delta = now; 46 } 47 } 48 Long end = System.currentTimeMillis(); 49 System.out.println("所要時間: " + (end - start) + "ms."); 50 51 // (5) 後始末(インスタンスの正常クローズ) 52 st.close(); 53 conn.close(); 54 } 55}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/11 02:07
2017/06/11 03:39
2017/06/11 08:29
2017/06/11 09:34
2017/06/13 02:57