-1

What is the correct method of connecting to a SQL Server database from Android using JDBC?

I am trying to connect to a SQL Server database from Android but cannot find any SQL Server examples. I found Jav_Rock's answer here for connecting to a MySQL database.

So I replaced his example getConnection() method with my own:

jdbc:sqlserver://xxxxxxxx.xxxx.xxxxx.xxxxxx:1433/dbname

However, I then get the error message:

com.microsoft.sqlserver.jdbc.SQLServerException: The port number 1433/dbname is not valid.

This suggests that the syntax does not support the database name included in the URL. Can anyone help me with the correct method of connecting to a SQL Server database via JDBC?

Community
  • 1
  • 1
Mike Baxter
  • 6,868
  • 17
  • 67
  • 115
  • 2
    You do not want to make a connection to an external database. Create a webservice and sync. – Stefan de Bruijn Nov 07 '13 at 16:27
  • For what reasons? Is there perhaps a good link that will explain it? – Mike Baxter Nov 07 '13 at 16:30
  • 2
    First, JDBC is designed for reliable connectivity to a server. Mobile devices do not have that. Second, it requires you to expose your database to access to your targeted audience, and if your audience is "the world", your database will be hacked. Third, it requires you to bake a database account, including password, into your Android app, and those credentials will be extracted and used against you if your app is available to the public. – CommonsWare Nov 07 '13 at 16:40
  • Also database connections usually perform better with low latency links (a lot of the time database protocols are 'chatty'). Do yourself and your users a favor and go for a webservice. – Mark Rotteveel Nov 07 '13 at 20:46

1 Answers1

0

try this way

Connection connection=DriverManager.getConnection("jdbc:sqlserver://xxx.xxx.x.xxx;databaseName=databasename;integratedSecurity=true;");
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245