【问题标题】:how to make a global MySQL connection method in Java如何在 Java 中创建全局 MySQL 连接方法
【发布时间】:2017-07-24 00:59:45
【问题描述】:

我正在尝试制作一个需要连接到 MySQL DB 并执行用户名和密码检查(登录)和其他 CRUD 应用程序的 JavaFX 应用程序。下面是我的 Java 类,用于连接和检查用户名和密码。但是,每次我需要确保用户连接到数据库或执行其他 CRUD 活动时,我认为这并不专业,我必须创建整个步骤...... 有没有更专业的方法来创建一个可以从任何地方访问的全局方法,以便我可以调用它来为我做数据库连接?

public void LoginButtonHandler(ActionEvent event){

    user.setUsername(username.getText().trim());
    user.setPassword(password.getText().trim());
    String Username = user.getUsername();
    String Password = user.getPassword();

    try{           
        Class.forName("com.mysql.jdbc.Driver");  // MySQL database connection
        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/airinvoice?" + "user=root&password=*******");     
        PreparedStatement pst = conn.prepareStatement("Select * from users where username=? and password=?");
        pst.setString(1, Username); 
        pst.setString(2, Password);
        ResultSet rs = pst.executeQuery();                        
        if(rs.next())   {
            Date dNow = new Date( );
            System.out.println("logged in with username: "+Username+" "+dNow);

            Alert alert = new Alert(AlertType.INFORMATION);
            alert.setTitle("Notification");
            alert.setHeaderText("you are logged in successfully");
            alert.setContentText("you are logged in as "+Username);
            alert.show();
        }         
        else{
            System.out.println("error logging in !");
            Alert erroAlert = new Alert(AlertType.INFORMATION);
            erroAlert.setTitle("Notification");
            erroAlert.setHeaderText("login unsuccessful");
            erroAlert.setContentText("check your username or password");
            erroAlert.show();
        }         
    }
    catch(Exception e){
        e.printStackTrace();
    }
}//end method LoginButtonHandler

【问题讨论】:

  • 您可以使用 Hibernate 或创建自己的 pojo,也可以创建自己的数据源来为您管理连接。
  • @VinayPrajapati hibernate 仅适用于 Web 应用程序吗? ...在我的情况下,我正在使用 JavaFX 希望它可以与 JavaFX 一起使用
  • hibernate 不依赖于 Web 应用程序。请记住,这是一个 ORM 框架,而不是 MVC。

标签: java mysql javafx


【解决方案1】:

您可以创建一个连接到数据库的单独类,例如:

import java.sql.*;

public class DBConnection {

  static final String DRIVER = "com.mysql.jdbc.Driver";
  static final String DB_URL = "jdbc:mysql://localhost/yourDatabaseName";
  static final String DB_USER = "root";
  static final String DB_PASSWORD = "";
  Connection connection;

  public DBConnection(){
    connect();
  }

  public void connect(){
    try{
      Class.forName(DRIVER);
      connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
    }catch(SQLException|ClassNotFoundException e){
      System.out.println("ERROR connecting to database!");
      System.out.println(e.toString());
    }
  }

  public ResultSet select(String query){
    try{
      Statement statement = connection.createStatement();
      ResultSet result = statement.executeQuery(query);
      return result;
    }catch(SQLException e){
      System.out.println("ERROR while executing select query!");
      System.out.println(e.toString());
      return null;
    }
  }

  public int update(String query){
    try{
      Statement statement = connection.createStatement();
      return statement.executeUpdate(query);
    }catch(SQLException e){
      System.out.println("ERROR while executing update query");
      System.out.println(e.toString());
      return -1;
    }
  }

  public int delete(String query){
    try{
      Statement statement = connection.createStatement();
      return statement.executeUpdate(query);
    }catch(SQLException e){
      System.out.println("ERROR while deleting line!");
      System.out.println(e.toString());
      return -1;
    }
  }

  public void close(){
    try{
      connection.close();
    }catch(SQLException e){
      System.out.println("ERROR while closing connections!");
      System.out.println(e.toString());
    }
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 2020-03-04
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    相关资源
    最近更新 更多