Best way to open and return a database connection in a Java application?

前端 未结 3 796
长发绾君心
长发绾君心 2021-01-11 13:08

I have come up with the following utility class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySql         


        
3条回答
  •  太阳男子
    2021-01-11 13:35

    You can throw an exception in your original code upwards, and then have to deal with a possible exception every time you would want to get a connection. Granted, you will not have null issues, but this could end up being more work. However, it does make it very explicit that you have to handle the connection not working, and this could be clearer than simply returning a null.

提交回复
热议问题