简体   繁体   English

从MySQL迁移到MS Azure SQL数据库

[英]Moving from MySQL to MS Azure SQL Database

I have a J2EE web application that issues parameterized SQL queries to a MySQL back-end. 我有一个J2EE Web应用程序,它向MySQL后端发出参数化的SQL查询。 I need to replace the back-end with MS Azure SQL Database. 我需要用MS Azure SQL数据库替换后端。 I have migrated the DB and data over to MS Azure SQL Database. 我已将数据库和数据迁移到MS Azure SQL数据库。 However all my queries from the app are failing. 但是,我从该应用程序发出的所有查询均失败。 For example the following query (shown with the wrapping code) runs perfectly fine in the Management Studio but fails in the java code: 例如,以下查询(与包装代码一起显示)在Management Studio中运行正常,但在Java代码中失败:

PreparedStatement statement = dbConnection.prepareStatement("SELECT * FROM [mydb].[apps] WHERE [key] = ?;");
statement.setString(1, appKey);
ResultSet resultSet = statement.executeQuery();

The error I get is: 我得到的错误是:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'key'.

I tried various things like removing the [], qualifying the column name with the table name, etc. but nothing works. 我尝试了各种操作,例如删除[],用表名限定列名等,但是没有任何效果。

Also one more question: The JDBC connection I am using string includes the database name (mydb) so I don't want to include it in each of my SQL statement. 还有一个问题:我正在使用的JDBC连接字符串包括数据库名称(mydb),所以我不想在每个SQL语句中都包含它。 I never did for MySQL so I'd rather avoid doing it now since it would require me to manually add the DB name to each statement in the code. 我从来没有为MySQL做过,所以我现在宁愿避免这样做,因为这将需要我手动将数据库名称添加到代码中的每个语句中。 However if I remove the DB name from the above query it again fails with error Invalid object name 'apps' . 但是,如果我从上述查询中删除了数据库名称,则它再次失败,并显示错误的Invalid object name 'apps' Why isn't the DB specified in the connection string being used as the default one? 为什么不将连接字符串中指定的数据库用作默认数据库? The connection string I am using is jdbc:sqlserver://{servername}.database.windows.net:1433;database=mydb;user={username}@{servername};password={password};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30; 我正在使用的连接字符串是jdbc:sqlserver://{servername}.database.windows.net:1433;database=mydb;user={username}@{servername};password={password};encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;

BTW I am using the Azure SQL Database V12 and connecting via Microsoft JDBC Driver 4.2 for SQL Server. 顺便说一句,我正在使用Azure SQL数据库V12,并通过用于SQL Server的Microsoft JDBC驱动程序4.2进行连接。

I tried to reproduce your issue, but my sample code ran fine. 我试图重现您的问题,但是我的示例代码运行良好。 Per my experience, I think that the issue cause is by using incorrect table name form. 根据我的经验,我认为问题原因是由于使用了不正确的表名形式。

The MSSQL table name completed form is <db_name>.<owner_name>.<table_name>. MSSQL表名称完成的形式为<db_name>。<owner_name>。<table_name>。 Its short form could be <owner_name>.<table_name> or <table_name>. 它的缩写形式可以是<owner_name>。<table_name>或<table_name>。 The item can be <item> or [<item>]. 该项目可以是<item>或[<item>]。

Sample Code (for Azure SQL Database, the same principle as MSSQL on Azure VM): 示例代码(对于Azure SQL数据库,与Azure VM上的MSSQL相同的原理):

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String jdbcUrl = "jdbc:sqlserver://<host_name>:1433;database=<db_name>;";
//The completed connection string is jdbc:sqlserver://<host_name>:1433;database=<db_name>;user=<user like username@server_name>;password={your_password_here};encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
        String user = "<user>";
        String password = "<password>";
        Connection conn = DriverManager.getConnection(jdbcUrl, user, password);
        String sql = "SELECT * FROM person WHERE name = ?;" // My test table is 'person'
// The table name could be person, [person], dbo.person, [dbo].[person], <db_name>.dbo.person, [<db_name>].[dbo].[person]
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, "Peter Pan");
        ResultSet rs = statement.executeQuery();
        while(rs.next()) {
            System.out.println(rs.getLong("id")+","+rs.getString("name"));
        }
    }
}

I suggest you to use the third-party Universal Database Management Tool "Dbeaver". 我建议您使用第三方通用数据库管理工具“ Dbeaver”。 It based on Eclipse and used JDBC Driver to connect kinds of Database include MSSQL. 它基于Eclipse并使用JDBC驱动程序来连接包括MSSQL在内的各种数据库。 You can create db connection to MSSQL on Azure VM and test SQL queries. 您可以在Azure VM上创建与MSSQL的数据库连接并测试SQL查询。

Best Regards. 最好的祝福。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM