0

So I'm trying to connect to my MySQL database hosted by AWS with Java so I can add some of my own modules but I'm having issues with the driver. I got the code from a guide online and have tried to fix it with imports, but the issue i'm getting from eclipse is: Got an exception! No suitable driver found for jdbc:sqlserver://ls-303a92f8cfd2da433af284eee07a1052317e9e14.c7xhy47efuuf.ca-central-1.rds.amazonaws.com

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.*;
import com.mysql.*;
import java.util.*;

public class JavaMysqlPreparedStatementInsertExample
{

  public static void main(String[] args)
  {
    try
    {
      // create a mysql database connection
      String myDriver = "com.mysql.cj.jdbc.Driver";
      String myUrl = "ls-303a92f8cfd2da433af284eee07a1052317e9e14.c7xhy47efuuf.ca-central-1.rds.amazonaws.com";
      Class.forName(myDriver);
      Connection conn = DriverManager.getConnection(myUrl, "user", "mypassword");
    
      // create a sql date object so we can use it in our INSERT statement
      Calendar calendar = Calendar.getInstance();
      java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());

      // the mysql insert statement
      String query = " insert into users (first_name, last_name, date_created, is_admin, num_points)"
        + " values (?, ?, ?, ?, ?)";

      // create the mysql insert preparedstatement
      PreparedStatement preparedStmt = conn.prepareStatement(query);
      preparedStmt.setString (1, "Barney");
      preparedStmt.setString (2, "Rubble");
      preparedStmt.setDate   (3, startDate);
      preparedStmt.setBoolean(4, false);
      preparedStmt.setInt    (5, 5000);

      // execute the preparedstatement
      preparedStmt.execute();
      
      conn.close();
    }
    catch (Exception e)
    {
      System.err.println("Got an exception!");
      System.err.println(e.getMessage());
    }
  }
}
  • Welcome to Stack Overflow. Might this help? https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database Incidentally what is this driver? ```"com.mysql.cj.jdbc.Driver";```? – ewokx May 04 '22 at 01:24
  • 1
    Are you sure the remote db you're connecting to is mySQL? – MarsAtomic May 04 '22 at 01:27
  • 1
    `sqlserver` is for MS SQL - that's short for 'Microsoft SQL server'. It has absolutely nothing whatsoever to do with MySQL - they just.. sound unfortunately similar. Perhaps you've confused the two - you won't be the first :) – rzwitserloot May 04 '22 at 02:01
  • How come your connection URL becomes this `jdbc:sqlserver://ls-303a92f8cfd2da433af284eee07a1052317e9e14.c7xhy47efuuf.ca-central-` – HariHaravelan May 04 '22 at 07:11
  • URI looks all wrong. At the minimum, it should be showing a host part and a db part. It looks to me like just one string. General form: `jdbc:mysql://HOST/DB`. I suggest you start out by trying to connect with a mysql client at the command line. You'll at least know that you *can* (or can't) connect and what the host and db should be. – g00se May 04 '22 at 11:09

0 Answers0