yuusuke-roughの日記

Java,SpringBoot,趣味等

JDBCの学習日記

動作環境

Java SE15

MySQL 8.0.25

MySQL-Connecor-J-8.0.32

 

内容

データソースおよびURL

MySQL :: MySQL Connector/J 8.0 Developer Guide

基本的にMySQLのドキュメントをメインで進める。

 

ORACLEJDBCドキュメントでJDBC XX標準サポートとあったが、

MySQL Connector/J is a JDBC Type 4 driver, implementing the JDBC 4.2 specification. 

との事。

 

もう既にインストールしていたが、手探りで無理やりだったので、1から読んでいく。

MySQLのInstalling from sourceより

  1. Place all the required third-party libraries in a the directory called lib at the root of the source tree (that is, in mysql-connector-j/lib, if you have followed the steps above), or put them elsewhere and supply the location to Ant later (see Step 5 below).

 

java - a simple question about lib folder in Eclipse - Stack Overflow

libどこ...(Eclipse)という事で、Build Pathでクラスパスにjarファイルを追加したのは間違っていなかったとわかった。

 

Class.forName()が何をしているのかは、7.1 Connecting to MySQL Using the JDBC DriverManager Interfaceにあった。

Specify to the DriverManager which JDBC drivers to try to make Connections with. The easiest way to do this is to use Class.forName() on the class that implements the java.sql.Driver interface. With MySQL Connector/J, the name of this class is com.mysql.cj.jdbc.Driver. With this method, you could use an external configuration file to supply the driver class name and driver parameters to use when connecting to a database.

 

そして、Connectionが確立されると、StatementとPreparedStatementオブジャクトを使用できると。

Statementとは...?

SQL文の実行を許可してResultSetクラスを通して結果を取得する。

 

...と読む事に傾いたので動かしていく。

 

... (パスワードなどのDB接続に必要なコードは省略)

try(
        
            Connection con = DriverManager.getConnection(URL, USER, PASS);
            Statement st = con.createStatement();    
            ){
            st.executeUpdate("INSERT INTO PERSON(ID,NAME) values (3, 'Y')");    
            ResultSet rs = st.executeQuery("SELECT * FROM person");
            
            while(rs.next()) {
                System.out.print(rs.getString(1) + " ");
                System.out.print(rs.getString(2) + " ");
            }
     
                }catch(SQLException ex) {
                    ex.printStackTrace();
                }
    

 

ここで、

  st.executeUpdate("INSERT INTO PERSON(ID,NAME) values (3, 'Y')");    
  ResultSet rs = st.executeQuery("SELECT * FROM person");

上記、2文をtryに置こうとするとSyntacs errorになる。(私のtry-with-resourcesに対する理解が足りないだけでした。)

try-with-resourcesとAutoCloseable - Qiita

 

ResultSetもConnectionもAutoCloseableを実装しているから記載できると。

BufferedReaderもReaderにCloseableが実装されている。

 

Statementについては下記記載があった。

To create a Statement instance, you call the createStatement() method on the Connection object you have retrieved using one of the DriverManager.getConnection() or DataSource.getConnection() methods described earlier.

 

日記

JDBCについては、まだまだ慣れません。ただPreparedStatementといったものを使用する理由についても学習する機会ですので、Java Gold範囲でも重点的に進めたいです。

 

今の会社に拾って頂いて、私はIT業界に飛び込もうとしている訳ですが、ここに至る経緯は17歳の春から続くもので、誰からも見向きすらされないのは当たり前でした。

そのような事情からも、自分には過度な期待はせず、ただ静かに、望むような一日を送る事を大切にしてきました。

なので、世間は「GPT-4が平均的なプログラマーよりAIの方がコードを書ける、仕事が無くなる」という話題で盛り上がっておりますが、「ああ、そうか」くらいの心地に収まります。

道半ばで終わるのも受け入れられる人生であればいいと思うので、働いて、勉強するだけです。

そのような生活の先にすら仕事がないなら実家に帰ります。それだけです。