2


I have created a remote sql server 2005 database.
I need to get some data from it. How can I do this? I would be glad if you gave me the code and tell me where I need to insert each thing (like username, password, sever etc.)
Thank you and have a good day!
Ron

RE6
  • 2,684
  • 4
  • 31
  • 57

3 Answers3

3

I suggest you use the features of the standard library in the package java.sql. The DriverManager should have what you need. An example of a connection could look something like this (depending on what driver you use - replace the "com.mysql.jdbc.Driver" with the driver you need):

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(
                        "jdbc:mysql://insert_address_here/",
                        "user","password");
Statement statement   = connection.createStatement();
ResultSet result = statement.executeQuery(
     "SELECT * FROM sometable WHERE id = 1");

.. Just as an example. See more examples in java2s. Particularly under DriverManager and [ResultSet4.

Jens Egholm
  • 2,730
  • 3
  • 22
  • 35
  • How can I know what driver do I need? and the address is the server address? or the server and then the database name? – RE6 Oct 29 '11 at 11:10
  • It throws an exception in the first line you gave me – RE6 Oct 29 '11 at 11:24
  • Is it a mysql database? If so check the link here: http://www.mysql.com/products/connector/ – Jens Egholm Oct 29 '11 at 11:49
  • thanks! now it works. but now it tells me "access denied" :`Access denied for user: '1318560292@46.120.22.144' (Using password: YES)` – RE6 Oct 29 '11 at 11:56
  • Regarding the comment on exceptions you need to bring the driver into scope (i. e. download the driver). This is usually stored in a file provided by the driver manufacturer. In the mysql example you'll find a .rar file with loads of info. If you open the docs folder and navigate to connector-j.html you'll find a (very) detailed description on how to do it. But if should suffice if you find and place the file mysql-connector-java-[version]-bin.jar in your classpath. – Jens Egholm Oct 29 '11 at 11:56
  • Hmm.. I guess it's probably because you're denied access ;) Are you sure you've typed the right user and password? Are you sure the user is allowed remote access? – Jens Egholm Oct 29 '11 at 11:59
  • Positive. Im also using this database in a C# program, and there I have no problem getting access to it – RE6 Oct 29 '11 at 12:02
  • Erm.. I'm not really sure what to do here. It certainly looks as if you're denied access in some way. Did you try to print the stacktrace? – Jens Egholm Oct 29 '11 at 12:08
  • Yes, and it gives me what I have told you – RE6 Oct 29 '11 at 12:09
  • Try catching it in a try-catch loop like so: try { ... code ... } catch (Exception e) { System.out.println(e.printStackTrace) } What does it print? – Jens Egholm Oct 29 '11 at 12:11
  • Well, apperanetly im not using mySql, but Sql Server. but now im back to the 2nd comment – RE6 Oct 29 '11 at 12:13
  • Try to download and use the driver from this webpage: http://www.microsoft.com/download/en/details.aspx?id=21599 the file to download is called 1033\sqljdbc_3.0.1301.101_enu.tar.gz – Jens Egholm Oct 29 '11 at 12:16
  • Did you copy the .jar file into the classpath - meaning: did you either specify the classpath or is it in your project home-folder? And did you remember to change the code-line: Class.forName("...") to the right name? If the class can't be found it must have something to do with the .jar file either not being in place or not containing the class you're looking for. – Jens Egholm Oct 29 '11 at 12:29
  • what the name I need to change Class.forName("...") to? – RE6 Oct 29 '11 at 12:33
  • Look in the files in the .rar. There's a lot of help-docs! It says that the driver is called: "com.microsoft.sqlserver.jdbc.SQLServerDriver". Good luck dude :) – Jens Egholm Oct 29 '11 at 12:37
  • Finally I managed to connect the database (HORRAY), but now I get this exception: `com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed".` – RE6 Oct 29 '11 at 13:04
  • Thanks man!!!!! I used jtds instead, but you helped me so much!!! you are a great man!!!!! – RE6 Oct 29 '11 at 13:19
  • No problem Ron. Glad you figured it out! :) – Jens Egholm Oct 29 '11 at 15:53
1

I suggest you connect sql server using web services there is a lot tutorial on web.

Mustafa Ekici
  • 7,263
  • 9
  • 55
  • 75
1

The most common way of connecting an Android application to a remote database like SQL Server 2005 or higher is to use Web Services.

There are loads of tutorials available for at your disposal. You can use and create a SOAP web service.

For starters, check this

If any problems please feel free to ask me, I have just recently completed a project on the same. I have used SQL Server 2008 as my database and Android as client

Cheers

Community
  • 1
  • 1
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129