Połączenie z bazą danych MySQL

Podstawowy connector

				
					        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
				
			
				
					import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConnect {

    private final static String URL = "jdbc:mysql://localhost:3306/javatest";
    private final static String USER = "root";
    private final static String PASSWORD = "";

    public static Connection connect() {

        Connection connection = null;
        try {
            connection = (Connection) DriverManager.getConnection(URL,USER,PASSWORD);
            System.out.println("connected");

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return connection;
    }
}
				
			

MariaDb connector

				
					        <!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>3.0.3</version>
        </dependency>
				
			
				
					import org.mariadb.jdbc.Connection;

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

public class DbConnectMariaDb {

    private final static String URL = "jdbc:mariadb://localhost:3306/javatest";
    private final static String USER = "root";
    private final static String PASSWORD = "";

    public static Connection connect() {

        Connection connection = null;
        try {
            connection = (Connection) DriverManager.getConnection(URL,USER,PASSWORD);
            System.out.println("connected");

        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return connection;
    }
}
				
			

Zostaw komentarz