jdbcの簡単な使用と注意事項

1826 ワード

レプリケーションの使用
データベース名をblogとする
jdbc:mysql://localhost:3306/blog?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false

Mybatisでの書き方は
jdbc:mysql://localhost:3306/blog?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

せつぞく
String driver="com.mysql.cj.jdbc.Driver";
//旧バージョンではcomとして駆動する.mysql.jdbc.Driver
String url="jdbc:mysql://localhost:3306/eng?serverTimezone=UTC";
//旧バージョンでは、末尾に付ける必要はありませんか?serverTimezone=UTC
String username="root";
String password="root";
Class.forName(driver)
Connection conn=(Connection) DriverManager.getConnection(url, username, password);
接続が確立されました
表示される場合
Sat Nov 18 19:27:23 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
これは警告で間違いではなく、後で使うのは影響しません.大体の意味はssl接続を確立するが、サーバにはアイデンティティ認証がなく、この方法は推奨されない.
String="jdbc:mysql://localhost:3306/eng?useUnicode=true&characterEncoding=utf-8&useSSL=false";
これに変えればいい
 
使用
できるだけ前処理文を使って添削して調べる
String sql="insert into test (name,address) values(?,?)";
PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1、「張三」);
pstmt.setString(2、「山東」);
pstmt.executeUpdate();
pstmt.close();