0

I'm getting the following error:

I tried various ways I'm getting an error please see the following error.

run: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at com.sqldbsamples.App.main(App.java:23)

BUILD SUCCESSFUL (total time: 0 seconds)

This is my code: please help me with how to connect my database to azure using java program !!!

public class App {

    public static void main(String[] args) {

        // Connect to database
        String hostName = "testchinnaa.database.windows.net:1433"; // update me
        String dbName = "Test_Gopi"; // update me
        String user = "chinna"; // update me
        String password = "******"; // update me

        String url = String.format("jdbc:sqlserver://testchinnaa.database.windows.net:1433;database=Test_Gopi;user=chinna@testchinna;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"
            + "hostNameInCertificate=*.database.windows.net;loginTimeout=30;", hostName, dbName, user, password);
        Connection connection = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            connection = DriverManager.getConnection(url);
            String schema = connection.getSchema();
            System.out.println("Successful connection - Schema: " + schema);

            System.out.println("Query data example:");
            System.out.println("=========================================");

            // Create and execute a SELECT SQL statement.
            String selectSql = "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName "
                + "FROM [SalesLT].[ProductCategory] pc "  
                + "JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid";

            try (Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(selectSql)) {

                // Print results from select statement
                System.out.println("Top 20 categories:");
                while (resultSet.next())
                {
                    System.out.println(resultSet.getString(1) + " "
                        + resultSet.getString(2));
                }
                connection.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
2
  • Is it mysql or mssql? Commented Feb 25, 2020 at 13:52
  • It is mysql using java Commented Feb 25, 2020 at 14:33

1 Answer 1

1

Sorry, you have to figure out if you are using MySql or MSSql. You said that you used mysql. However, in your connection string, it is a sqlserver which means it is a MSSql.

Here is the tutorial for accessing database using java:

  1. You need to download connector for your database:

  2. Manually add connector jar file to your classpath. Or you can use Maven dependencies manager to install and configure the Connector/J library in your project.

    <!-- Example for mysql -->
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>8.0.18</version>
    </dependency>
    
  3. Code sample

    • For MSSql:

      try {
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
          connection = DriverManager.getConnection("conntection_string");
          String SQL = "SELECT name FROM sysdatabases;";
      
          try (Statement statement = connection.createStatement();
               ResultSet resultSet = statement.executeQuery(SQL)) {
      
              // Print results from select statement
              while (resultSet.next()) {
                  System.out.println(resultSet.getString(1));
              }
              connection.close();
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
      
    • For MySql:

      Connection conn = null;
      ResultSet rs = null;
      try {
          Class.forName("com.mysql.cj.jdbc.Driver");
          String connectionUrl = "jdbc:mysql://{server_name}.mysql.database.azure.com:3306?useSSL=true&requireSSL=false&serverTimezone=UTC";;
          conn = DriverManager.getConnection(connectionUrl, "username_from_portal, like: jack@mysqldemo258", "password");
          rs = conn.prepareStatement("show databases").executeQuery();
      
          while(rs.next()){
              System.out.println(rs.getString(1));
          }
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      } catch (SQLException e) {
          e.printStackTrace();
      } finally {
          try{
              if(rs != null) rs.close();
              if(conn != null) conn.close();
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
      
Sign up to request clarification or add additional context in comments.

5 Comments

I'm getting this error: com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "java.security.cert.CertificateException: Failed to validate the server name in a certificate during Secure Sockets Layer (SSL) initialization. The server name is *.database.windows.net, the name in certificate is cr2.eastus1-a.control.database.windows.net.". at
@chinna98 Which jdbc driver version do you use?
It seems to be a problem with old version driver. #623, #816, #836. SO, please check your jdk version and choose right latest driver version.
@chinna98 Do you have any other problems?
No..still I didn't check that driver I will check soon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.