MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

Java-MySQLアプリケーション全体で1つのデータベース接続オブジェクトを操作するにはどうすればよいですか?


シングルトンデザインパターンを使用します。これが単一のオブジェクトを返すJavaコードです-

ConnectDatabase.java

import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectDatabase {
   static Connection conn = null;
   public static Connection getConnection() {
      if (conn != null) return conn;
      String database = "test";
      String Username = "root";
      String password = "123456";
      return getConnection(database, Username, password);
   }
   private static Connection getConnection(String databaseName, String UserName, String password) {
      try {
         Class.forName("com.mysql.jdbc.Driver");
         conn = DriverManager.getConnection("jdbc:mysql://localhost/" + databaseName + "?user=" + UserName + "&password=" + password);
      } catch (Exception e) {
         e.printStackTrace();
      }
      return conn;
   }
}

上記のメソッドを呼び出すクラスは次のとおりです-

CallConnection.java

import java.sql.Connection;
public class CallConnection {
   public static void main(String[] args) {
      Connection con = ConnectDatabase.getConnection();
      if (con != null) {
         System.out.println("Connection successful !!!");
      }
   }
}   

出力

Java-MySQLアプリケーション全体で1つのデータベース接続オブジェクトを操作するにはどうすればよいですか?

上記の出力の内容は次のとおりです-

Mon Feb 11 20:15:32 IST 2019 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.
Connection successful !!!

  1. Wink HubでZ-Wave接続の問題を修正するにはどうすればよいですか?

    Z-Wave 主にホームオートメーションに使用され、ワイヤレス通信プロトコルに基づいています。低エネルギー電波のメッシュネットワークを使用して、アプライアンス間で通信します。 ウィンクハブ は、スマートホーム製品のさまざまなプロトコル/テクノロジーを1つに接続する、Wink Labs Incの製品であり、WinkAppによって制御されます。 Bluetooth Low Energy(LE)、Wi-Fi、Z-Wave、Kidde、Lutron Clear Connect、ZigBeeと互換性があります。これらは、ほとんどの製品に見られる主要なスマートホームプロトコルです。 Winkハブは

  2. Windows 10 の自動再生オプションにアクセスして操作する方法

    Windows にある最も古い機能の 1 つ オペレーティング システムは AutoPlay と呼ばれます . Windows 98、Windows XP から存在しています 次に Vista まで 、Windows 7 Windows 8、Windows 8.1 に引き継がれました そして今、Windows 10 に ! ただし、Windows のごく一部にすぎません ユーザーは、Windows を 1 つでも保存できるこの重要な機能に注目しています。 USB フラッシュ ドライブなどのポータブル ストレージ デバイスからのウイルスにマシンが感染するのを防ぎます。 およ