0
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){}

Now I Want to call the following class when I am coding for jButton and do not have to write the connectivity statement again and again

  • Encapsulate them in a class, test them, and give a reference to the class to your JFrame. – duffymo Nov 04 '17 at 12:40
  • I did not understand what you told me – youngdevelop Nov 04 '17 at 13:19
  • I know. That's the problem. How to say it differently? Let's try: Put that connection logic in a separate class. In your JFrame, call new to create an instance of that class and call its methods. You only write it once, but call it multiple times. – duffymo Nov 04 '17 at 15:11
  • I tried that already it does not work for me – youngdevelop Nov 04 '17 at 16:32
  • What does "not work" look like? This is what programming is. Not everyone can do it. – duffymo Nov 04 '17 at 16:52
  • Okay see what I am doing is I am connecting my netbeans project with mysql.Now to connect my java buttons with mysql I have to write a three line long code I don't want to write them again and again and just want to call them from a class – youngdevelop Nov 05 '17 at 00:44
  • I tried your way by creating the reference class object and calling the method helper() from above code.But it does not work,and the variable that I use from the class after the method declaration are not accepted – youngdevelop Nov 05 '17 at 00:46
  • Post some code and some error messages. I can assure you from past experience that this works if you do it correctly. You are wrong again. – duffymo Nov 05 '17 at 12:02

1 Answers1

0

Let's look at the code you posted. I assume that you're trying to get a database connection. If that's true, why don't you return one from the method?

Here's how I would write it:

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();
        }
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561