0

我收到以下错误:

我尝试了各种方法出现错误,请参阅以下错误。

运行: java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在 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) 在 com.sqldbsamples.App.main(App.java:23)

构建成功(总时间:0 秒)

这是我的代码:请帮助我如何使用 java 程序将我的数据库连接到 azure !!!

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();
        }
    }
}
4

1 回答 1

1

抱歉,您必须弄清楚您使用的是MySql还是MSSql。你说你用的是mysql。但是,在您的连接字符串中,它是一个sqlserver,这意味着它是一个 MSSql。

下面是使用java访问数据库的教程:

  1. 您需要为您的数据库下载连接器:

  2. 手动将连接器 jar 文件添加到您的类路径。或者,您可以使用 Maven 依赖项管理器在项目中安装和配置 Connector/J 库。

    <!-- Example for mysql -->
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>8.0.18</version>
    </dependency>
    
  3. 代码示例

    • 对于 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();
      }
      
    • 对于 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();
          }
      }
      
于 2020-02-25T20:13:36.517 回答