JavaからMySQLに接続
スケジュールって基本できる時間で組むべきで
理想で組むべきじゃないと思う
残業した時点で既に破綻していることになっているから…
MySQL用のJarを取得する
https://dev.mysql.com/downloads/connector/j/へアクセス
※Oracleアカウントへのサインアップが必要です。
プロジェクトに追加する
データベース構成
テーブル
mysql> create table item( -> id integer not null auto_increment, -> name varchar(50) not null, -> price int not null, -> create_at datetime, -> update_at datetime, -> primary key(id) -> );
サンプルデータ追加
mysql> insert into item(name, price, create_at) values('test1', 100, current_timestamp); Query OK, 1 row affected (0.00 sec) mysql> insert into item(name, price, create_at) values('test2', 150, current_timestamp); Query OK, 1 row affected (0.00 sec) mysql> insert into item(name, price, create_at) values('test2', 3000, current_timestamp); Query OK, 1 row affected (0.00 sec) mysql> insert into item(name, price, create_at) values('test4', 10000, current_timestamp); Query OK, 1 row affected (0.00 sec) mysql> mysql> select * from item; +----+-------+-------+---------------------+-----------+ | id | name | price | create_at | update_at | +----+-------+-------+---------------------+-----------+ | 1 | test1 | 100 | 2016-12-04 02:40:59 | NULL | | 2 | test2 | 150 | 2016-12-04 02:41:08 | NULL | | 3 | test2 | 3000 | 2016-12-04 02:41:13 | NULL | | 4 | test4 | 10000 | 2016-12-04 02:41:25 | NULL | +----+-------+-------+---------------------+-----------+ 4 rows in set (0.00 sec)
実装
取得
package sample_mysql01; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Sample01 { // 接続情報 private static final String URL = "jdbc:mysql://192.168.33.10/myapp?useSSL=false"; private static final String USER = "app"; private static final String PASS = "Password123@"; public static void main(String[] args) { System.out.println("start"); try ( Connection con = DriverManager.getConnection(URL, USER, PASS); Statement st = con.createStatement(); ){ String sql = null; ResultSet rs = null; PreparedStatement pstmt = null; // 単純なSQL sql = "select * from item;"; rs = st.executeQuery(sql); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); int price = rs.getInt("price"); System.out.println("id=" + id + " name=" + name + " price=" + price); } rs.close(); System.out.println(); // プリペアードステートメント sql = "select * from item where price >= ?;"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, 300); rs = pstmt.executeQuery(); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); int price = rs.getInt("price"); System.out.println("id=" + id + " name=" + name + " price=" + price); } rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("end"); } }
↓
start id=1 name=test1 price=100 id=2 name=test2 price=150 id=3 name=test2 price=3000 id=4 name=test4 price=10000 id=3 name=test2 price=3000 id=4 name=test4 price=10000 end
実装
更新
package sample_mysql02; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; public class Sample02 { // 接続情報 private static final String URL = "jdbc:mysql://192.168.33.10/myapp?useSSL=false"; private static final String USER = "app"; private static final String PASS = "Password123@"; public static void main(String[] args) { System.out.println("start"); try ( Connection con = DriverManager.getConnection(URL, USER, PASS); Statement st = con.createStatement(); ){ String sql = null; ResultSet rs = null; PreparedStatement pstmt = null; int result; // プリペアードステートメント sql = "insert into item(name, price, create_at) values(?, ?, ?);"; pstmt = con.prepareStatement(sql); pstmt.setString(1, "hoge"); pstmt.setInt(2, 500); pstmt.setTimestamp(3, new Timestamp(System.currentTimeMillis())); result = pstmt.executeUpdate(); // プリペアードステートメント sql = "update item set name = ?, update_at = ? where name = ?"; pstmt = con.prepareStatement(sql); pstmt.setString(1, "foo"); pstmt.setTimestamp(2, new Timestamp(System.currentTimeMillis())); pstmt.setString(3, "hoge"); result = pstmt.executeUpdate(); // プリペアードステートメント sql = "select * from item where price >= ?;"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, 300); rs = pstmt.executeQuery(); while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); int price = rs.getInt("price"); System.out.println("id=" + id + " name=" + name + " price=" + price); } rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("end"); } }
SE7以降は毎回閉じなくてもいい感じ…
本当は閉じたほうが良いかもしれないけど、try-catchでファイルとかは自動で開放してくれるようになったので
ちょっと楽な感じ
SQLはプログラムでは今後はプリペアしか使わない…
一応サンプルとして使って見たけどセキュリティの問題もあるので使わないようにする