java - Moving from MySQL to MS Azure SQL Database -
i have j2ee web application issues parameterized sql queries mysql back-end. need replace back-end ms azure sql database. have migrated db , data on ms azure sql database. queries app failing. example following query (shown wrapping code) runs fine in management studio fails in java code:
preparedstatement statement = dbconnection.preparestatement("select * [mydb].[apps] [key] = ?;"); statement.setstring(1, appkey); resultset resultset = statement.executequery();
the error is:
com.microsoft.sqlserver.jdbc.sqlserverexception: incorrect syntax near keyword 'key'.
i tried various things removing [], qualifying column name table name, etc. nothing works.
also 1 more question: jdbc connection using string includes database name (mydb) don't want include in each of sql statement. never did mysql i'd rather avoid doing since require me manually add db name each statement in code. if remove db name above query again fails error invalid object name 'apps'
. why isn't db specified in connection string being used default one? connection string using 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 using azure sql database v12 , connecting via microsoft jdbc driver 4.2 sql server.
i tried reproduce issue, sample code ran fine. per experience, think issue cause using incorrect table name form.
the mssql table name completed form <db_name>.<owner_name>.<table_name>. short form <owner_name>.<table_name> or <table_name>. item can <item> or [<item>].
sample code (for azure sql database, same principle mssql on azure vm):
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 jdbc:sqlserver://<host_name>:1433;database=<db_name>;user=<user 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 * person name = ?;" // test table 'person' // table name 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 use third-party universal database management tool "dbeaver". based on eclipse , used jdbc driver connect kinds of database include mssql. can create db connection mssql on azure vm , test sql queries.
best regards.
Comments
Post a Comment