Help Center > > FAQs> Database Connection> How Can I Connect to a MySQL Database Through JDBC?

How Can I Connect to a MySQL Database Through JDBC?

Updated at: May 26, 2020 GMT+08:00

If you are connecting to a MySQL database through Java database connectivity (JDBC), the SSL certificate is optional. For security reasons, you are advised to download the SSL certificate to encrypt the connection. SSL is disabled by default for MySQL DB instances. You can enable SSL by referring to Configuring an SSL Connection. SSL encrypts connections to databases but it increases the connection response time and CPU usage. Therefore, you are advised not to enable SSL.

Prerequisites

You must be familiar with:

  • Computer basics
  • Java programming language
  • JDBC basic knowledge

Connection with the SSL Certificate

The JDBC connection is an SSL connection. The SSL certificate needs to be downloaded and verified for connecting to databases.

In the DB Information area on the Basic Information page, click in the SSL field to download the root certificate or certificate bundle.

  1. Connect to the RDS MySQL DB instance through JDBC.

    jdbc:mysql://<instance_ip>:<instance_port>/<database_name>?sslmode=verify-full&sslrootcert=<ca.pem>
    Table 1 Parameter description

    Parameter

    Description

    <instance_ip>

    If you are accessing the RDS DB instance through an ECS, instance_ip indicates the floating IP address displayed on the Basic Information page of the DB instance to which you intend to connect.

    If you are accessing the RDS DB instance through an EIP, instance_ip indicates the EIP that has been bound to the DB instance.

    <instance_port>

    Indicates the database port displayed on the Basic Information page. The default port is 3306.

    <database_name>

    Indicates the name of the database to which you intend to connect. The default database name is mysql.

    sslmode

    Indicates the SSL connection mode. The default mode is verify-full.

    sslrootcert

    Indicates the directory of the CA certificate for the SSL connection. The certificate should be stored in the directory where the command is executed.

    Example script in Java:

    import java.sql.Connection; 
    import java.sql.DriverManager; 
    import java.sql.ResultSet; 
    import java.sql.Statement;
    import java.sql.SQLException; 
     
    public class MyConnTest { 
        final public static void main(String[] args) { 
            Connection conn = null; 
             Statement stat = null;
            // set sslmode here. 
            // with ssl certificate and path. 
            String url = "jdbc:mysql://192.168.0.225:3306/my_db_test?sslmode=verify-full&sslrootcert=/home/Ruby/ca.pem"; 
     
            try { 
                Class.forName("com.mysql.jdbc.Driver"); 
                conn = DriverManager.getConnection(url, "root", "password"); 
                System.out.println("Database connected"); 
     
                Statement stmt = conn.createStatement(); 
                  String sql = "SELECT * FROM mytable WHERE columnfoo = 500";
                ResultSet rs = stmt.executeQuery sql); 
                while (rs.next()) { 
                    System.out.println(rs.getString(1)); 
                } 
     
                rs.close(); 
                stmt.close(); 
                conn.close(); 
            } catch (Exception e) { 
                e.printStackTrace(); 
                System.out.println("Test failed"); 
            } finally { 
                // release resource .... 
            } 
        } 
    }

Connection Without the SSL Certificate

The JDBC connection is an SSL connection, but you do not need to download the SSL certificate because the certificate verification on the server is not required.

  1. Connect to the RDS MySQL DB instance through JDBC.

    jdbc:mysql://<instance_ip>:<instance_port>/<database_name>?sslmode=require
    Table 2 Parameter description

    Parameter

    Description

    <instance_ip>

    If you are accessing the RDS DB instance through an ECS, instance_ip indicates the floating IP address displayed on the Basic Information page of the DB instance to which you intend to connect.

    If you are accessing the RDS DB instance through an EIP, instance_ip indicates the EIP that has been bound to the DB instance.

    <instance_port>

    Indicates the database port displayed on the Basic Information page. The default port is 3306.

    <database_name>

    Indicates the name of the database to which you intend to connect. The default database name is mysql.

    sslmode

    Indicates the SSL connection mode. require indicates that data needs to be encrypted.

    Example script in Java:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class MyConnTest {
    	final public static void main(String[] args) {
    		Connection conn = null;
    		// set sslmode here.
    		// no ssl certificate, so do not specify path.
    		String url = "jdbc:mysql://192.168.0.225:3306/my_db_test?sslmode=require";
    		try {
    			Class.forName("org.postgresql.Driver");
    			conn = DriverManager.getConnection(url, "root", "password");
    			System.out.println("Database connected");
    
    			Statement stmt = conn.createStatement();
    			ResultSet rs = stmt.executeQuery("SELECT * FROM mytable WHERE columnfoo = 500");
    			while (rs.next()) {
    				System.out.println(rs.getString(1));
    			}
    			rs.close();
    			stmt.close();
    			conn.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    			System.out.println("Test failed");
    		} finally {
    			// release resource ....
    		}
    	}
    }

Did you find this page helpful?

Submit successfully!

Thank you for your feedback. Your feedback helps make our documentation better.

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel