在Netbeans中连接Azure数据库的Java程序我收到错误



我得到以下错误:

我尝试了各种方法我得到了一个错误,请查看下面的错误。

运行:java.lang.ClassNotFoundException:com.microsoft.sqlserver.jdbc.SQLServerDriver位于java.net.URLClassLoader.findClass(URLClassLoader.java:381(java.lang.ClassLoader.loadClass(ClassLoader.java:424(sun.mic.Launcher$AppClassLoader.loadClass(Launcher.java:338(java.lang.ClassLoader.loadClass(ClassLoader.java:357(位于的java.lang.Class.forName0(本机方法(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();
}
}
}

对不起,您必须弄清楚您使用的是MySql还是MSSqlqlserver,这意味着它是MSSql。

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

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

    • 对于MySql:MySql连接器/J

    • 对于MSSql:Microsoft JDBC Driver For SQL服务器

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

相关内容

  • 没有找到相关文章

最新更新