Recently I have tried to connect to SQL Database server which is in my local network machine. I can connect and access SQL server from my Android app. I did it in the following way….
1. First of all you need a JDBC driver library for SQL Server. As we know android library has only SQLite database driver. So first download an open source JDBC driver from this http://jtds.sourceforge.net/ site (I downloaded the Linux version).
2. Then import the jar file into your Android app.(jtds-1.2.5.jar).
3. Now just try this code by modifying according to your context
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import net.sourceforge.jtds.jdbc.*; public void query2() { Log.i("Android"," MySQL Connect Example."); Connection conn = null; try { String driver = "net.sourceforge.jtds.jdbc.Driver"; Class.forName(driver).newInstance(); //test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class; String connString = "jdbc:jtds:sqlserver://server_ip_address :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;"; String username = "xxxxxx"; String password = "xxxxxxxxxx"; conn = DriverManager.getConnection(connString,username,password); Log.w("Connection","open"); Statement stmt = conn.createStatement(); ResultSet reset = stmt.executeQuery("select * from TableName"); //Print the data to the console while(reset.next()){ Log.w("Data:",reset.getString(3)); // Log.w("Data",reset.getString(2)); } conn.close(); } catch (Exception e) { Log.w("Error connection","" + e.getMessage()); } }
4. You will find more about parameter passing here in connection string http://jtds.sourceforge.net/doc.html .