如何将 mysql 的数据库连接方法存储在 Java NetBeans 类中,并在 jFrame 中需要时调用它


public void Helper(){
    try{Class.forName("com.mysql.jdbc.DRIVER"); 
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost/kutbuddin","root","*****");
       Statement stmt=con.createStatement();
    }
    catch(Exception e){}

现在我想在为 jButton 编码时调用以下类,而不必一次又一次地编写连接语句

让我们看看你发布的代码。 我假设您正在尝试获取数据库连接。 如果这是真的,为什么不从该方法返回一个?

这是我的写法:

package database.util;
import org.mariadb.jdbc.MySQLDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * Database utilities
 * Created by Michael
 * Creation date 5/3/2016.
 * @link https://stackoverflow.com/questions/36999860/mysql-driver-problems/37000276#comment61553720_37000276
 */
public class DatabaseUtils {
    public static final String DEFAULT_DRIVER = "org.mariadb.jdbc.Driver";
    public static final String DEFAULT_URL = "jdbc:mariadb://localhost:3306/contact";
    public static final String DEFAULT_USERNAME = "contact";
    public static final String DEFAULT_PASSWORD = "contact";
    public static final String DEFAULT_HOST = "localhost";
    public static final int DEFAULT_PORT = 3306;
    public static final String DEFAULT_DATABASE = "contact";
    public static void main(String[] args) {
        Connection connection = null;
        try {
            connection = createConnection(DEFAULT_DRIVER, DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD);
            DatabaseMetaData meta = connection.getMetaData();
            System.out.println(String.format("Connected to %s version %s", meta.getDatabaseProductName(), meta.getDatabaseProductVersion()));
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            close(connection);
        }
    }
    public static DataSource createDataSource(String host, int port, String database) throws ClassNotFoundException, SQLException {
        return new MySQLDataSource(host, port, database);
    }
    public static Connection createConnection(String driverClass, String url, String username, String password) throws ClassNotFoundException, SQLException {
        Class.forName(driverClass);
        return DriverManager.getConnection(url, username, password);
    }
    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Statement st) {
        try {
            if (st != null) {
                st.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新